chrisdhaan / CDMarkdownKit

An extensive Swift framework providing simple and customizable markdown parsing.
MIT License
250 stars 65 forks source link

UITextViewDelegate not called textView(_ textView: UITextView, shouldInteractWith.... #40

Open Disconnecter opened 1 year ago

Disconnecter commented 1 year ago

What did you do?

final class MarkDownItemView: StackItemView<MarkDownItemView.Style> {
    override var arrangedViews: [UIView] {
        [markdownView]
    }

    private lazy var markdownView: CDMarkdownTextView = { [unowned self] in
        let textContainer = NSTextContainer(size: .zero)
        let layoutManager = CDMarkdownLayoutManager()
        layoutManager.addTextContainer(textContainer)
        let codeTextView = CDMarkdownTextView(frame: .zero,
                                              textContainer: textContainer,
                                              layoutManager: layoutManager)
        codeTextView.backgroundColor = .clear
        codeTextView.isScrollEnabled = false
        codeTextView.isEditable = false
        codeTextView.isSelectable = true
        codeTextView.delegate = self
        return codeTextView
    }()

    private var model: MarkdownTitleCellModelType?

    override func applyStyle(_ style: Style) {
        setContentInsets(style.contentInsets)
    }
}

extension MarkDownItemView {
    struct Style {
        let headerFont: UIFont
        let font: UIFont
        let boldFont: UIFont
        let italicFont: UIFont
        let linkColor: UIColor
        let fontColor: UIColor
        var contentInsets: UIEdgeInsets = .single(inset: 16)

        static let `default`: Style = .init(
            headerFont: Asset.Font.withStyle(.medium, size: .caption),
            font: Asset.Font.withStyle(.regular, size: .caption),
            boldFont: Asset.Font.withStyle(.medium, size: .caption),
            italicFont: Asset.Font.withStyle(.light, size: .body),
            linkColor: ColorPalette.Purple.darkPrimary.color,
            fontColor: ColorPalette.Black.primary.color
        )
    }

    func setup(model: MarkdownTitleCellModelType) {
        guard let style = styles[model.itemViewState] else {
            return
        }

        self.model = model
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.paragraphSpacing = 3
        paragraphStyle.paragraphSpacingBefore = 12
        paragraphStyle.lineSpacing = 1.38

        let markdownParser = CDMarkdownParser(
            font: style.font,
            boldFont: style.boldFont,
            italicFont: style.italicFont,
            fontColor: style.fontColor,
            paragraphStyle: paragraphStyle
        )
        markdownParser.header.font = style.headerFont
        markdownParser.header.fontIncrease = 1

        let paragraphStyleList = NSMutableParagraphStyle()
        paragraphStyleList.paragraphSpacing = 3
        paragraphStyleList.paragraphSpacingBefore = 0
        paragraphStyleList.lineSpacing = 1.38
        markdownParser.list.paragraphStyle = paragraphStyleList

        markdownView.linkTextAttributes = [.foregroundColor: style.linkColor]
        markdownView.attributedText = markdownParser.parse(model.title)
        restyle(state: model.itemViewState)
    }
}

extension MarkDownItemView: UITextViewDelegate {
    func textView(_ textView: UITextView, shouldInteractWith markdownURL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        if ValidationRule(ruleType: .email).validate(against: markdownURL.absoluteString) {
            model?.emailHandler(markdownURL.absoluteString)
        } else if UIApplication.shared.canOpenURL(markdownURL) {
            UIApplication.shared.open(markdownURL, options: [:])
        }
        return false
    }
}

What did you expect to happen?

call delegate method func textView(_ textView: UITextView, shouldInteractWith markdownURL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {

What happened instead?

nothing

CDMarkdownKit Environment

CDMarkdownKit version: 2.2.0 or 2.3.0 same behaviour Xcode version: 14.1 (14B47b) Swift version: 5.0 Platform(s) running CDMarkdownKit: ios 16 macOS version running Xcode: 13.0 (22A380)

chrisdhaan commented 1 year ago

Thank you for posting this @Disconnecter. I've seen an increase in issues reported related to the TextField and Label components. I will try and get to fixing them soon.