UITextView lineHeightMultiple Clips 顶部,第一行,文本

2024-01-22

在 iOS 8 中,我有一个普通版本UITextView当 a 时,会剪辑第一行的顶部lineHeightMultiple被应用到它的NSMutableParagraphStyle,见下图:

看起来好像lineHeightMultiple除了后续行之外,还会影响文本的第一行。

Setting clipsToBounds = false on the UITextView至少会显示剪切的部分,但您可以从下图中看到,现在文本的顶部显然位于其框架上方:

我可以通过仅对违规者设置最高约束来解决此问题UITextView来补偿clipsToBounds = false但这对我来说就像是一种黑客攻击。

我也尝试过使用WKWebView对于有问题的文本,只需设置 cssline-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.

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

我的此设置的示例代码如下:

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。

有点棘手,但它有效。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UITextView lineHeightMultiple Clips 顶部,第一行,文本 的相关文章

随机推荐