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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值