On IOS Rich Text

Source: Internet
Author: User

Sometimes, we need to display different colors, different sizes of text in one or more lines of text, even some need to click, some do not need. This is collectively referred to as rich text.

There are a lot of similar apps in the Web. Apart from the Web page, I now encounter this situation is also non-use, user policy and User Agreement in a multi-lingual implementation, considering the adaptive, it must be displayed in the same control (Uilabel/uitextview).

nsmutableattributedstring/nsattributedstring is used to represent rich text.

As we have a text, the middle of the two paragraph is required to display different colors, you can click, because it is internationalized, each paragraph length is not the same, so simply completely opened.

var attrDic1 = [Nsfontattributename:uifont. systemfontofsize ()]

var str1 = nslocalizedstring("register prompt First SE Gment ", Comment: " Register prompt First segment ")

var attrDic2 = [nsfontattributename: uifont. systemfontofsize (), Nsforegroundcolorattributename : Uicolor (red: 255, Green: 126/255 , Blue: 255, Alpha: 1), Nslinkattributename:"http://www.baidu.com"]

var str2 = nslocalizedstring("Register prompt second S Egment ", Comment: " Register prompt second segment ")

var attrDic3 = [nsfontattributename: uifont. systemfontofsize ()]

var str3 = nslocalizedstring("register prompt Three SE Gment ", Comment: " Register prompt three segment ")

var attrDic4 = [nsfontattributename: uifont. systemfontofsize (), Nsforegroundcolorattributename : Uicolor (red: 255, Green: 126/255 , Blue: 255, Alpha: 1), Nslinkattributename:"http://www.baidu.com"]

var str4 = nslocalizedstring("register prompt Four SEG ment ", Comment: " Register prompt Four segment ")

var attrDic5 = [nsfontattributename: uifont. systemfontofsize ()]

var str5 = nslocalizedstring("register prompt Five SEG ment ", Comment: " Register prompt Five segment ")

var allstr = STR1+STR2+STR3+STR4+STR5

var attrStr1 = nsmutableattributedstring(string:str1, attributes : AttrDic1)

var attrStr2 = nsmutableattributedstring(STRING:STR2, Attributes:attrdic 2)

var attrStr3 = nsmutableattributedstring(STRING:STR3, Attributes:attrdic 3)

var attrStr4 = nsmutableattributedstring(STRING:STR4, attributes : ATTRDIC4)

var attrStr5 = nsmutableattributedstring(STRING:STR5, attributes : ATTRDIC5)

attrStr1. appendattributedstring (ATTRSTR2);

attrStr1. appendattributedstring (ATTRSTR3);

attrStr1. appendattributedstring (ATTRSTR4);

attrStr1. appendattributedstring (ATTRSTR5);

vprompttextview. Attributedtext = AttrStr1

Vprompttextview.linktextattributes = AttrDic2

Vprompttextview.userinteractionenabled = True

Vprompttextview.scrollenabled = False

Vprompttextview.editable = False

Vprompttextview.selectable = True

vPromptTextView.textContainer.lineFragmentPadding = 0

Vprompttextview.textcontainerinset = uiedgeinsetsmake (0, 0, 0, 0)

Vprompttextview.delegate = self;

vprompttextview. TextAlignment = nstextalignment . Center

Vprompttextview.datadetectortypes = Uidatadetectortypes.all

var tap = uitapgesturerecognizer(target: Self , Action: "Tappedtextview:" )

vprompttextview. Addgesturerecognizer (TAP)

Here is the 5 paragraph content, the use of internationalization, Uilabel said is not clickable, in fact, I put all the comments open, at the same time to achieve the uitextview of the Commission, there is no way to click, OK, I do not know where the problem. According to the content of the online search, you can click, that is, to open a link with safiri, so the words and my actual application scenarios are different, so use the tapgesture way to see the specific implementation:

func tappedtextview (tapgesture:uitapgesturerecognizer) {

if tapgesture. State != uigesturerecognizerstate . Ended {

return

}

var textView = tapgesture. View as Uitextview

var taplocation = tapgesture. Locationinview (TextView)

var textposition = TextView. Closestpositiontopoint (taplocation)

var attributes = TextView. textstylingatposition (TextPosition, indirection: uitextstoragedirection. Backward )

if let url = attributes[nslinkattributename] as ? String {

Uiapplication.sharedapplication (). OpenURL (Nsurl (string:url)!)

link = URL

self. Performseguewithidentifier ("OpenLink", Sender: self)

}

}


This will enable me to achieve my own content, but also to achieve internal click.

Text Mix The next step is the core text rendering, this content is very rich.

IOS7 added Textkit, which is encapsulated on core text.

iOS content is quite rich, is not dare to call iOS ace, learn more, found not to know the more things.

Finally, two third-party libraries are recommended


Two libraries:dtcoretext=> http://blog.cnbang.net/tech/2630/

tttattributedlabel=> Https://github.com/TTTAttributedLabel/TTTAttributedLabel


On IOS Rich Text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.