我在 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/