美文网首页
UILabel增加行间距

UILabel增加行间距

作者: mnz12138 | 来源:发表于2017-09-22 11:12 被阅读0次
        CGFloat width = self.view.bounds.size.width;
        NSDictionary *attr = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
        NSMutableAttributedString *mAttrStr = [[NSMutableAttributedString alloc] initWithString:@"我是有行间距的啊!\n我是有行间距的啊!"];
        NSMutableAttributedString *testAttrStr = [[NSMutableAttributedString alloc] initWithString:@"测试"];
        [testAttrStr addAttributes:attr range:NSMakeRange(0, testAttrStr.length)];
        CGFloat oneHeight = [testAttrStr boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
        CGFloat rowHeight = [mAttrStr boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size.height;
        // 如果超出一行,则offset=0;
        if(rowHeight > oneHeight){
            NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
            paragraphStyle.lineSpacing = 10;
            [mAttrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, mAttrStr.length)];
        }
        self.label.attributedText = [mAttrStr copy];
    

    可以看一下我写的分类WQJTool
    支持pod导入项目

    相关文章

      网友评论

          本文标题:UILabel增加行间距

          本文链接:https://www.haomeiwen.com/subject/iioxextx.html