您的位置:首页 > 其它

label显示不同大小、颜色的字符串

2016-04-09 13:57 369 查看
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(20,
400, 200,
30)];

 
  //定义字符串
NSMutableAttributedString类型可以方便改变特定的字符

 
  NSMutableAttributedString *attributeString =
[[NSMutableAttributedString alloc]
initWithString:@"This is
xiaoming"];

 
  //前2个文字是红色

    

   

   
[attributeString addAttributes:@{NSForegroundColorAttributeName:[UIColor
redColor],NSFontAttributeName:[UIFont systemFontOfSize:30]}
range:NSMakeRange(0,
1)];

    

   
[attributeString addAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]}
range:NSMakeRange(2,
10)];

 
  //设置属性文字

    l.attributedText = attributeString;

    [self.view addSubview:l];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: