Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set different font in One UIlabel?

I have a string like "This is a good apple." to display on my UIlabel. I would like to set the word "good" with different font. Just look like "This is a good apple."

like image 212
lu yuan Avatar asked Apr 20 '12 08:04

lu yuan


3 Answers

Take a look at NSAttributedString... Use OHAttributedLabel draw NSAttributedString because UILabel,UITextView doesnot support NSAttributedString...

PS: if you plan to distribute an iOS6 or newer application, as UILabel now support NSAttributedString, you should use UILabel directly instead of OHAttributedLabel as it is now natively supported by the OS.

like image 73
KingofBliss Avatar answered Oct 14 '22 08:10

KingofBliss


There is a way to set different / multiple fonts & other properties on Label using NSMutableAttributedString. Foll is my code:

 UIFont *ArialFont = [UIFont fontWithName:@"arial" size:18.0];
 NSDictionary *arialdict = [NSDictionary dictionaryWithObject: ArialFont forKey:NSFontAttributeName];    
 NSMutableAttributedString *AattrString = [[NSMutableAttributedString alloc] initWithString:title attributes: arialdict];

 UIFont *VerdanaFont = [UIFont fontWithName:@"verdana" size:12.0];
 NSDictionary *veradnadict = [NSDictionary dictionaryWithObject:VerdanaFont forKey:NSFontAttributeName];
 NSMutableAttributedString *VattrString = [[NSMutableAttributedString alloc]initWithString: newsDate attributes:veradnadict];    
 [VattrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:(NSMakeRange(0, 15))];

 [AattrString appendAttributedString:VattrString];


 lblText.attributedText = AattrString;

Note that lblText is the UILabel, outlet as file owner.
One can keep on appending as many NSMutableAttributedString he wants..

Also Note that I've added verdana & arial font in my project & added a plist for the same.

like image 4
Akshay Avatar answered Oct 14 '22 07:10

Akshay


This is not possible with UILabel. But you could use Core Text.

An other easier approach could be to use a very small UIWebView. In this you can set the font using html commands.

like image 1
dasdom Avatar answered Oct 14 '22 09:10

dasdom