ios – UITextView lineHeightMultiple Clips Top,第一行,Text

在iOS 8中,我有一个vanilla UITextView,当lineHeightMultiple应用于它的NSMutableParagraphStyle时,它会剪切第一行的顶部,见下图:

看起来,除了后续行之外,lineHeightMultiple还会影响第1行文本.

在UITextView上设置clipsToBounds = false至少会使剪裁的部分显示出来,但是你可以从下图中看到,现在文本的顶部显然位于它的框架上方:

我可以通过在有问题的UITextView上设置顶部约束来补偿clipsToBounds = false来解决这个问题,但这对我来说就像是一个黑客攻击.

我也尝试使用WKWebView来处理有问题的文本,只是设置了css line-height属性,这样就可以了.但是,在UITextView方面,我必须简单地找到一些东西.

此外,根据文档,将段落样式的linespacing设置为小于0没有任何影响:

The distance in points between the bottom of one line fragment 
and the top of the next.

**This value is always nonnegative.**

This value is included in the line fragment heights in the 
layout manager.

我也尝试过设置UITextView的contentInset以及使用系统字体,两者都没有影响.

我的示例代码如下:

let text = "THIS IS A MULTILINE RUN OF TEXT"

let font = UIFont(name: "MyFontName",size: 31.0)!
// let font = UIFont.systemFontOfSize(31.0)

let paragraph = NSMutableParagraphStyle()
paragraph.lineHeightMultiple = 0.75
paragraph.alignment = NSTextAlignment.Center

let attributes = [
    NSParagraphStyleAttributeName: paragraph,NSFontAttributeName: font
]

titleView.attributedText = NSAttributedString(string: text,attributes: attributes)

// titleView.contentInset = UIEdgeInsets(top: 50.0,left: 0.0,bottom: 0.0,right: 0.0)
titleView.clipsToBounds = false

有没有人遇到过这个并且克服或者是最重要的约束黑客唯一的出路?

解决方法

我遇到过同样的问题.

我使用NSBaselineOffsetAttributeName补偿它.

你应该使用:

let attributes = [
    NSParagraphStyleAttributeName: paragraph,NSFontAttributeName: font,NSBaselineOffsetAttributeName: -5
]

您还必须设置较低的paragraph.lineHeightMultiple.

有点棘手,但它的确有效.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...