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

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)];

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

推荐阅读更多精彩内容