I need to reduce the text size in order to fit a specific area width. The text contains some different format settings (mainly text color) so I am using NSMutableAttributedString
Then I set adjustsFontSizeToFitWidth for fitting with the bounding rectangle.
testLabel.minimumScaleFactor = 0.1
testLabel.adjustsFontSizeToFitWidth = true
let attributedGreen = NSAttributedString(string: "GREEN", attributes: [NSAttributedString.Key.foregroundColor: UIColor.green])
let attributedYellow = NSAttributedString(string: "YELLOW", attributes: [NSAttributedString.Key.foregroundColor: UIColor.yellow])
let textCombination = NSMutableAttributedString()
textCombination.append(attributedGreen)
textCombination.append(attributedYellow)
textCombination.append(attributedGreen)
textCombination.append(attributedYellow)
testLabel.attributedText = textCombination
The expected result is to have a full visible text and re-scaled to the right font size but it does not happen and the text is truncated.
Everything is working fine if I do not use the attributed string:
testLabel.minimumScaleFactor = 0.1
testLabel.adjustsFontSizeToFitWidth = true
testLabel.text = "GREENYELLOWGREENYELLOW"
Is that the right way for doing it? How can I properly re-scale the font size?
Extra note: I've just tested it on a device running iOS 13 (instead of iOS 14) and font re-scale with attributed string is working fine.
Could it be an iOS 14 bug? Any workarounds to suggest?
UPDATE 2020/10/26: I've just tested it using iOS 14.0.1 and the issue is still present.