在UITextView中设置行高 [英] Set line height in UITextView

查看:540
本文介绍了在UITextView中设置行高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经很确定它不能使用任何公共API,但我仍然想问:



有什么办法在UITextView中更改行高?



足以静态地执行,无需在运行时更改它。问题是,默认行高只是WAY太小。



感谢,
最大



编辑:我知道有 UIWebView ,它很好,可以做样式等。但它是不可编辑。我需要一个可接受的行高的可编辑文本组件。来自Omni框架的东西也没有帮助,因为它太慢了,感觉不对劲。

解决方案

在iOS 7之后,styleString方法不再工作。



有两种新的选择。



首先,TextKit;一个强大的新布局引擎。要更改行间距,请设置UITextView的布局管理器委托:

  textView.layoutManager.delegate = self; //你需要声明你实现NSLayoutManagerDelegate协议

然后重写这个委托方法: p>

   - (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect 
{
return 20; //真的宽间距选择您自己的值
}






,iOS 7现在支持NSParagraphStyle的lineSpacing。这给予更多的控制,例如。第一行缩进,以及边界矩形的计算。所以或者...

  NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
paragraphStyle.headIndent = 15; //< --- indention if you need it
paragraphStyle.firstLineHeadIndent = 15;

paragraphStyle.lineSpacing = 7; //< --- magic line spacing here!

NSDictionary * attrsDictionary =
@ {NSParagraphStyleAttributeName:paragraphStyle}; //< - 有更多的attrs,例如NSFontAttributeName

self.textView.attributedText = [[NSAttributedString alloc] initWithString:@Hello World over many lines!属性:attrsDictionary];






FWIW,旧的contentInset方法沿着UITextView的左边缘也没有在iOS7下使用。而是删除边距:

  textView.textContainer.lineFragmentPadding = 0; 


I'm already pretty sure that it can't be done with any public API, but I still want to ask:

Is there any way to change the line height in a UITextView?

Would be enough to do it statically, no need to change it at runtime. The problem is that the default line height is just WAY too small. Text will look extremely compressed and is a nightmare when trying to write longer texts.

thanks, Max

EDIT: I know that there is UIWebView and that it's nice and can do styling etc. But it's not editable. I need a editable text component with acceptable line height. That thing from the Omni Frameworks doesn't help either, as it's too slow and doesn't feel right...

解决方案

After iOS 7, the styleString approach no longer works.

Two new alternatives are available.

Firstly, TextKit; a powerful new layout engine. To change line spacing, set the UITextView's layout manager's delegate:

textView.layoutManager.delegate = self; // you'll need to declare you implement the NSLayoutManagerDelegate protocol

Then override this delegate method:

- (CGFloat)layoutManager:(NSLayoutManager *)layoutManager lineSpacingAfterGlyphAtIndex:(NSUInteger)glyphIndex withProposedLineFragmentRect:(CGRect)rect
{
    return 20; // For really wide spacing; pick your own value
}


Secondly, iOS 7 now supports NSParagraphStyle's lineSpacing. This gives even more control, e.g. first line indentation, and calculation of a bounding rect. So alternatively...

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.headIndent = 15; // <--- indention if you need it
paragraphStyle.firstLineHeadIndent = 15;

paragraphStyle.lineSpacing = 7; // <--- magic line spacing here!

NSDictionary *attrsDictionary =
@{ NSParagraphStyleAttributeName: paragraphStyle }; // <-- there are many more attrs, e.g NSFontAttributeName

self.textView.attributedText = [[NSAttributedString alloc] initWithString:@"Hello World over many lines!" attributes:attrsDictionary];


FWIW, the old contentInset method to align the text along the left edge of UITextView is also no use under iOS7. Instead, to remove the margin:

textView.textContainer.lineFragmentPadding = 0;

这篇关于在UITextView中设置行高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆