【问题标题】:AttributedString in UITextView is trimmed at the topUITextView 中的 AttributedString 在顶部被修剪
【发布时间】:2019-06-15 19:31:21
【问题描述】:

我在 UITextView 中使用属性字符串。示例游乐场可以找到here

这是对正在发生的事情的快速总结。

UITextView 有填充问题,可以使用this 解决,感谢@Fattie

我在 UITextView 中设置了属性字符串。

然后我使用 sizeThatFits 方法计算 textView 的高度,我猜它给了我正确的高度。

但是当我运行它时,顶部的文本被修剪,底部留下了一些空间。请从here运行示例游乐场

我想这可能是 textView 带有默认填充的原因,但我并不完全确定。

奇怪的是,我对 UILabel 进行了同样的尝试,但它似乎在相同的设置下工作得非常好。取消注释 UILabel 后,您可以在上面的操场上尝试相同的操作。

谁能告诉我为什么 UITextView 会出现这个问题?

【问题讨论】:

    标签: ios swift uilabel uitextview nsattributedstring


    【解决方案1】:

    使用后解决

    let htmlText = text.data(using: .utf8, allowLossyConversion: false)!
    let mutableText = try! NSAttributedString(
            data: htmlText,
            options: [.documentType: NSAttributedString.DocumentType.html],
            documentAttributes: nil)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-11
      • 1970-01-01
      • 2010-12-06
      • 2015-08-13
      • 2021-02-21
      • 1970-01-01
      相关资源
      最近更新 更多