【发布时间】:2013-04-03 12:13:26
【问题描述】:
如何增加 UITextView 中的行间距?
我希望使用默认系统字体,即尺寸为 15 的 Helvetica。
【问题讨论】:
-
所以简而言之,我不能像其他主题建议的那样去做。
-
你绝对可以做到这一点。请接受答案或提交您的解决方案。
标签: ios xcode ios6 xcode4 uitextview
如何增加 UITextView 中的行间距?
我希望使用默认系统字体,即尺寸为 15 的 Helvetica。
【问题讨论】:
标签: ios xcode ios6 xcode4 uitextview
声明你通过添加实现协议
<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 可以是UITextField、UILabel 或UITextView。
【讨论】:
在 IOS6+ 中,您可以设置 UITextView 的打字属性
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpacing];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
[textView setTypingAttributes:attrsDictionary];
【讨论】: