【问题标题】:How to increase spacing between lines in UITextView如何增加 UITextView 中的行间距
【发布时间】:2013-04-03 12:13:26
【问题描述】:

如何增加 UITextView 中的行间距?

我希望使用默认系统字体,即尺寸为 15 的 Helvetica。

【问题讨论】:

标签: ios xcode ios6 xcode4 uitextview


【解决方案1】:

声明你通过添加实现协议

<NSLayoutManagerDelegate>

到您的界面。然后,设置:

yourTextView.layoutManager.delegate = self;

然后重写这个委托方法:

- (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect
{
    return 5; // Line spacing of 19 is roughly equivalent to 5 here.
}

更新: 我最近发现这也可以使用NSMutableParagraphStyle setLineSpacing: API 来完成。

为了始终提供有用的复制粘贴代码-sn-p 超赞,开始吧!

NSMutableParagraphStyle *myStyle = [[NSMutableParagraphStyle alloc] init];
[myStyle setLineSpacing:myLineSpacingInt];
[myString addAttribute:myDesiredAttribute value:myStyle range:myDesiredRange];
[myViewElement setAttributedText:myString];

^myViewElement 可以是UITextFieldUILabelUITextView

【讨论】:

    【解决方案2】:

    在 IOS6+ 中,您可以设置 UITextView 的打字属性

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpacing];
    
    NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
    
    [textView setTypingAttributes:attrsDictionary];
    

    【讨论】:

      猜你喜欢
      • 2011-02-01
      • 2020-04-04
      • 2016-09-10
      • 2014-01-02
      • 1970-01-01
      • 1970-01-01
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多