微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐