The text in the default Uilabel in iOS can be aligned vertically in the vertical direction, and I inherit a new class from Uilabel, enabling alignment, alignment, and alignment. Specific as follows:
1. Create a new class VerticalAlignmentLabel.h inherit from Uilabel
2.
//
VerticalAlignmentLabel.h
Inface
//
Created by Huangzengsong on 15/5/10.
Copyright (c) 2015 Huangzs. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef enum
{
Verticalalignmenttop = 0,//default
Verticalalignmentmiddle,
Verticalalignmentbottom,
} VerticalAlignment;
@interface Verticalalignmentlabel:uilabel
{
@private
VerticalAlignment _verticalalignment;
}
@property (nonatomic) VerticalAlignment verticalalignment;
@end
3.
//
verticalalignmentlabel.m
Inface
//
Created by Huangzengsong on 15/5/10.
Copyright (c) 2015 Huangzs. All rights reserved.
//
#import "VerticalAlignmentLabel.h"
@implementation Verticalalignmentlabel
@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];
}
/*
Only override Drawrect:if perform custom drawing.
An empty implementation adversely affects performance during animation.
-(void) DrawRect: (cgrect) Rect {
Drawing Code
}
*/
@end
4. Importing header files when calling
#import "VerticalAlignmentLabel.h"
5.
[Self. Detaillabel Setverticalalignment:verticalalignmenttop];
iOS Uilabel settings are aligned, centered, aligned