Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different font size in the same label?

Is it possible to have different fontsize or weight in the same UILabel? I can do it in the storyboard as Attributed label, but I need to do it programmatically.

cell.lblOne.text = [NSString stringWithFormat:
                       @"FontSize15:: %@, FontSize20:: %@",monkey, goat];

Edit: I saw something about NSAttributedString but I can't get it to work.

like image 482
hugocarlmartin Avatar asked Jan 17 '13 11:01

hugocarlmartin


1 Answers

Take a look at my answer here:

UITextView Alternative

  • make an NSMutableAttributedString
  • give it some attributes (applied to ranges of characters)
  • set the label's attributedText property

.

 NSMutableAttributedString *attString = 
                              [[NSMutableAttributedString alloc]
                                        initWithString: @"monkey goat"];

[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)];

self.label.attributedText  = attString;
like image 59
foundry Avatar answered Sep 21 '22 00:09

foundry