开发者

how to set different font for a UILabel which has text containing both chinese and english characters separately?

开发者 https://www.devze.com 2023-04-04 14:09 出处:网络
I have a UILabel which has tex开发者_如何转开发t containing both chinese and english characters,

I have a UILabel which has tex开发者_如何转开发t containing both chinese and english characters,

now I want to set a font for chinese and another font for english,

how to do this?


There are couple of things that might be of interesting to you:

OHAttributedLabel

and TTTAttributedLabel

OHAttributedLabel stays it is capable of dealing with mixed fonts, color, size, ...


Generally one label can have only one font. Still if you want to show different font for different languages than you can keep different language string in different labels and arrange them the way you want.

See this to decide size of your labels.

Resize UITableViewCell to UILabel's height dynamically

and this is also helpful.

How do I wrap text in a UITableViewCell without a custom cell


I do not believe this is possible. The font property set in a UILabel would apply to the entire string specified in the text property of that UILabel.


I've not tried using chinese font, but you can use the following code 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号