ios label文字行间距_iOS- 设置label的行间距字体间距
- (void)viewDidLoad {
[super viewDidLoad];
NSString *content = @"好嗎 一句話就哽住了喉 城市當(dāng)背景的海市蜃樓 我們像分隔成一整個宇宙 再見都化作烏有 我們說好決不放開相互牽的手 可現(xiàn)實說過有愛還不夠走到分岔的路口 你向左我向右 我們都倔強地不曾回頭 我們說好就算分開一樣做朋友 時間說我們從此不可能再問候 人群中再次邂逅";
NSMutableDictionary *attDic = [NSMutableDictionary dictionary];
[attDic setValue:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName]; // 字體大小
[attDic setValue:[UIColor redColor] forKey:NSForegroundColorAttributeName]; // 字體顏色
[attDic setValue:@5 forKey:NSKernAttributeName]; // 字間距
[attDic setValue:[UIColor cyanColor] forKey:NSBackgroundColorAttributeName]; // 設(shè)置字體背景色
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:content attributes:attDic];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.lineSpacing = 20; // 設(shè)置行之間的間距
[attStr addAttribute:NSParagraphStyleAttributeName value:style range: NSMakeRange(0, content.length)];
CGFloat contentH = [attStr boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height; // 自動計算文本高度
UILabel *txtLbl = [[UILabel alloc] init];
txtLbl.frame = CGRectMake(100, 100, 200, contentH);
txtLbl.numberOfLines = 0;
txtLbl.attributedText = attStr;
txtLbl.backgroundColor = [UIColor redColor];
[self.view addSubview:txtLbl];
}
效果圖:
img.png
總結(jié)
以上是生活随笔為你收集整理的ios label文字行间距_iOS- 设置label的行间距字体间距的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java笔记-解决读取文件时中文乱码问题
- 下一篇: Python笔记-CAPM(资本资产定价