Coder Social home page Coder Social logo

List View has incorrect height about marky-mark HOT 2 CLOSED

m2mobi avatar m2mobi commented on September 25, 2024
List View has incorrect height

from marky-mark.

Comments (2)

jvanzummeren avatar jvanzummeren commented on September 25, 2024

Iā€™m not sure if this is an issue related to MarkyMark, is the same set-up with a multi line UILabel working correctly?

Regards,

Jim

from marky-mark.

illyabusigin avatar illyabusigin commented on September 25, 2024

I had the same issue as @christie-alphero but was able to create my own label class that worked around the issue. This is the most wonderful markdown library on iOS, thank you so much for your hard work @jvanzummeren!

@IBDesignable
open class CustomMarkdownAttributedLabel: AttributedInteractiveLabel {
    
    public let styling = DefaultStyling()
    
    override open var font: UIFont? {
        didSet {
            styling.paragraphStyling.baseFont = font
            markDownAttributedString = attributedText
        }
    }
    
    override open var textColor: UIColor? {
        didSet {
            styling.paragraphStyling.textColor = textColor
            markDownAttributedString = attributedText
        }
    }
    
    override open var text: String? {
        didSet {
            if let attributedText = createMarkDownAttributedString(markDownText: text) {
                markDownAttributedString = attributedText
            }
        }
    }
    
    override open var textAlignment: NSTextAlignment {
        didSet {
            styling.paragraphStyling.textAlignment = markyMarkTextAlignment(ofTextAlignment: textAlignment)
            markDownAttributedString = attributedText
        }
    }
    
    public init(font: UIFont? = nil) {
        super.init()
        self.font = font
        configureViewProperties()
    }
    
    override public init(frame: CGRect) {
        super.init(frame: CGRect())
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        configureViewProperties()
        
        styling.paragraphStyling.baseFont = font
        styling.paragraphStyling.textColor = textColor
        styling.paragraphStyling.textAlignment = markyMarkTextAlignment(ofTextAlignment: textAlignment)
        
        if let attributedText = createMarkDownAttributedString(markDownText: text) {
            markDownAttributedString = attributedText
        }
    }
}

private extension CustomMarkdownAttributedLabel {
    
    func createMarkDownAttributedString(markDownText: String?) -> NSAttributedString? {
        guard let markDownText = markDownText else { return nil }
        
        let markyMark = MarkyMark(build: {
            $0.setFlavor(ContentfulFlavor())

        })
        
        // Parsing to MarkDownItem's
        let markDownItems = markyMark.parseMarkDown(markDownText)
        
        // Converting to views
        let configuration = MarkDownToAttributedStringConverterConfiguration(styling: styling)
        let converter = MarkDownConverter(configuration: configuration)
        
        return converter.convert(markDownItems)
    }
    
    func markyMarkTextAlignment(ofTextAlignment textAlignment: NSTextAlignment) -> TextAlignment {
        let markyMarkTextAlignment: TextAlignment
        
        switch textAlignment {
        case .left:
            markyMarkTextAlignment = .left
        case .right:
            markyMarkTextAlignment = .right
        case .center:
            markyMarkTextAlignment = .center
        default:
            markyMarkTextAlignment = .left
        }
        
        return markyMarkTextAlignment
    }
}

from marky-mark.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.