BDG*_*pps 5 nsattributedstring ios
如何在iPhone上设置NSAtributedString的字体.我在网上找到了如何为mac做这件事,但它不一样.当我试图将它转换到iOS平台时,它无法正常工作.我需要设置字体名称和字体大小.
NSDictionary *attributes = [[NSDictionary alloc] initWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica" size:26], kCTFontAttributeName,
[UIColor blackColor], kCTForegroundColorAttributeName, nil];
Run Code Online (Sandbox Code Playgroud)
看看代码
infoString=@"This is an example of Attributed String";
NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];
NSInteger _stringLength=[infoString length];
UIColor *_black=[UIColor blackColor];
UIFont *font=[UIFont fontWithName:@"Helvetica-Bold" size:30.0f];
[attString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, _stringLength)];
[attString addAttribute:NSForegroundColorAttributeName value:_black range:NSMakeRange(0, _stringLength)];
Run Code Online (Sandbox Code Playgroud)
值kCTFontAttributeName必须是a CTFontRef,而不是a UIFont *.你不能直接将a转换UIFont为a CTFont.您应该可以使用CTFontCreateWithName它来创建它.完成后,您需要使用CFRelease它,以避免内存泄漏.查看CTFont参考.
此外,值kCTForegroundColorAttributeName必须是a CGColorRef,而不是a UIColor *.你可以轻松地解决这个问题[UIColor blackColor].CGColor.
如果您正在使用UIKit属性字符串支持(在iOS 6.0中添加),则可以使用NSFontAttributeName带有a 的键UIFont作为值.您还可以使用NSForegroundColorAttributeName带有UIColor值的键.请参阅NSAttributedString UIKit Additions Reference.
| 归档时间: |
|
| 查看次数: |
7309 次 |
| 最近记录: |