0

I am recently learning iOS development, i am wondering if that is possible to have one label with different size, different fonts and different colors, such as

user name(blue, bolder, and large font) is watching walking dead (red, bolder and medium font) on xxxx site (blue, bolder and large font)

Thanks!

  • 4
    for that you have to used NSAttributedString. – Dhaval Bhadania Feb 07 '14 at 05:12
  • Please, before posting, have a look at the related questions that SO shows you. Or do some searching first. – rmaddy Feb 07 '14 at 05:21
  • Check this one: http://stackoverflow.com/questions/3482346/how-do-you-use-nsattributedstring – Dhaval Bhadania Feb 07 '14 at 05:24
  • I think this is what you're looking for http://stackoverflow.com/a/12853645/501487 – aToz Feb 07 '14 at 05:28
  • NSAttributedString is the way to go. If you're trying for iOS7 alone, check out TextKit. It gives you great capabilities. https://developer.apple.com/Library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/CustomTextProcessing/CustomTextProcessing.html – Anil Feb 07 '14 at 05:31

2 Answers2

4

There are 2 ways to do this

  1. xib file or storyboard

    go the label, and choose attribute instead of plain enter image description here

    then do whatever you want there enter image description here

  2. codes

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"user name is watching walking dead on xxx site"];
    
        [attributedString addAttributes:[[NSDictionary alloc] initWithObjectsAndKeys:
                                        NSFontAttributeName, [UIFont fontWithName:@"WHATEVER FONT" size:FONT_SIZE_HERE],
                                        NSForegroundColorAttributeName, [UIColor blueColor],
                                        nil]
                          range:NSMakeRange(0, 9)];//9 is the length of "user name"
    
        [attributedString addAttributes:[[NSDictionary alloc] initWithObjectsAndKeys:
                                 NSFontAttributeName, [UIFont fontWithName:@"WHATEVER FONT" size:FONT_SIZE_HERE],
                                 NSForegroundColorAttributeName, [UIColor blueColor],
                                 nil]
                          range:NSMakeRange(22, 12)];//22 is the start index of "Walking dead"
                                                     //and 12 is the length of "Walking dead"
    
    //you got the idea, same way to do the xxx site.
    //Check a file called "NSAttributedString.h" 
    //you will find even more options there
    

I personally prefer the second solution, since you have more options in the code, and it works for pretty much all the cases. But there is a learning curve.

Hope that helps

Xu Yin
  • 3,932
  • 1
  • 26
  • 46
0

In your case you need to use NSMutableAttributedString.

NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString: @"monkey goat"];        
UILabel *testingLbl = [[UILabel alloc] init];
testingLbl.frame = CGRectMake(50, 100, 200, 30);
[attString addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange(0,6)];                
[attString addAttribute: NSFontAttributeName value:  [UIFont fontWithName:@"Helvetica" size:15] range: NSMakeRange(0,6)];        
[attString addAttribute: NSFontAttributeName value:  [UIFont fontWithName:@"Didot" size:24] range: NSMakeRange(7,4)];        
testingLbl.attributedText  = attString;
[self.view addSubview:testingLbl];

And also this is the best site for find any custom controllers, Bye the way in your case fine UILabel with source code:

iPatel
  • 46,010
  • 16
  • 115
  • 137