6

我一直在网上寻找信息/例子......

我正在尝试将 UITextView 对象内的文本行距更改为双倍行距。我以为你可以通过 Core Text 做到这一点,但还没有找到解决方案!

任何示例代码或信息将不胜感激!谢谢!

4

3 回答 3

5

You don't have to look all over the net. A look at the documentation for UITextView is sufficient to determine that changing the line spacing is not supported by that control.

With Core Text you have complete control over the layout of the text you draw, of course. But it would be a lot of work to rewrite a UITextView-like control from scratch.

于 2010-09-29T19:28:50.220 回答
5

这个问题是在 iOS 6 之前提出的。我想为那些想要这样做的人发布一个更新的答案。

现在可以在 iOS 6 及更高版本中使用 NSAttributedString 来完成此操作。UITextView 现在接受属性字符串作为其属性之一。您可以使用属性字符串进行各种文本操作,包括行距。您可以为字符串的段落样式属性设置最小和最大行高。

查看NSAttributedString 类参考了解更多信息。

这是您可以执行的操作的示例:

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.minimumLineHeight = 21.0f;
paragraph.maximumLineHeight = 21.0f;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Test text line 1\nTest text line 2\nTest text line 3" attributes:@{NSParagraphStyleAttributeName: paragraph}];
textView.attributedText = attributedString;
于 2014-02-16T06:12:50.673 回答
3

您可以使用 NSLayoutManagerDelegate。将该委托添加到您的 ViewController 或 UIView 类(等),然后在您创建 UITextView 时...

yourTextView.layoutManager.delegate = self

添加此委托方法:

func layoutManager(layoutManager: NSLayoutManager, lineSpacingAfterGlyphAtIndex glyphIndex: Int, withProposedLineFragmentRect rect: CGRect) -> CGFloat {
    return 5 //Whatever you'd like...
}
于 2016-02-03T15:20:28.793 回答