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

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

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

(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

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

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

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

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

}

添加删除线:

NSMutableAttributedString * discount = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@",discountStr] attributes:

@{NSFontAttributeName : [UIFont jk_systemFontOfPxSize:30],

NSForegroundColorAttributeName : JKSameRGBColor(153),

NSStrikethroughStyleAttributeName:@(NSUnderlineStyleDouble),

NSStrikethroughColorAttributeName : [UIColor yellowColor]

其中:NSStrikethroughStyleAttributeName 删除线

实际上,设置删除线 NSStrikethroughColorAttributeName 的时候,值也是这个枚举。

// NSUnderlineStyleNone 不设置下划线/删除线

// NSUnderlineStyleSingle 设置下划线/删除线为细的单线

// NSUnderlineStyleThick 设置下划线/删除线为粗的单线

// NSUnderlineStyleDouble 设置下划线/删除线为细的双线

// NSUnderlinePatternSolid 设置下划线/删除线样式为连续的实线

//NSUnderlinePatternDot 设置下划线/删除线样式为点,也就是虚线,比如这样:------

// NSUnderlinePatterDash 设置下划线/删除线样式为破折号,比如这样:—— —— ——

// NSUnderlinePatternDashDot 设置下划线/删除线样式为连续的破折号和点,比如这样:——-——-——-

// NSUnderlinePatternDashDotDot 设置下划线/删除线样式为连续的破折号、点、点,比如:——--——--——--

// NSUnderlineByWord 在有空格的地方不设置下划线/删除线

当 : ¥ 和 100 组合 时 ,删除线出现断层,不水平现象

当我用$符号时,发现没问题,所以我猜测是 中英文 结合问题

上面的是 直接 快捷键shift + 4 打得 符号,有断层

下面是 用 搜狗 打“RMB”选的符号,没有断层


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11588260.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存