改变UIView行间距

背景

开发的过程中,我们会遇到诸如此类的需求(增加或者减小行间距的大小)。这时我们就纠结用UIVie w能否完成呢?答案是可以的。并且还是很简单的。


改变UIView行间距_第1张图片
奋斗的郅博

具体实现步骤

UITextView *textView = [[UITextView alloc] init];
NSString *upgradeDescription = @"你知道我在等你吗?你肯定不知道吧!哈哈哈哈哈哈哈哈哈哈";
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//行间距为9
paragraphStyle.lineSpacing = 9;
 NSRange range = NSMakeRange(0, upgradeDescription.length);
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:upgradeDescription];
    [attrStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    [attrStr addAttribute:NSForegroundColorAttributeName value: [UIColor RedColor] range:range];
    [attrStr addAttribute:NSFontAttributeName value:updateTextFont range:range];
textView.attributedText = attrStr;
[self.view addSubview: textView];

结论:
正如上所述,就是这么简单主要就用NSMutableParagraphStyle的lineSpacing属性就解决了这个问题了。

你可能感兴趣的:(改变UIView行间距)