Post not yet marked as solved
Post marked as unsolved with 2 replies, 3,311 views
Up until iOS 15 it was possible to set maximum number of lines of the UIButton titleLabel property, but when UIButton.Configuration is used to configure button's properties setting the titleLabel?.numberOfLines to any value is always ignored and reset after assigning text to configuration.
See the short piece of code below:
import UIKit
let button = UIButton(configuration: .plain())
button.titleLabel?.numberOfLines = 1
print("Lines: \(button.titleLabel?.numberOfLines ?? 999)") // <- prints "Lines: 1"
var buttonConfiguration = button.configuration
buttonConfiguration?.title = "Any text"
button.configuration = buttonConfiguration
print("Lines: \(button.titleLabel?.numberOfLines ?? 999)") // <- prints "Lines: 0"
Even if you set the numberOfLines to 1 it is ignored and the button renders one, or more lines of text.
I think it's a bug, there is no compiler warning about accessing the titleLabel property, as there is with imageEdgeInsets for example. If you access that one Xcode gives you a warning
'imageEdgeInsets' was deprecated in iOS 15.0: This property is ignored when using UIButtonConfiguration
If modifying titleLabel directly there should be a warning as well, but if that's the case, the UIButton.Configuration is seriously lacking some features, like setting maximum number of lines, or even a font (this can be done via AttributedString but setting font is so common that constructing AttrbitutedString every time the title is changed feels so wrong and unnecessary)
For the time being I'm forced to use the legacy button customisation