Skip to content

Instantly share code, notes, and snippets.

@Ajimi
Created June 28, 2020 11:01
Show Gist options
  • Save Ajimi/d402d765750036a64405fae04fd3eb96 to your computer and use it in GitHub Desktop.
Save Ajimi/d402d765750036a64405fae04fd3eb96 to your computer and use it in GitHub Desktop.
// Create Attachment
let imageAttachment = NSTextAttachment()
imageAttachment.image = UIImage(named:"iPhoneIcon")
// Set bound to reposition
let imageOffsetY: CGFloat = -5.0
imageAttachment.bounds = CGRect(x: 0, y: imageOffsetY, width: imageAttachment.image!.size.width, height: imageAttachment.image!.size.height)
// Create string with attachment
let attachmentString = NSAttributedString(attachment: imageAttachment)
// Initialize mutable string
let completeText = NSMutableAttributedString(string: "")
// Add image to mutable string
completeText.append(attachmentString)
// Add your text to mutable string
let textAfterIcon = NSAttributedString(string: "Using attachment.bounds!")
completeText.append(textAfterIcon)
self.mobileLabel.textAlignment = .center
self.mobileLabel.attributedText = completeText
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment