I have a text label containing dynamic text inside of a scrollview. A few lines of text are being cut off from the bottom of the label. I've set everything up in Storyboard using auto layout.
I've tried toggling isScrollingEnabled in ViewDidLoad on the UIScrollView per another post to no avail.
I've also tried removing the bottom constraint on the text label and padding it
I've narrowed down the issue by process of elimination to an extension I'm using to change the font in the HTML as I'm working with an html attributed string. I can't figure out why this is cutting the text off, sometimes just a couple lines, sometimes the majority of the content, and sometimes all of the content is missing
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 Hierarchy:
UITextLabel Constraints (Superview being the UIScrollView):
UPDATED IMAGES:
View of screen before scrolling:
View of screen when reaching bottom of label:
A scrollview needs to have a definitive content size. To do that, its subviews should have width/height (at least one of them).
You can set width of you UILabel to a screen width (minus whatever margin you want). And set its height to any default value, set height constraint priority to 950. Then set UILabel's vertical content hugging and compression priority to 1000.
I've never used storyboard so I couldn't tell you what boxes to check but you don't need to explicitly set any heights in a scroll view if you're using autolayout correctly (unless of course if you have views that need heights)—not the content size, not dynamic labels, nothing.
As long as the UILabel
(or whatever is the bottom-most view in the scroll view) is anchored to the bottom of the scroll view, you're fine.
someLabel.bottomAnchor.constraint(equalTo: scrollView.bottomAnchor).isActive = true
If you've also properly anchored the top-most view to the top of the scroll view, autolayout will handle the content size for you (there are more rules but this is the general premise). As for the label, let autolayout size that for you as well.
someLabel.text = someLongString
someLabel.numberOfLines = 0
someLabel.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(someLabel)
someLabel.leadingAnchor.constraint(equalTo: scrollView.leadingAnchor, constant: 16).isActive = true
someLabel.topAnchor.constraint(equalTo: someView.bottomAnchor, constant: 32).isActive = true
someLabel.widthAnchor.constraint(equalTo: view.widthAnchor, constant: -32).isActive = true
someLabel.sizeToFit()
You need not do anything more than this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.