             }
         }
     }
-    
+
     /// default is `0`.
     @IBInspectable
     open var numberOfLines: Int {

            }
        }
    }
    
    /// If need to use intrinsicContentSize set true. Also should call invalidateIntrinsicContentSize when intrinsicContentSize is cached. When text was changed for example.
    public var intrinsicAutoLayout = false
    
    var mergedAttributedText: NSAttributedString? {
        if let attributedText = attributedText {
            return mergeAttributes(attributedText)
        }
        return nil
    }
    
    let container = NSTextContainer()
    let layoutManager = NSLayoutManager()
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        
        isOpaque = false
        contentMode = .redraw
        lineBreakMode = .byTruncatingTail
        padding = 0
        layoutManager.addTextContainer(container)
    }
    
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        isOpaque = false
        contentMode = .redraw
        lineBreakMode = .byTruncatingTail
        padding = 0
        layoutManager.addTextContainer(container)
    }
    
    open override func setNeedsDisplay() {
        if Thread.isMainThread {
            super.setNeedsDisplay()
        }
    }
    
    open override var intrinsicContentSize: CGSize {
        if intrinsicAutoLayout {
            guard let attributedText = mergedAttributedText else {

            return bounds.size
        }
    }

    open override func draw(_ rect: CGRect) {
        guard let attributedText = mergedAttributedText else {
            return
        }

        let storage = NSTextStorage(attributedString: attributedText)
        storage.addLayoutManager(layoutManager)

        container.size = rect.size
        let frame = layoutManager.usedRect(for: container)
        let point = contentAlignment.alignOffset(viewSize: rect.size, containerSize: frame.integral.size)

        let glyphRange = layoutManager.glyphRange(for: container)
        layoutManager.drawBackground(forGlyphRange: glyphRange, at: point)
        layoutManager.drawGlyphs(forGlyphRange: glyphRange, at: point)
    }

    open override func sizeThatFits(_ size: CGSize) -> CGSize {
        guard let attributedText = mergedAttributedText else {
            return .zero
        }

        let storage = NSTextStorage(attributedString: attributedText)
        storage.addLayoutManager(layoutManager)

        container.size = size
        let frame = layoutManager.usedRect(for: container)
        return frame.integral.size
    }

    open override func sizeToFit() {
        super.sizeToFit()

        frame.size = sizeThatFits(CGSize(width: bounds.width, height: CGFloat.greatestFiniteMagnitude))
    }

    private func mergeAttributes(_ attributedText: NSAttributedString) -> NSAttributedString {
        let attrString = NSMutableAttributedString(attributedString: attributedText)

        addAttribute(attrString, attrName: .font, attr: font)

        if let textColor = textColor {
            addAttribute(attrString, attrName: .foregroundColor, attr: textColor)
        }

        if let paragraphStyle = paragraphStyle {
            addAttribute(attrString, attrName: .paragraphStyle, attr: paragraphStyle)
        }

        if let shadow = shadow {
            addAttribute(attrString, attrName: .shadow, attr: shadow)
        }

        return attrString
    }

