wordpress-mobile / WordPress-iOS-Shared

Shared components used in building the WordPress iOS apps and other library components
GNU General Public License v2.0
18 stars 22 forks source link

Adding support for NSAttributedString in WPNoResultsView message #127

Closed etoledom closed 6 years ago

etoledom commented 6 years ago

This PR adds support for attributes in the WPNoResultsView's message 'label'. This is used to show a tappable link in the Stock Photos feature in the Main WPiOS app.

These changes where already reviewed and accepted here, but since we were working in a feature branch, this changes didn't make it to develop on time.

This is a re-implementation of those changes so they are accessible in the feature branch after updating with develop in the main WPiOS project.

jleandroperez commented 6 years ago

Update Mark II:

(Suggestions!!)

  1. Construct the string with the target Font + Color + Alignment
  2. Then linkify all the things with a snippet like this:
extension NSMutableAttributedString {

    private var foundationString: NSString {
        return string as NSString
    }

    func linkify(anchorText: String, link: String) {
        var range = foundationString.range(of: anchorText)
        while range.location != NSNotFound {
            addAttribute(.link, value: link, range: range)

            let minimumRange = NSRange(location: range.location + 1, length: foundationString.length - range.location - 1)
            range = foundationString.range(of: anchorText, options: .caseInsensitive, range: minimumRange)
        }
    }
}

let test = NSMutableAttributedString(string: "Automattic Company Testing", attributes: nil)
test.linkify(anchorText: "Company", link: "http://www.a8c.com")

--

Squirrel still stands, this can be improved whenever the WPNoResults replacement is in place!

etoledom commented 6 years ago

We decided to add those improvements to the new NoResultsViewController

Meanwhile, I added a comment to the attributedMessageText property explaining intention and behavior.

Thank you @jleandroperez !