I'm trying to achieve this on iOS using Swift and storyboards:
Notice that the image size on the right never changes, only the text font size changes depending on the user's default font size. I want the image to remain at the end of the label.
Does anyone have an idea how I can do it?
CodePudding user response:
The simplest is to create an extension on UILabel and append whatever image/icon to the end of the attributedText, something along these lines:
extension UILabel {
func appendIcon() {
let iconAttachment = NSTextAttachment()
iconAttachment.image = UIImage(systemName: "info.circle.fill")
let iconString = NSAttributedString(attachment: iconAttachment)
guard let attributedText = self.attributedText else { return }
let currentString = NSMutableAttributedString(attributedString: attributedText)
currentString.append(iconString)
self.attributedText = currentString
}
}
And then call that programmatically.
label.appendIcon() // will need to create outlet