'Align the image and text in NSAttributed text

I need to align the image and text horizontally and assign that string to a UILabel. Below is my code:

let attachment = NSTextAttachment()
let text = "Current user."
attachment.image = UIImage(named: "icon-horizontal-line")                        
let attachmentString = NSAttributedString(attachment: attachment)
let mutableAttributedString = NSMutableAttributedString()
mutableAttributedString.append(attachmentString)                        
let string = NSMutableAttributedString(string: text, attributes: [:])
mutableAttributedString.append(string)
label.attributedText = mutableAttributedString

And my result looks like this :

app_screenshot

I want both the image and string to be aligned something similar to : ----- Current User.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source