iOS 在UILabel显示不同的字体和颜色
现实代码:
.h文件
@interface
ViewController : UIViewController
@property
(
nonatomic
, strong)
IBOutlet
UILabel *attrLabel;
- (
IBAction
)next:(
id
)sender;
@end
.m文件
在viewDidLoad方法中添加以下代码:
self
.title = @
"For iOS 6 & later"
;
NSMutableAttributedString
*str = [[
NSMutableAttributedString
alloc] initWithString:@
"Using NSAttributed String"
];
[str addAttribute:
NSForegroundColorAttributeName
value:[UIColor blueColor] range:
NSMakeRange
(0,5)];
[str addAttribute:
NSForegroundColorAttributeName
value:[UIColor redColor] range:
NSMakeRange
(6,12)];
[str addAttribute:
NSForegroundColorAttributeName
value:[UIColor greenColor] range:
NSMakeRange
(19,6)];
[str addAttribute:
NSFontAttributeName
value:[UIFont fontWithName:@
"Arial-BoldItalicMT"
size:30.0] range:
NSMakeRange
(0, 5)];
[str addAttribute:
NSFontAttributeName
value:[UIFont fontWithName:@
"HelveticaNeue-Bold"
size:30.0] range:
NSMakeRange
(6, 12)];
[str addAttribute:
NSFontAttributeName
value:[UIFont fontWithName:@
"Courier-BoldOblique"
size:30.0] range:
NSMakeRange
(19, 6)];
attrLabel.attributedText = str;
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。