label的常见用法(替换,删除线,不同颜色等)

1、label的字符替换。用@"2"替换@"1"

NSString *str= [String ByReplacingOccurrencesOfString:@"1"withString:@"2"];


2、label的删除线

[attributedString addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, 10)];

这样可以加中划线,不用自定义lable,直接改字符串属性就行了


3、将一个数组中的数据用字符串拼接

NSString * imagepic = [strArray componentsJoinedByString:@“,”];//将一个数组中的字符串,以逗号形式隔开


4、一个label设置多种颜色

(1)第一种方法

NSMutableAttributedString*str = [[NSMutableAttributedStringalloc]initWithString:@"合计:¥456.00"];

//设置:在0-3个单位长度内的内容显示成红色

[str addAttribute:NSForegroundColorAttributeNamevalue:[UIColor blackColor]range:NSMakeRange(0, 5)];

priceLabel.attributedText= str;


效果图(1)

(2)第二种方法

NSMutableAttributedString*noteStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"总共公开了%@条文书-符合搜索条件的共有%@条文书",allRemain,searchRemain]];

NSRangeredRange =NSMakeRange([[noteStr string]rangeOfString:allRemain].location, [[noteStr string]rangeOfString:allRemain].length);

[note StraddAttribute:NSForegroundColorAttributeNamevalue:[UIColor redColor]range:redRange];

searchCell.remainLabel.attributedText= noteStr;


效果图(2)


5、当label超出多少范围之后用...替换

if(cell.labelOne.text.length> 35) {

NSString* strText = [cell.labelOne.textsubstringToIndex:35];

cell.labelOne.text= [NSStringstringWithFormat:@"%@...",strText];

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容