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

When there are multiple clickable texts in the string, the color cannot be set differently #419

Open
zuqiu123 opened this issue Jun 12, 2022 · 2 comments

Comments

@zuqiu123
Copy link

let htmlStr = "You can parse HTML with <strong>strong</strong>, <em>em</em>, and even <strong><em>nested strong and em</em></strong> tags."
let emStr = StringStyle(
    .link(URL("emStr://")),
    .color(UIColor.red),
    .underline(.single, nil)
)

let strongStr = StringStyle(
    .link(URL("strongStr://")),
    .color(UIColor.green),
    .underline(.single, nil)
)

let style = StringStyle(
    .font(.systemFont(ofSize: 30)),
    .color(.black),
    .xmlRules([
        .style("em", emStr),
        .style("strong", strongStr),
    ])
)
let attributedText = htmlStr.styled(with: style)

textView.linkTextAttributes = [
    NSAttributedString.Key.foregroundColor: UIColor.yellow
]
textView.attributedText = attributedText

Two links are set. If you do not set this sentence,

  textView.linkTextAttributes = [
            NSAttributedString.Key.foregroundColor: UIColor.yellow
        ]

image

they will all show blue. After setting, they will all turn yellow. How to set two different colors, and you can click the link,please help

@ZevEisenberg
Copy link
Collaborator

Hi @zuqiu123, and thanks for your report. Unfortunately, I think this is a limitation of UITextView/UILabel, where it won't let you override the color of individual links. I don't know if TTTAttributedLabel supports multicolored links within the same attributed string, but it's worth a look.

@ZevEisenberg
Copy link
Collaborator

You might try this as well, although I haven't tested it either https://github.com/zacwest/ZSWTappableLabel

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