xcode 如何增加 UITextView 中的行间距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15787057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:07:18  来源:igfitidea点击:

How to increase spacing between lines in UITextView

iosxcodeios6xcode4uitextview

提问by nitz19arg

How to increase spacing between lines in UITextView?

如何增加 UITextView 中的行间距?

I wish to use default system font,i.e., Helvetica with size 15.

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

回答by jungledev

Declare you implement the protocol by adding

通过添加声明您实现了协议

<NSLayoutManagerDelegate>

<NSLayoutManagerDelegate>

to your interface. Then, set:

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

yourTextView.layoutManager.delegate = self;

yourTextView.layoutManager.delegate = self;

Then override this delegate method:

然后覆盖这个委托方法:

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

UPDATE: I recently discovered that this can also be done using the NSMutableParagraphStyle setLineSpacing:API.

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

In an effort to always provide useful copy-paste code-snippet awesomeness, here you go!

为了始终提供有用的复制粘贴代码片段,非常棒,给你!

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

^myViewElementcan be a UITextField, UILabel, or UITextView.

^myViewElement可以是UITextField, UILabel, 或UITextView

回答by Rami

In IOS6+ you can set the typing attributes for a UITextView

在 IOS6+ 中,您可以为 UITextView 设置输入属性

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpacing];

NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];

[textView setTypingAttributes:attrsDictionary];