Fixed an issue where button's setTitle(string:state:) did not respond to a title change.#9
Fixed an issue where button's setTitle(string:state:) did not respond to a title change.#9mixdesign wants to merge 2 commits intopmusolino:masterfrom
Conversation
… to a title change. The cause of the problem was the getAttributedString() method, which always returned the value .attributedText and ignored the value of .text;
|
I forgot to mension override method in PMSuperButton, which is self explanatory: override open func setTitle(_ title: String?, for state: UIControlState) {
// setTitle always sets attributed text
self.setAttributedTitle(getAttributedString(with: title, letterSpacing: letterSpacing), for: state)
}What do you think, is it ok always setting the attributed string for |
|
Ohh thanks for this fix. What happens if the user wants to set a custom attributed title? Did he lose all the changes? |
|
I tested your solution, but I think that this can be better: This solution works on all cases that I tested and is more simple and elegant + in future can be added more IBInspectable variables that need use the setAttributedString override method. What do you think? |
|
In reality, this fixes not manage all the cases, for example, the color change and other adjustments on UIButton. I think we need to remove the letter spacing feature, at least for now. |
|
@codeido, I think we can solve this problem. Let's continue the discussion until we find the best solution. I will try to prepare another solution till the end of this week. |
|
Excellent @mixdesign, consider that everything that impacts the text needs to be "converted" in a NSAttributedString with this approach. |
|
Hey, @codeido It works well with various combinations of programmatically calling setTitle, setAttributedTitle, setTitleColor. I'm still testing it on my real project, just keep an eye by now. |
Hi, @codeido
Thanks for accepting my PR.
We have a problem in previous PR's implementation 😶:
Everythings works OK if the developer configures the button only through the storyboard.
But, if there is a need to change the title of the button programmatically, then a problem arises:
I fixed the issue.
Now we have a flexible function:
// Get the NSMutableAttributedString with kern value: optional("string") ?? ""; // Button's previous attributedText is kept, only mutableString value is changed. private func getAttributedString(with string:String?, letterSpacing spacing:CGFloat) -> NSMutableAttributedString { var attr:NSMutableAttributedString! if let string = string, let attrText = self.titleLabel?.attributedText { let attrString = NSMutableAttributedString(attributedString:attrText) attrString.mutableString.setString(string) attr = attrString } else { attr = NSMutableAttributedString(string:string ?? "") } attr.addAttribute(NSAttributedStringKey.kern, value: spacing, range: NSRange(location: 0, length: attr.length)) return attr }In the future PR with another attributed string features, we may create another common private function which will return
titleLabel'sNSMutableAttributedString with a single string parameter.Thanks!