I am following this cool tutorial Implementing Rich Text with Images on OS X and iOS by @Duncan Groenewald and was able to display images in my UITextView. However, these images are not centered they way I would like them to be. See image

As you can see, I would like my image to be centered on the X-axis.
I tried returning the rect with appropriate values in -attachmentBoundsForTextContainer:proposedLineFragment:glyphPosition:characterIndex but that did not help.
I also tried setting the NSKernAttributeName for the NSTextAttachment attributed string. But all it did was hide the image some how.
This is Swift 3.1 using extension:
In this way you can you easily apply alignment for attachments on attributed string: