在UITextView中设置行高

我已经很确定不能使用任何公共API来完成此操作,但是我仍然想问:

有什么办法可以改变UITextView中的行高吗?

足以静态地完成它,而无需在运行时进行更改。问题是默认的行高太小。文本看起来极度压缩,并且是尝试编写较长文本时的噩梦。

谢谢,马克斯

编辑:我知道有UIWebView而且很好,可以做样式等。但是它是不可编辑的。我需要具有可接受的行高的可编辑文本组件。Omni Frameworks提供的功能也无济于事,因为它太慢且感觉不正确...


呼唤远方
浏览 1369回答 3
3回答

智慧大石

在iOS 7之后,styleString方法不再起作用。有两种新的选择。首先是TextKit;强大的新版式引擎。要更改行距,请设置UITextView的布局管理器的委托:textView.layoutManager.delegate = self; // you'll need to declare you implement the NSLayoutManagerDelegate protocol然后重写此委托方法:- (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect{&nbsp; &nbsp; return 20; // For really wide spacing; pick your own value}其次,iOS 7现在支持NSParagraphStyle的lineSpacing。这样可以提供更多控制,例如第一行缩进和边界矩形的计算。所以或者...NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];paragraphStyle.headIndent = 15; // <--- indention if you need itparagraphStyle.firstLineHeadIndent = 15;paragraphStyle.lineSpacing = 7; // <--- magic line spacing here!NSDictionary *attrsDictionary =@{ NSParagraphStyleAttributeName: paragraphStyle }; // <-- there are many more attrs, e.g NSFontAttributeNameself.textView.attributedText = [[NSAttributedString alloc] initWithString:@"Hello World over many lines!" attributes:attrsDictionary];FWIW,在iOS7下也没有使用旧的contentInset方法来沿UITextView的左边缘对齐文本。相反,要删除边距:textView.textContainer.lineFragmentPadding = 0;

料青山看我应如是

仅当您在UITextView上定义了定义styleString的类别时,才可以使用styleString的UITextView子类重写,否则会出现编译错误。例如,在您的UITextView子类中:#import "SomeDangTextView.h"@interface UITextView ()- (id)styleString;@end@implementation SomeDangTextView- (id)styleString {&nbsp; &nbsp; return [[super styleString] stringByAppendingString:@"; line-height: 1.5em"];}@end
打开App,查看更多内容
随时随地看视频慕课网APP