Custom Justified Label

Source: Internet
Author: User

How to use:

Fnlabel *TEXTL = [[Fnlabel alloc] initWithFrame: (cgrect) {20,100,300,maxfloat}];

NSString *textstr = @ ", Attack oh evaluation, send my home even odd even give, I saw your access to interest rate; Get the Magic card, the classroom is the norm? Give me a poker, give me a card, I'm afraid, better than, guest to judge, send me; get the Magic cards, the classroom is the norm? To me, I am afraid, than, the guest to evaluate, send my house even odd even give, I saw you visit ";

Textl.backgroundcolor = [Uicolor Redcolor];

[Self.view ADDSUBVIEW:TEXTL];

Use effect

Core approach:

Text attribute Nsmutableparagraphstyle and coretext combination

Use Note:

1. Do not set NumberOfLines, wrap line

2. Default line spacing is 5, can be changed in 104 rows, you can add more properties yourself

3.string property for adding rich Text, text for normal text

Code----Declaration File

#import <UIKit/UIKit.h>@interface  fnlabel:uiview@property (nonatomic, Strong) Nsmutableattributedstringstring,@property (nonatomic, strong) uifont* font; @property ( Nonatomic, Strong) uicolor* textcolor; -(void) SetText: (nsstring*) text; @end

Code----Implementation file

1 #import "FNLabel.h"2 #import<CoreText/CoreText.h>3 4 @implementationFnlabel5 6- (ID) initWithFrame: (CGRect) Frame7 {8Self =[Super Initwithframe:frame];9     if(self) {TenSelf.backgroundcolor =[Uicolor Clearcolor]; One     } A     returnSelf ; - } -  the- (void) DrawRect: (cgrect) Rect - { - [self formatString]; -      +Cgcontextref CTX =Uigraphicsgetcurrentcontext (); -      +     //transformation matrix from text space to user control A Cgcontextsettextmatrix (CTX, cgaffinetransformidentity); at     //Zoom Range -CGCONTEXTTRANSLATECTM (CTX,0, self.bounds.size.height); -     //adjust the correct coordinate system -CGCONTEXTSCALECTM (CTX,1.0, -1.0); -      -     //1. Get framesetter from Rich text inCtframesetterref Framesetter =ctframesettercreatewithattributedstring ((__bridge cfattributedstringref) _string); -     //2. Get path from Self.bounds toCgmutablepathref Path =cgpathcreatemutable (); + cgpathaddrect (Path, NULL, self.bounds); -     //3. By Framesetter (content text contents), range (the range to be drawn), path text rectangle gets ctframeref theCtframeref frame = Ctframesettercreateframe (Framesetter, Cfrangemake (0, [_string length]), path, NULL); *     //4. Draw Text $ Ctframedraw (frame, ctx);Panax Notoginseng      - cfrelease (path); the cfrelease (frame); + cfrelease (framesetter); A } the #pragmaMark-Normal two-side align +- (void) SetText: (NSString *) Text - { $_string =[[Nsmutableattributedstring alloc] initwithstring:text]; $     if(_textcolor) { -[_string addattribute:nsforegroundcolorattributename Value:_textcolor Range:nsmakerange (0, [_string length])]; -     } the     if(_font) { -[_string addattribute:nsfontattributename Value:_font Range:nsmakerange (0, [_string length])];Wuyi     } the [self setneedsdisplay]; - } Wu #pragmaMark-Save Overall text color -- (void) SetTextColor: (Uicolor *) TextColor About { $_textcolor =TextColor; - } - #pragmaMark-Save the whole font -- (void) SetFont: (Uifont *) Font A { +_font =font; the } - #pragmaMark-Add all Rich Text properties $- (void) SetString: (nsmutableattributedstring *)string the { the      thensmutableattributedstring *str = [[Nsmutableattributedstring alloc] initwithstring:string.string]; the      -_string =str; in     //Set Text color the     if(_textcolor = =Nil) { the_textcolor =[Uicolor Blackcolor]; About     } the[_string addattribute:nsforegroundcolorattributename Value:_textcolor Range:nsmakerange (0, [stringlength]); the     if(_font = =Nil) { the_font = [Uifont systemfontofsize: -]; +     } -     //Set Text font the[_string addattribute:nsfontattributename Value:_font Range:nsmakerange (0, [stringlength]);Bayi      the     //traverse Add all font Text properties the[stringEnumerateattribute:nsfontattributename Inrange:nsmakerange (0, _string.length) options:nsattributedstringenumerationlongesteffectiverangenotrequired usingBlock:^ (ID_nullable value, Nsrange range, BOOL *_nonnull stop) { -         if(value) { - [_string addattribute:nsfontattributename value:value Range:range]; the              the [self setneedsdisplay]; the         } the     }]; -     //traverse Add all color Text properties the[stringEnumerateattribute:nsforegroundcolorattributename Inrange:nsmakerange (0, _string.length) options:nsattributedstringenumerationlongesteffectiverangenotrequired usingBlock:^ (ID_nullable value, Nsrange range, BOOL *_nonnull stop) { the         if(value) { the [_string addattribute:nsforegroundcolorattributename value:value Range:range];94              the [self setneedsdisplay]; the         } the     }];98 } About #pragmaMark-Add justification properties -- (void) formatString101 {102Nsmutableparagraphstyle *style =[[Nsmutableparagraphstyle alloc] init];103Style.alignment =nstextalignmentjustified;104Style.linespacing =5; the[_string addattribute:nsparagraphstyleattributename Value:style Range:nsmakerange (0, _string.length)];106     107 }108 109 @end

Custom Justified Label

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.