     /// Otherwise, we'll always take the whole container's width.
     ///
     override open func attachmentBounds(for textContainer: NSTextContainer?, proposedLineFragment lineFrag: CGRect, glyphPosition position: CGPoint, characterIndex charIndex: Int) -> CGRect {
-        
+
         ensureImageIsUpToDate(in: textContainer)
-        
+
         if image == nil {
             return .zero
         }
 
-        var padding = (textContainer?.lineFragmentPadding ?? 0)
+        var padding = (textContainer?.lineFragmentPadding ?? 0) * 2
         if let storage = textContainer?.layoutManager?.textStorage,
-           let paragraphStyle = storage.attribute(NSParagraphStyleAttributeName, at: charIndex, effectiveRange: nil) as? NSParagraphStyle {
-            padding += paragraphStyle.firstLineHeadIndent + paragraphStyle.tailIndent
-        }
-        let width = floor(lineFrag.width - (padding * 2))
+            let paragraphStyle = storage.attribute(NSParagraphStyleAttributeName, at: charIndex, effectiveRange: nil) as? NSParagraphStyle {
+            let attachmentString = storage.attributedSubstring(from: NSMakeRange(charIndex, 1)).string
+            let headIndent = storage.string.isStartOfParagraph(at: attachmentString.startIndex) ? paragraphStyle.firstLineHeadIndent : paragraphStyle.headIndent
+
+            padding += headIndent
+
+            if paragraphStyle.tailIndent > 0 {
+                padding += lineFrag.width - headIndent - paragraphStyle.tailIndent
+            } else if paragraphStyle.tailIndent < 0 {
+                padding -= paragraphStyle.tailIndent
+            }
+        }
+        let width = floor(lineFrag.width - padding)
 
         let size = CGSize(width: width, height: onScreenHeight(width))
-        
+
         return CGRect(origin: CGPoint.zero, size: size)
     }
 }

    /// Identifier used to match this attachment with a custom UIView subclass
    ///
    private(set) open var identifier = String()
    
    /// Attachment URL
    ///
    fileprivate(set) public var url: URL?

    // MARK: - NSTextAttachmentContainer

    override open func image(forBounds imageBounds: CGRect, textContainer: NSTextContainer?, characterIndex charIndex: Int) -> UIImage? {

        ensureImageIsUpToDate(in: textContainer)

        guard let image = image else {
