我在 ScrollView 中有一个包含动态文本的文本标签。标签底部的几行文字被剪掉了。我已经使用自动布局在 Storyboard 中设置了所有内容。
我已尝试在另一篇文章的 UIScrollView 上的 ViewDidLoad 中切换 isScrollingEnabled,但无济于事。
我也试过移除文本标签的底部约束并填充它
我已经通过消除过程将问题缩小到一个扩展,我在使用 html 属性字符串时使用它来更改 HTML 中的字体。我不明白为什么这会切断文本,有时只是几行,有时是大部分内容,有时所有内容都丢失了
extension NSAttributedString {
func changeHTMLFont(_ text: NSAttributedString) -> NSAttributedString {
let newAttributedString = NSMutableAttributedString(attributedString: (text))
newAttributedString.enumerateAttribute(NSAttributedStringKey.font, in: NSMakeRange(0, newAttributedString.length), options: []) { value, range, stop in
guard let currentFont = value as? UIFont else {
return
}
//USE FOR FACE OPTIONS
let fontDescriptor = currentFont.fontDescriptor.addingAttributes([UIFontDescriptor.AttributeName.family: "Optima", UIFontDescriptor.AttributeName.face: "Bold"])
//let fontDescriptor = currentFont.fontDescriptor.addingAttributes([UIFontDescriptorFamilyAttribute: "Optima"])
if let newFontDescriptor = fontDescriptor.matchingFontDescriptors(withMandatoryKeys: [UIFontDescriptor.AttributeName.family]).first {
let newFont = UIFont(descriptor: newFontDescriptor, size: 32.0) //use size: currentFont.pointSize for default font size
newAttributedString.addAttributes([NSAttributedStringKey.font: newFont], range: range)
}
}
return newAttributedString
}
}
Storyboard层次结构:
UITextLabel 约束(Superview 是 UIScrollView):
更新图像:
滚动前的屏幕 View :
到达标签底部时的屏幕 View :
最佳答案
ScrollView 需要有明确的内容大小。为此,它的 subview 应该具有宽度/高度(至少其中之一)。
您可以将 UILabel 的宽度设置为屏幕宽度(减去您想要的任何边距)。并将其高度设置为任意默认值,将高度约束优先级设置为950。然后将UILabel的垂直内容拥抱和压缩优先级设置为1000。
关于ios - TextLabel 在 ScrollView 中被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47856875/