preface
Rich text is a common feature in iOS development. In the face of “fancy” text design, NSAttributedString of Foundation provides us with great help, but its shortcomings also bring us a lot of troubles, for example: In order to solve the pain points of using rich text in daily development, I designed AttributedString to make full use of the characteristics of Swift voice to simplify the rich text construction process, and expand more elegant interaction processing, custom view insertion and so on.
What problems can be solved
1. Tedious creation process
- Simple rich text
- Rich text with paragraph style
- Rich text mixed with text
There is a point here to say that the calculation method of attachment position is often -4-2 on the network at present, which is not correct. Once the size is increased or decreased, it cannot be accurately centered. If you need it, you can have a look at my internal implementation, which should be the most accurate in the whole network. Later, I will have the opportunity to write a separate explanation. 🕊
2. Common click interactions
The native implementation is supported only by UITextView and is limited to URL and NSTextAttachment types.
There are a number of ways UILabel wants to support click-to-click interactions, and I won’t go into that here. All in all, if you want to achieve click and long press triggers, as well as various content types of listening and highlighting style display costs are relatively large.
This is an AttributedString way of listening to get a callback for a click, and there’s another way of doing an action property that I’ll show you briefly.
One more long press trigger.
So that’s about it, so that’s all you have to do with the interaction, and the highlights and all that stuff you can explore in the Demo.
3. Specify content highlighting
For example, if you want to set the phone number in the text to a separate size and color:
Of course, regular expressions should not be less:
4. Insert a custom view
I’m not going to talk about how to implement it native because it’s too much work, there’s a lot of points involved, but I’m just going to show you how AttributedString adds a custom view to a UITextView.
Ahem… Is it too easy? Actually it’s the same as attachment, in AttributedString it’s called Viewattinfringe. The specific location and size Settings are consistent with other accessories. Specific see AttributedString. Attachment. Style.
Other problems
How to get from itNSAttributedString
Objects?
let string: NSAttributedString = AttributedString("LEE").value
Copy the code
Was there any code intrusion?
At present, all functions of UIKit related controls are implemented based on Extension, striving to minimize the impact.
How to append concatenation rich text?
AttributedString has a perfect extension for the + += operator.
It also supports String + AttributedString and NSAttributedString + AttributedString operations.
Only supportiOS
Platform?
AttributedString is actually an encapsulation of the native features of NSAttributedString, and many new features are added. In theory, all platform AttributedString supported by NSAttributedString can support it. Except, of course, for a few features. WatchOS, for example, has no NS ExtAttachment.
In general, AttributedString is currently supported for iOS, tvOS, macOS, and watchOS.
What features will be added in the future?
Many currently in the works include: expression parser, asynchronous rendering (requires building a new UIView subclass), and more.
If you have any suggestions, you can initiate Issues on Github.
I don’t know. Can you explain how it works?
Of course not. Wait for the next one.
Say what you think
As we all know, YYText is an excellent rich text library in OC. It represents an era, but it seems that it has not been maintained any more and has accumulated many Issues that cannot be solved.
Moreover, libraries using OC in Swift are difficult to exploit the features of their language. It just so happens that Swift doesn’t have a rich text library as good as YYText, and that’s always been a problem for me, which is why I created AttributedString. Now AttributedString can’t be as good as YYText… There’s still a lot of work to be done, but I’ll take YYText as the benchmark and do even better than “YYText” in Swift. Thank you all for your support.
conclusion
Through some simple comparisons above, it is obvious how simplified the overall code is. The more complex the rich text code is, the more obvious it is. The overall readability is not a problem, and the call experience is very friendly when writing.
If you feel good about it, you can have a Star Fork Share
AttributedString
Welcome to join the exchange group and learn together