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

Add support for styling NSLinkAttribute with existing urlAttributes in MessageLabel #1091

Conversation

marcetcheverry
Copy link

What does this implement/fix? Explain your changes.
—————————————————————————

Since MessageLabel supports handling NSLinkAttribute as tappable links, this add support for styling the links with the same styles as a URL data detector.

UILabel has no option to override the default styling that it applies to NSLinkAttribute, this faculty is reserved for UITextView's linkTextAttributes.

The only way to imitate this behavior is to remove the NSLinkAttribute before drawing and to apply our own attributes (which have already been detected by the existing code in MessageLabel).

With the current behavior you just get a blue style which is the default UILabel style.

Below is a real world example of using NSLinkAttribute to highlight a filename which will open an url when tapped.

Screen Shot 2019-06-11 at 10 07 27

A future PR may want to implement a highlight/touch down attributed style for these links

Any other comments?

Not sure which branch to submit too, but I am using Swift 5.

Where has this been tested?

Devices/Simulators: iPhone XS

iOS Version: 12.2

Swift Version: Swift 5

MessageKit Version: 3.0.0-swif5

👻

@nathantannar4
Copy link
Member

Could you please elaborate on why we would want to remove the link attribute? It seems odd that if a user specifies an attributed string to have a link attribute that it gets removed. This could break people's code.

If specific highlighting is wanted, is there a problem with a user just not using the link detector type?

Sent with GitHawk

@marcetcheverry
Copy link
Author

marcetcheverry commented Jun 13, 2019

It isn't removed in the attributedString, but in the one that gets rendered through the text storage. It is the only way to actually make UILabel render a NSLinkAttribute with the attributes you want (in this case, MessageLabel parses that it was a NSLinkAttribute and takes over with its own attributes and link handling mechanisms).

Otherwise if the NSLinkAttribute is present but you have attributes such as foregroundColor they will get ignored. You will get the default styling no matter what. This is why UITextView has that property to override link styling.

The link detector type only works with full URLs, and not arbitrary words marked as links, as in the screenshot shown. See "Attempts to locate URL links." in the documentation. Using a custom regex is not a proper solution.

This does not break people's code, links are still drawn and handled the same way as before, in fact, MessageLabel had existing code to detect the attribute and handle those taps. This piece of code just ensures that NSTextStorage and NSLayoutManager draws them the same as 'data detected' links, and not with the default UILabel style.

No data is lost, it is a rendering choice to use urlAttributes for NSLinkAttribute as well.

 

@nathantannar4 nathantannar4 changed the base branch from 3.0.0-swift5 to development June 29, 2019 00:29
CHANGELOG.md Outdated Show resolved Hide resolved
// This will enable us to attribute it with our own styles, since `UILabel` does not provide link attribute overrides like `UITextView` does
if detector.textCheckingType == .link {
mutableAttributedString.removeAttribute(NSAttributedString.Key.link, range: range)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok looks good!

@nathantannar4 nathantannar4 merged commit 5dcec8b into MessageKit:development Jul 1, 2019
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

Successfully merging this pull request may close these issues.

2 participants