141 Setting the horizontal alignment of text in the screen (Extended knowledge: Set label line spacing)

Source: Internet
Author: User

The effect is as follows:

ViewController.h

1 #import <UIKit/UIKit.h>23@interface  viewcontroller: Uiviewcontroller4@end

Viewcontroller.m

1 #import "ViewController.h"2 #import "KMLabel.h"3 4 @interfaceViewcontroller ()5- (void) Layoutui;6 @end7 8 @implementationViewcontroller9 Ten- (void) Viewdidload { One [Super Viewdidload]; A      - [self layoutui]; - } the  -- (void) didreceivememorywarning { - [Super didreceivememorywarning]; -     //Dispose of any resources the can be recreated. + } -  +- (void) Layoutui { ASelf.title =@"set the horizontal alignment of text in the screen"; at      -CGRect Newrect = CGRectMake (20.0,40.0,320.0,160.0); -Kmlabel *lblleft =[[Kmlabel alloc] initwithtextalignment:nstextalignmentleft]; -Lblleft.frame =Newrect; -Lblleft.textcolor =[Uicolor Bluecolor]; -Lblleft.linebreakmode =nslinebreakbywordwrapping; in [Self.view Addsubview:lblleft]; -      toKmlabel *lblcenter =[[Kmlabel alloc] initwithtextalignment:nstextalignmentcenter]; +NEWRECT.ORIGIN.Y + = the; -Lblcenter.frame =Newrect; theLblcenter.textcolor =[Uicolor Browncolor]; *Lblcenter.linebreakmode =nslinebreakbywordwrapping; $ [Self.view Addsubview:lblcenter];Panax Notoginseng      -Kmlabel *lblright =[[Kmlabel alloc] initwithtextalignment:nstextalignmentright]; theNEWRECT.ORIGIN.Y + = the; +Lblright.frame =Newrect; ALblright.textcolor =[Uicolor Darkgraycolor]; theLblright.linebreakmode =nslinebreakbywordwrapping; + [Self.view addsubview:lblright]; - } $  $ @end

KMLabel.h

1 #import <UIKit/UIKit.h>23@interface  kmlabel:uilabel4 -(ID) initwithtextalignment: (nstextalignment) textalignment; 5 6 @end

kmlabel.m

1 #import "KMLabel.h"2 3 @interfaceKmlabel ()4 @end5 6 @implementationKmlabel7 8- (ID) Initwithtextalignment: (nstextalignment) textalignment {9     if(self =[Super Init]) {TenSelf.textalignment =textalignment; OneSelf.backgroundcolor =[Uicolor Whitecolor]; ASelf.autoresizingmask = Uiviewautoresizingflexibletopmargin | -Uiviewautoresizingflexibleleftmargin | -Uiviewautoresizingflexiblebottommargin | the Uiviewautoresizingflexiblerightmargin; -Self.layer.masksToBounds =YES; -Self.layer.cornerRadius =10.0; -Self.layer.borderColor =[Uicolor Graycolor]. Cgcolor; +Self.layer.borderWidth =1.0; -     } +     returnSelf ; A } at  -- (void) DrawRect: (cgrect) rect { -NSString *STRMSG; -     Switch(self.textalignment) { -          Casenstextalignmentleft: { -STRMSG =@"Left justified \ n left justified \ n left justified"; in              Break; -         } to          CaseNstextalignmentcenter: { +STRMSG =@"Center align \ n center align \ n Center"; -              Break; the         } *          Casenstextalignmentright: { $STRMSG =@"Right justify \ n Right justify \ n Right";Panax Notoginseng              Break; -         } the         default: { +              Break; A         } the     } +      -     //to draw $Nsmutableparagraphstyle *paragraphstyle =[[Nsparagraphstyle Defaultparagraphstyle] mutablecopy]; $     //Set line break mode -Paragraphstyle.linebreakmode =Self.linebreakmode; -     //Set Text Alignment theParagraphstyle.alignment =self.textalignment; -Paragraphstyle.linespacing =5.0;//Set Line spacingWuyi      theNsdictionary *dicattribute = @{ - NSForegroundColorAttributeName:self.textColor, WuNsfontattributename: [Uifont systemfontofsize:18.0], -Nsbaselineoffsetattributename: [NSNumber numberwithfloat:-10.0],//the top baseline of each line of content starts with a vertical offset (increases the line spacing), the positive floating point represents the top move, and the negative floating point represents the lower movement About Nsparagraphstyleattributename:paragraphstyle $                                    }; -      - [STRMSG drawinrect:rect withattributes:dicattribute]; -     //the effect is similar to the above statement, but the width and vertical layout height can not be limited, and the horizontal alignment can not be limited, only one line display content A     //[STRMSG drawAtPoint:rect.origin withattributes:dicattribute];//the width (height for vertical layout) of the rendering area is unlimited, unlike drawinrect:withattributes:, which US Es a bounding rectangle. As a result, this method is renders the text in a. + } the  - @end

141 Setting the horizontal alignment of text in the screen (Extended knowledge: Set label line spacing)

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.