             }
             origin.y += separatorFrame.height
             origin.y += captionSpacing
-            let captionFrame = captionTextView.wmf_preferredFrame(at: origin, fitting: widthMinusMargins, alignedBy: semanticContentAttribute, apply: true)
-            let insets: UIEdgeInsets = captionTextView.textContainerInset;
-            origin.y += insets.top + insets.bottom;
-            origin.y += captionFrame.layoutHeight(with: 0)
-        }
-    
-        origin.y += layoutMargins.bottom
+            // set width first to get proper content size
+            captionTextView.frame = CGRect(origin: origin, size: CGSize(width: widthMinusMargins, height: 32))
+            let captionTextViewSize = captionTextView.contentSize
+            let captionFrame = CGRect(origin: origin, size: CGSize(width: widthMinusMargins, height: captionTextViewSize.height))
+            if (apply) {
+                captionTextView.frame = captionFrame
+            }
+            origin.y += captionFrame.height
+        } else {
+            origin.y += layoutMargins.bottom
+        }
+    
         return CGSize(width: size.width, height: origin.y)
     }
 }

    
    open override func updateFonts(with traitCollection: UITraitCollection) {
        super.updateFonts(with: traitCollection)
        messageLabel.font = UIFont.wmf_preferredFontForFontFamily(.system, withTextStyle: .subheadline)
        actionButton.titleLabel?.font = UIFont.wmf_preferredFontForFontFamily(.system, withTextStyle: .body)
        dismissButton.titleLabel?.font = UIFont.wmf_preferredFontForFontFamily(.system, withTextStyle: .footnote)
        captionTextView.font = UIFont.wmf_preferredFontForFontFamily(.system, withTextStyle: .footnote)
    }
    
    @objc var isImageViewHidden = false {

            setNeedsLayout()
        }
    }

    fileprivate func updateCaptionTextViewWithAttributedCaption() {
        guard let text = caption else {
            isCaptionHidden = true
            return
        }

        let mutableText = NSMutableAttributedString(attributedString: text)
        guard mutableText.length > 0 else {
            isCaptionHidden = true
            return
        }

        let pStyle = NSMutableParagraphStyle()
        pStyle.lineBreakMode = .byWordWrapping
        pStyle.baseWritingDirection = .natural
        pStyle.alignment = .center
        let font = UIFont.wmf_preferredFontForFontFamily(.system, withTextStyle: .footnote, compatibleWithTraitCollection: traitCollection) ?? UIFont.systemFont(ofSize: UIFont.systemFontSize)
        let attributes: [NSAttributedStringKey : Any] = [NSAttributedStringKey.paragraphStyle: pStyle, NSAttributedStringKey.font: font]
        mutableText.addAttributes(attributes, range: NSMakeRange(0, mutableText.length))
        captionTextView.attributedText = mutableText

        isCaptionHidden = false
    }

    @objc var caption: NSAttributedString? {
        didSet {
            updateCaptionTextViewWithAttributedCaption()
        }
    }
    
