美文网首页iOS进阶之路iOS开发iOS技术资料
修改label的字间距 行间距 字体颜色 下划线 自动换行

修改label的字间距 行间距 字体颜色 下划线 自动换行

作者: 幻影道哥 | 来源:发表于2017-06-12 10:49 被阅读57次

label大家玩的都比较多了.下面我就分享一下我用到的比较实用的几个例子.

1.自动换行

UILabel * WithdrawalRules = [[UILabel alloc] init];

指定numberOfLines =0  然后在 字符串里需要换行的地方 加\n就可以了.

WithdrawalRules.numberOfLines = 0;

WithdrawalRules.text  =@"提现规则:\n1、当天提现最多不可超过3次;\n2、每次提现金额不低于10元,不高于100元。" ;

2.修改label的字间距

NSString *str = @"亲爱的手环用户!若是您的手环刷卡体验不佳,建议尝试更换手环参数模式,此参数目前仅在南京地区有效。";

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSKernAttributeName:@(1.0)}];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [str length])];

alabel.attributedText = attributedString;

3.修改label的行间距

-(NSAttributedString *)getAttributedStringWithString:(NSString *)string lineSpace:(CGFloat)lineSpace {

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

paragraphStyle.lineSpacing = lineSpace; // 调整行间距

NSRange range = NSMakeRange(0, [string length]);

[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];

return attributedString;

}

4.添加下划线

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"点击购买手环"];

NSRange strRange = {0,[str length]};

[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];

5.字体的颜色

[str addAttribute:NSForegroundColorAttributeName value:rgbaColor(220, 221, 217, 1) range:NSMakeRange(0, str.length)];

相关文章

网友评论

    本文标题:修改label的字间距 行间距 字体颜色 下划线 自动换行

    本文链接:https://www.haomeiwen.com/subject/sjxlqxtx.html