IOS--经常使用控件--UILabel

1.在label中显示不一样的颜色和字体字体

在项目开发中,咱们常常会遇到在这样一种情形:在一个UILabel 使用不一样的颜色或不一样的字体来体现字符串,在iOS 6 之后咱们能够很轻松的实现这一点,官方的API 为咱们提供了UILabel类的attributedText, 使用不一样颜色和不一样字体的字符串,咱们能够使用NSAttributedText 和 NSMutableAttributedText 类来实现。atom

现实代码:spa

1
2
3
4
@interface  ViewController : UIViewController
@property  ( nonatomic , strong) IBOutlet  UILabel *attrLabel;
- ( IBAction )next:( id )sender;
@end

 .m文件 在viewDidLoad方法中添加如下代码:code

1
2
3
4
5
6
7
8
9
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;

 

效果图:blog

2.label行间距等参数修改ci

相关文章
相关标签/搜索