如何在UITextView中增加行间距?
我希望使用默认系统字体,即Helvetica,大小为15。
答案 0 :(得分:8)
声明通过添加
来实现协议 <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完成。
为了始终提供有用的复制粘贴代码段令人敬畏,请到这里去!
NSMutableParagraphStyle *myStyle = [[NSMutableParagraphStyle alloc] init];
[myStyle setLineSpacing:myLineSpacingInt];
[myString addAttribute:myDesiredAttribute value:myStyle range:myDesiredRange];
[myViewElement setAttributedText:myString];
^ myViewElement
可以是UITextField
,UILabel
或UITextView
。
答案 1 :(得分:5)
在IOS6 +中,您可以设置UITextView的输入属性
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:lineSpacing];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
[textView setTypingAttributes:attrsDictionary];