Simple use of uilabel Rich Text

Source: Internet
Author: User

Simple use of uilabel Rich Text

 

Effect:

Source code:

Nsstring + Yx. h nsstring + Yx. m

/// Nsstring + Yx. h // yxkit /// copyright (c) 2014 y. x. all rights reserved. // # import <Foundation/Foundation. h> # import "configattributedstring. H "@ interface nsstring (Yx) // create rich text and configure rich text (the data in nsarray must be a collection of configattributedstring objects)-(nsmutableattributedstring *) createattributedstringandconfig :( nsarray *) configs; // used to search for the nsange value of a string in another string-(nsange) rangefrom :( nsstring *) string; // The range-(nsange) range of the string; @ end
/// Nsstring + Yx. M // yxkit /// copyright (c) 2014 y. x. all rights reserved. // # import "nsstring + Yx. H "@ implementation nsstring (Yx)-(nsmutableattributedstring *) createattributedstringandconfig :( nsarray *) configs {S * attributedstring = [[nsmutableattributedstring alloc] initwithstring: Self]; [configs failed: ^ (id obj, nsuinteger idx, bool * Stop) {configattributedstring * oneconfig = OBJ; [attributedstring addattribute: oneconfig. attribute Value: oneconfig. value Range: oneconfig. range];}]; return attributedstring;}-(nsange) rangefrom :( nsstring *) string {return [String rangeofstring: Self];}-(nsange) range {return nsmakerange (0, self. length) ;}@ end

Configattributedstring. h configattributedstring. m

//// Configattributedstring. h // nsmutableattributedstring // copyright (c) 2014 y. x. all rights reserved. // # import <Foundation/Foundation. h> @ interface configattributedstring: nsobject @ property (nonatomic, strong, readonly) nsstring * attribute; // Rich Text attribute @ property (nonatomic, strong, readonly) ID value; // Rich Text value @ property (nonatomic, assign, readonly) nsange range; // Rich Text range value // general configuration + (instancetype) attribute :( nsstring *) attribute Value :( ID) value range :( nsange) range; // configure the font + (instancetype) font :( uifont *) font range :( nsange) range; // configure the font color + (instancetype) foregroundcolor :( uicolor *) color range :( nsange) range; // configure the font background color + (instancetype) backgroundcolor :( uicolor *) color Range: (nsange) range; // font stroke color, stroke width, and shadow (the following two methods can be used together) + (instancetype) strokecolor :( uicolor *) color Range: (nsange) range; + (instancetype) strokewidth :( float) number range :( nsange) range; + (instancetype) shadow :( nsshadow *) shadow range :( nsange) range; // configure the underlined text + (instancetype) strikethroughstyle :( nsinteger) number range :( nsange) range; // configure the underlined text + (instancetype) underlinestyle :( nsinteger) number range: (nsange) range; // padding + (instancetype) Kern :( float) number range :( nsange) range; // paragraph style (it is useful to set numberoflines in uilabel to 0) + (instancetype) paragraphstyle :( nsmutableparagraphstyle *) style range :( nsange) range; @ end
//// Configattributedstring. M // nsmutableattributedstring // copyright (c) 2014 y. x. all rights reserved. // # import "configattributedstring. H "@ interface configattributedstring () @ property (nonatomic, strong) nsstring * attribute; @ property (nonatomic, strong) ID value; @ property (nonatomic, assign) nsange range; @ end @ implementation configattributedstring + (instancetype) attribute :( nsstring *) attribute value :( ID) value range :( nsange) range {configattributedstring * Config = [self new]; config. attribute = attribute; config. value = value; config. range = range; return config;} + (instancetype) font :( uifont *) font range :( nsange) range {configattributedstring * Config = [self new]; config. attribute = nsfontattributename; config. value = font; config. range = range; return config;} + (instancetype) foregroundcolor :( uicolor *) color range :( nsange) range {configattributedstring * Config = [self new]; config. attribute = nsforegroundcolorattributename; config. value = color; config. range = range; return config;} + (instancetype) backgroundcolor :( uicolor *) color range :( nsange) range {configattributedstring * Config = [self new]; config. attribute = nsbackgroundcolorattributename; config. value = color; config. range = range; return config;} + (instancetype) strikethroughstyle :( nsinteger) number range :( nsange) range {configattributedstring * Config = [self new]; config. attribute = nsstrikethroughstyleattributename; config. value = [nsnumber numberwithinteger: number]; config. range = range; return config;} + (instancetype) paragraphstyle :( nsmutableparagraphstyle *) style range :( nsange) range {configattributedstring * Config = [self new]; config. attribute = nsparagraphstyleattributename; config. value = style; config. range = range; return config;} + (instancetype) Kern :( float) number range :( nsange) range {configattributedstring * Config = [self new]; config. attribute = nskernattributename; config. value = [nsnumber numberwithfloat: number]; config. range = range; return config;} + (instancetype) underlinestyle :( nsinteger) number range :( nsange) range {configattributedstring * Config = [self new]; config. attribute = nsunderlinestyleattributename; config. value = [nsnumber numberwithinteger: number]; config. range = range; return config;} + (instancetype) strokecolor :( uicolor *) color range :( nsange) range {configattributedstring * Config = [self new]; config. attribute = nsstrokecolorattributename; config. value = color; config. range = range; return config;} + (instancetype) strokewidth :( float) number range :( nsange) range {configattributedstring * Config = [self new]; config. attribute = nsstrokewidthattributename; config. value = [nsnumber numberwithfloat: number]; config. range = range; return config;} + (instancetype) shadow :( nsshadow *) shadow range :( nsange) range {configattributedstring * Config = [self new]; config. attribute = nsshadowattributename; config. value = shadow; config. range = range; return config;} @ end

Controller source code:

//// Rootviewcontroller. M // richtext /// copyright (c) 2014 y. x. all rights reserved. // # import "rootviewcontroller. H "# import" nsstring + Yx. H "@ interface rootviewcontroller () @ end @ implementation rootviewcontroller-(void) viewdidload {[Super viewdidload]; // string nsstring * STR = @ "unselected path-Frost \ n the yellow forest separates two paths. \ n unfortunately, I cannot get involved at the same time, \ n I stood at the intersection for a long time, \ n I looked at a path, \ n until it disappeared into the depths of the jungle. \ N, but I chose another path, \ n, which is very deserted, \ n seems more attractive and beautiful, \ n although in these two small roads, \ n rarely leaves the footprints of travelers. \ n although the leaves were full in the early morning, \ n was not polluted by footprints. \ N, leave a path and wait for another day to see you! \ N, but I know the path is endless. \ n may be difficult for me to return. \ N maybe some years later, \ n I will sigh softly to review the past, \ n a piece of woods separated by two roads, \ n and I chose one with fewer traces, \ n has decided the path of my life. "; // Set the group nsarray * array = @ [// global settings [configattributedstring Font: [uifont systemfontofsize: 10.f] range: [STR range], [configattributedstring paragraphstyle: [self style] range: [STR range], // set [configattributedstring foregroundcolor: [uicolor redcolor] range: [@ "unselected path" rangefrom: Str], [configattributedstring Font: [uifont systemfontofsize: 20.f] range: [@ "unselected path" rangefrom: Str]; // initialize the Rich Text uilabel * label = [[uilabel alloc] initwithframe: Self. view. bounds]; label. numberoflines = 0; label. attributedtext = [STR createattributedstringandconfig: array]; [self. view addsubview: Label];} // section style-(nsmutableparagraphstyle *) style {nsmutableparagraphstyle * style = [nsmutableparagraphstyle new]; style. linespacing = 10.f; style. firstlineheadindent = 10.f; return style ;}@ end

Design principle:

I abstracted the configuration into an object, and an object may correspond to different configurations, so it won't be messy to write.

You can directly configure it in the array when using it.

You can directly generate rich text using the nsstring category and assign the value to attributedtext to complete the Rich Text operation.

This is called object-oriented programming :)

 

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.