iOS 在UILabel显示不同的字体和颜色

来源:互联网 发布:帝势艺术学院知乎 编辑:程序博客网 时间:2024/05/20 02:22

iOS 在UILabel显示不同的字体和颜色 使用的控件是 NSMutableAttributedString  解决了创建多个label的笨方法 ,下面就简单介绍一下NSMutableAttributedString 的使用

NSMutableAttributedString*str = [[NSMutableAttributedStringalloc] initWithString:@"Using NSAttributed String"];
[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColor blueColor] range:NSMakeRange(0,5)];
[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColor redColor] range:NSMakeRange(6,12)];
[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColor greenColor] range:NSMakeRange(19,6)];
[str addAttribute:NSFontAttributeNamevalue:[UIFont fontWithName:@"Arial-BoldItalicMT"size:30.0] range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeNamevalue:[UIFont fontWithName:@"HelveticaNeue-Bold"size:30.0] range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeNamevalue:[UIFont fontWithName:@"Courier-BoldOblique"size:30.0] range:NSMakeRange(19, 6)];
attrLabel.attributedText = str;


效果图:


0 0
原创粉丝点击