ios - TextLabel 在 ScrollView 中被截断

标签 ios swift uiscrollview ios-autolayout textlabel

我在 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层次结构:

Storyboard Hierarchy

UITextLabel 约束(Superview 是 UIScrollView):

Text Label Constraints

更新图像:

滚动前的屏幕 View :

enter image description here

到达标签底部时的屏幕 View :

enter image description here

最佳答案

ScrollView 需要有明确的内容大小。为此,它的 subview 应该具有宽度/高度(至少其中之一)。

您可以将 UILabel 的宽度设置为屏幕宽度(减去您想要的任何边距)。并将其高度设置为任意默认值,将高度约束优先级设置为950。然后将UILabel的垂直内容拥抱和压缩优先级设置为1000。

enter image description here

enter image description here

关于ios - TextLabel 在 ScrollView 中被截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47856875/

相关文章:

ios - UITextView 自动换行异常行为

iphone - UIScrollView 顶部的菜单

ios - 如何在 Swift3 中使用分隔符连接 NSMutableArray 的对象

ios - AVPlayer View Controller 自动旋转关闭?

swift - 为什么每次我尝试让用户在 Swift 中输入整数时,Xcode 都会让我在变量后面加上 "!"

ios - 如何在inputAccessoryView上面设置PKHUD

ios - 根据内容调整 uiscrollview 的大小

ios - 如何在 ios 应用程序中检索 firebase 数据库中的数据?我的 JSON 数据如下所示,我想获取与问题文本 A...E 关联的每个值

ios - 专门的 Cell.init(编码器 :) Crash

ios - 当我第一次点击搜索栏时键盘会自动关闭