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