Objective:
There is no reason not to try. png
Body:
Want to achieve Uilabel alignment, center alignment, under the alignment, the following effect:
. png in iOS default Uilabel text in the vertical direction can only center alignment, Bo Master reference to foreign sites, from the Uilabel inherit a new class, to achieve the alignment of the home, center alignment, the bottom of the alignment
Specific as follows:
Created: MYLabel inheritance and Uilabel
Completed in MYLabel.h
Completed in MYLabel.h
////MYLabel.h //labeldemo////Created by Wangergang on 2016/12/7.// Copyright? 2016 mycompangname. All rights Reserved.//#import <uikit/uikit.h>typedef < Span class= "Hljs-keyword" >enum {verticalalignmenttop = 0, // Default Verticalalignmentmiddle, Verticalalignmentbottom,} verticalalignment; @interface Mylabel:uilabel {@private verticalalignment _verticalalignment;} @property (nonatomic) verticalalignment verticalalignment;@end
Completed in MYLABEL.M
Completed in MYLABEL.M
//mylabel.mLabeldemo//Created by Wangergang on 2016/12/7.Copyright? 2016 mycompangname. All rights reserved.//#import"MYLabel.h"@implementationMYLabel@synthesize VerticalAlignment = verticalalignment_;-(ID) initWithFrame: (CGRect) Frame {Self = [Super Initwithframe:frame];if (Self) {Self.verticalalignment = Verticalalignmentmiddle; }ReturnSelf;} - (void) Setverticalalignment: (verticalalignment) verticalalignment {verticalalignment_ = VerticalAlignment; [Self setneedslayout];} - (CGRect) Textrectforbounds: (CGRect) Bounds Limitedtonumberoflines: (Nsinteger) NumberOfLines {CGRect Textrect = [Super Textrectforbounds:bounds Limitedtonumberoflines:numberoflines];Switch (Self.verticalalignment) {Case VERTICALALIGNMENTTOP:TEXTRECT.ORIGIN.Y = BOUNDS.ORIGIN.Y;break; case verticalalignmentbottom:textrect.origin.y = bounds.origin.y + Bounds.size.height- TextRect.size.height; break; case verticalalignmentmiddle: //Fall through. DEFAULT:TEXTRECT.ORIGIN.Y = bounds.origin.y + (bounds.size.height-textrect.size.height)/2.0; } return Textrect;} -(void) Drawtextinrect: (cgrect) requestedrect {cgrect actualrect = [self textrectforbounds:requestedrect Limitedtonumberoflines:self.numberoflines]; [super Drawtextinrect:actualrect];} @end
Use: First remember to introduce the header file
import "MYLabel.h"
- (void) Viewdidload {[Super Viewdidload]; [Self Setuplabel];Do any additional setup after loading the view, typically from a nib.} - (void) Setuplabel {Align MYLabel *toplabel = [[MYLabel alloc] initWithFrame:CGRectMake (20,275,350,200)]; Toplabel.text =@ "The ending, the ending, the ending, the ending, the ending, the ending, the ending,"; Toplabel.backgroundcolor = [Uicolor Cyancolor]; Toplabel.textalignment =Nstextalignmentleft; Toplabel.textcolor = [Uicolor Bluecolor]; Toplabel.linebreakmode =nslinebreakbycharwrapping; Toplabel.numberoflines =0; [Toplabel Setverticalalignment:verticalalignmentmiddle]; [Self.view Addsubview:toplabel];Center-aligned MYLabel *middlelabel = [[MYLabel alloc] initWithFrame:CGRectMake (20,500,350,200)]; Middlelabel.text =@ "Look down, look down, look down, look down, look down, look down, look down, look down,"; Middlelabel.backgroundcolor = [Uicolor Cyancolor]; Middlelabel.textalignment =Nstextalignmentleft; Middlelabel.textcolor = [Uicolor Bluecolor]; Middlelabel.linebreakmode =nslinebreakbycharwrapping; Middlelabel.numberoflines =0; [Middlelabel Setverticalalignment:verticalalignmentbottom]; [self.view Addsubview:middlelabel]; //under alignment MYLabel *bottomlabel = [[MYLabel alloc] Initwithframe: CGRectMake (20, 50, 350, 200)]; Bottomlabel.text = @ "Look at my home alignment ah, you see right ah, seems to be right"; bottomlabel.backgroundcolor = [uicolor Cyancolor]; Bottomlabel.textalignment = nstextalignmentleft; bottomlabel.textcolor = [uicolor Bluecolor]; Bottomlabel.linebreakmode = nslinebreakbycharwrapping; bottomLabel.numberOfLines = Span class= "Hljs-number" >0; [Bottomlabel Setverticalalignment:verticalalignmenttop]; [self.view Addsubview:bottomlabel];}
Such as: Will not upload the
Demo Location GitHub Location
iOS Uilabel settings are aligned, centered, aligned