I have a NSMutableAttributedString string and a NSTextAttachment() image.
The text displays at the bottom of the image. Is there a way to get the text centered in the image?
let fullString = NSMutableAttributedString(string: "Acceptance")
let image1 = NSTextAttachment()
image1.image = UIImage(named: "pos")
image1.setImageHeight(height:30)
let image1String = NSAttributedString(attachment: image1)
fullString.append(image1String)
I do have a NSTextAttachment extension for scaling the image down and plays no part in why the text isn't centered
extension NSTextAttachment {
func setImageHeight(height: CGFloat) {
guard let image = image else { return }
let ratio = image.size.width / image.size.height
bounds = CGRect(x: bounds.origin.x, y: bounds.origin.y, width: ratio * height, height: height)
}
}
This is what it looks like
EDIT: I got it figured out. I will include this because it m ay help someone else out in the future.
image1.bounds = CGRect(x: 0, y: -10, width: 35, height: 35)