Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clickable image inside Attributed Label #64

Closed
Sob7y opened this issue Dec 4, 2018 · 1 comment
Closed

Clickable image inside Attributed Label #64

Sob7y opened this issue Dec 4, 2018 · 1 comment

Comments

@Sob7y
Copy link

Sob7y commented Dec 4, 2018

Hello,
I'm trying to add multiple attachments (NSTextAttachment) to the Attributed Label to looks like the attached image but it's not clickable
any way to do it?

205_sura_info egthadi

let number = Atributika.Style("b")
            .font(AppFont.getUthmanTahaBoldFont(size: 16), .normal)
            .backgroundColor(self.view.backgroundColor!, .highlighted)
            .backgroundColor(self.view.backgroundColor!, .normal)
            .foregroundColor(.white, .normal)

        let str = "<b>Running</b> with <img note=\"1\"></img> bla bla bla <img note=\"1\"></img> <b>Running</b> with <img note=\"1\"></img> bla bla bla <img note=\"1\"></img> <b>Running</b> with <img note=\"1\"></img> bla bla bla <img note=\"1\"></img> <b>Running</b> with <img note=\"1\"></img> bla bla bla <img note=\"1\"></img>".style(tags: number)

        let mutableAttrStr = NSMutableAttributedString(attributedString: str.attributedString)

        var locationShift = 0
        for detection in str.detections {
            switch detection.type {
            case .tag(let tag):
                if let imageId =  tag.attributes["note"] {
                    let textAttachment = NSTextAttachment()
                    let imgview = FootnoteView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 20, height: 20)), number: imageId)
                    let image = imgview.asImage()
                     textAttachment.image = image
                    let imageAttrStr = NSAttributedString(attachment: textAttachment)
                    let nsrange = NSRange(detection.range, in: mutableAttrStr.string)
                    mutableAttrStr.insert(imageAttrStr, at: nsrange.location + locationShift)
                    locationShift += 1
                }
            default:
                break
            }
        }

        attributedLbl.numberOfLines = 0
        let all = Atributika.Style.font(AppFont.getUthmanTahaBoldFont(size: 16), .normal)
            .backgroundColor(.pinkColor, .highlighted)
            .backgroundColor(.pinkColor, .normal)
            .foregroundColor(.white, .normal)

        attributedLbl.attributedText = mutableAttrStr.styleAll(all)
        attributedLbl.onClick = { label, detection in
            switch detection.type {
            case .tag(let tag):
                if tag.name == "img" {
                    let alert = UIAlertController(title: "Test", message: "Success", preferredStyle: .alert)
                    let okAction = UIAlertAction(title: L10n.Download.lblOk, style: .cancel) { (_) in

                    }
                    alert.addAction(okAction)
                    self.present(alert, animated: true)
                }
            default:
                break
            }
        }
@psharanda
Copy link
Owner

Hello,
Unfortunately there is no support for NSTextAttachment in AttributedLabel at this moment. This is a known issue (#54)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants