In iOS, the default Uilabel text in the vertical direction can only center alignment, Bo Master to participate in foreign sites. Inherits a new class from the Uilabel, realizes the alignment, the center aligns, the bottom aligns. Details such as the following:
[CPP]View Plaincopy
- //
- //MyUILabel.h
- //
- //
- //Created by yexiaozi_007 on 3/4/13.
- //Copyright (c) yexiaozi_007. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- typedef enum
- {
- Verticalalignmenttop = 0, //default
- Verticalalignmentmiddle,
- Verticalalignmentbottom,
- } VerticalAlignment;
- @interface Myuilabel:uilabel
- {
- @Private
- VerticalAlignment _verticalalignment;
- }
- @property (nonatomic) VerticalAlignment verticalalignment;
- @end
[CPP]View Plaincopy
- //
- //MYUILABEL.M
- //
- //
- //Created by yexiaozi_007 on 3/4/13.
- //Copyright (c) yexiaozi_007. All rights reserved.
- //
- #import "MyUILabel.h"
- @implementation Myuilabel
- @synthesize verticalalignment = Verticalalignment_;
- -(ID) initWithFrame: (CGRect) Frame {
- if (self = [Super Initwithframe:frame]) {
- Self.verticalalignment = Verticalalignmentmiddle;
- }
- return Self ;
- }
- -(void) setverticalalignment: (verticalalignment) VerticalAlignment {
- Verticalalignment_ = VerticalAlignment;
- [Self setneedsdisplay];
- }
- -(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
When in use:
[CPP]View Plaincopy
- Lbl_mylabel = [[Myuilabel alloc] Initwithframe:cgrectmake (20, 50, 150, 600)];
- Uicolor *color = [Uicolor colorwithpatternimage:[uiimage imagenamed:@"Halftransparent.png"]]; //Use a translucent picture as the label's background color
- Lbl_mylabel.backgroundcolor = color;
- Lbl_mylabel.textalignment = Uitextalignmentleft;
- Lbl_mylabel.textcolor = Uicolor.whitecolor;
- Lbl_mylabel.linebreakmode = Uilinebreakmodewordwrap;
- Lbl_mylabel.numberoflines = 0;
- [Lbl_mylabel Setverticalalignment:verticalalignmenttop];
- [Self Addsubview:lbl_mylabel];
IOS uilabel settings Align, center, align