ios - UITextView 中的 AttributedString 在顶部被修剪

标签 ios swift uilabel uitextview nsattributedstring

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

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

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

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

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

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

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

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

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

最佳答案

使用后解决

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

关于ios - UITextView 中的 AttributedString 在顶部被修剪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56613581/

相关文章:

iOS 应用程序 - 查找(未知)私有(private) api 调用

ios - 图像未设置为 UIImageView 的嵌套 UIViewController

ios - 在 BarButtonItem Swift 上添加角标(Badge)计数图标

ios - UILabel 文本不会使用自动布局自动调整大小

swift - 设置文本以适合 UILabel 的大小并在 SWIFT 中居中

ios - 我们可以以编程方式注销Facebook吗

objective-c - 在 Objective-c 中使用调度队列

ios - iOS 上的 PJSIP,编译器错误 "undeclared type ` pj_thread_t`"

arrays - 如何快速检查一个值在嵌套字典中

ios - 如何根据 UITableView 中的文本增加 UILabel 高度?