100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > ios label文字行间距_iOS- 设置label的行间距字体间距

ios label文字行间距_iOS- 设置label的行间距字体间距

时间:2018-10-27 12:58:00

相关推荐

ios label文字行间距_iOS- 设置label的行间距字体间距

- (void)viewDidLoad {

[super viewDidLoad];

NSString *content = @"好吗 一句话就哽住了喉 城市当背景的海市蜃楼 我们像分隔成一整个宇宙 再见都化作乌有 我们说好决不放开相互牵的手 可现实说过有爱还不够走到分岔的路口 你向左我向右 我们都倔强地不曾回头 我们说好就算分开一样做朋友 时间说我们从此不可能再问候 人群中再次邂逅";

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]; // 设置字体背景色

NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:content attributes:attDic];

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

style.lineSpacing = 20; // 设置行之间的间距

[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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。