What’s it

EmphasizeString(https://gitee.com/kmyhy/emphasizedstring) is an extension that it help you to emphasize, highlight, underline, style specific sub-strings in a string. For example, given a string said ‘Pls login now at www.yourdomain.com’, you can make thewww.yourdomain.comlook like an url which has an underline textdecoration with this extension.

EmphasizeString is extremely easy to use, please keep reading.

How to use

EmphasizedString extend from String, but to be convenient, it also include 2 UIKit extension: UILabel and UITextView. Absolutely, feel free to extend it to other UIKit to take advantage of its functionality.

UILabel extension

You can use this extension to customize your UILabel’s text style:

label.text = "your special conditioner"
label.setEmphasizedText("conditioner", emphasizedFont: UIFont.boldSystemFont(ofSize: 20), emphasizedColor: UIColor.red)

It looks like:

Other than change text color and font, you can do more better like this:

let attributes = [
 	NSAttributedString.Key.underlineStyle: 1
]
label.setEmphasizedText("conditioner", emphasizedAttributes: attributes)

You can see that:

UITextView extention

Sometime, we need to insert some clickable rich-text element into our string. we can make it by this extension:

...
messageTextView.setLinkedText("salonlabandme.com", linkTo: "http://salonlabandme.com")

It makes the textsalonlabandme.comclickable and looks like this:

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注