Label字间距行间距
2018-01-19 本文已影响0人
温水煮青蛙a
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 100, 500)];
[self.view addSubview:label2];
NSString *labelText = @"哈哈\nqwer\n!!!aaaaaaaaaaaaaaaaaaaaaa";
label2.numberOfLines = 0; // 最关键的一句
NSMutableDictionary *attDic = [NSMutableDictionary dictionary];
// 字体大小
[attDic setValue:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
// 字体颜色
[attDic setValue:[UIColor redColor] forKey:NSForegroundColorAttributeName];
// 字间距
[attDic setValue:@5 forKey:NSKernAttributeName];
// 设置字体背景色
[attDic setValue:[UIColor cyanColor] forKey:NSBackgroundColorAttributeName];
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:labelText attributes:attDic];
// 设置行间距
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = 10;
[attStr addAttribute:NSParagraphStyleAttributeName value:style range: NSMakeRange(0, labelText.length)];
label2.attributedText = attStr;