I’m experiencing an issue with ActiveLabel, a custom UILabel used for handling rich text interactions such as URLs, mentions, and hashtags. When I apply different font styles (specifically switching some text to bold) within a single line of text, the height of the label unexpectedly doubles.
Details:
Framework: ActiveLabel
Font settings: The default text uses UIFont.systemFont(ofSize: 15), and for specific patterns like URLs, I apply UIFont.boldSystemFont(ofSize: 15).
Configuration: I use label.numberOfLines = 0 and customize link attributes to apply the bold font to certain text types.
Issue: The issue occurs only when there is a single line of text; if the label wraps to more than one line, the height appears normal. It seems like the addition of bold text increases the line height, which doesn’t reset even when the bold text is a minor part of the overall text.
let label = ActiveLabel()
label.font = UIFont.systemFont(ofSize: 15)
label.numberOfLines = 0
label.configureLinkAttribute = { type, attributes, isSelected in
var atts = attributes
if type == .mention || type == .hashtag {
atts[.font] = UIFont.boldSystemFont(ofSize: 15)
}
return atts
}
How can I prevent the label’s height from doubling when applying bold to only parts of a single line of text? Is there a way to enforce a consistent line height across different font styles within the same ActiveLabel?
PS: When I remove “.numberOfLines = 0”, the issue with the line height getting messed up goes away, but it’s not a solution as I need the label to have an unlimited number of lines.
hedi gharbi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1