如何在同一串文字中凸现富文本-EmphasizeString

Posted 颐和园

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在同一串文字中凸现富文本-EmphasizeString相关的知识,希望对你有一定的参考价值。

What’s it

EmphasizeString(https://gitee.com/kmyhy/emphasized-string) 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 the ‘www.yourdomain.com’ look like an url which has an underline text-decoration 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 text ‘salonlabandme.com’ clickable and looks like this:

以上是关于如何在同一串文字中凸现富文本-EmphasizeString的主要内容,如果未能解决你的问题,请参考以下文章

如何在同一串文字中凸现富文本-EmphasizeString

Axure 文本框去掉边框 富文本 粘贴文字图标

Axure 文本框去掉边框 富文本 粘贴文字图标

一个简单富文本输入框控件的设计历程(一)

富文本框显示文字长度 超出变色

富文本中文字部分提取