UILable字符串的不同字段的样式设…

来源:互联网 发布:数控车t型螺纹编程实例 编辑:程序博客网 时间:2024/06/08 13:34
在使用APP搜索功能时用户输入一个字符,UI会把包含该字符的文本对象罗列出来,用户输入的字符串则用红色的颜色显示出来,显得特别鲜明。以下是关于一个字符串的不断范围的文字设置样式的一个方法。主要是使用到NSMutableAttributedString这个类。下面是代码:

UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100,100, 120, 50)];
   NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:@"123456"];
   
   //修改字体的颜色
    [straddAttribute:NSForegroundColorAttributeName value:[UIColorredColor] range:NSMakeRange(0,2)];
    [straddAttribute:NSBackgroundColorAttributeName value:[UIColorredColor] range:NSMakeRange(2,4)];
   
   //修改字体样式
    [straddAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25]range:NSMakeRange(0,6)];
   lable.attributedText=str;
   lable.lineBreakMode=NSLineBreakByTruncatingTail;
    [self.viewaddSubview:lable];
0 0