Text-To-text mix-use emoticons instead of "[Type]" display

Source: Internet
Author: User

1. Simply set a string with attributes

defines a string with a nsmutableattributedstring property

nsmutableattributedstring *str = [[nsmutableattributedstring alloc] initwithstring: @ "Hello[1_1] world! [ haha] [ smile ] "];

Setting Properties

[StrSetAttributes:@{Nsfontattributename:[uifont systemfontofsize:60], nsforegroundcolorattributename:[uicolor Span class= "S1" >redcolornsbackgroundcolorattributename:[< Span class= "S1" >uicolor greencolor], Nsunderlinecolorattributename:[uicolor Bluecolornsunderlinestyleattributename:@ ( nsunderlinestyledouble)} range:nsmakerange (0, 5)];

< span class= "s2" > < Span class= "S1" > < span class= "s2" >    display

_label. Attributedtext = str;

2. Use emoticons instead of words with "" (QQ session, session)

definition Regular Expressions

nsstring *pattern = @ "\\[[\u4e00-\u9fa5]+\\]";

nsregularexpression *regular = [nsregularexpression regularexpressionwithpattern:p Attern options:nsregularexpressioncaseinsensitive error:nil];

nsstring *text = @ "Hello[1_1" world![ haha ] ";

an array of type Nstextcheckingresult that conforms to the expression.

nsarray *resultarray = [regular matchesinstring: Text options: 0 range:nsmakerange(0, text. Length)];

defines a string with an attachment

nsmutableattributedstring *attstr = [[nsmutableattributedstring alloc] Initwithstring: text];

for (nstextcheckingresult *result in resultarray) {

location

nsrange range = result. Range;

Get Attachments

nstextattachment *attach = [[nstextattachment alloc] init];

set up a picture of an attachment

Attach. image = [UIImage imagenamed:@ "D_guzhang"];

get the string generated by the attachment

nsattributedstring *imagestr = [nsattributedstring attributedstringwithattachment: Attach];

replace the words with expressions

[Attstr replacecharactersinrange: Range withattributedstring: imagestr];

}

_label. Attributedtext = attstr;

Text-To-text mix-use emoticons instead of "[Type]" display

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.