iOS⾃定义UILabel可以使⽂字居上居中居下
前⾔:做过iOS 的都知道,UILabel有让⽂字居左,居右,居中的属性,可是有时候我们在做项⽬的时候,可能会需要将label上的⽂字居上, 居中或者 居下这样的操作。那么今天我们就来说⼀说这种效果的实现⽅式。fag
#import <UIKit/UIKit.h>
typedefenum
ertl{
VerticalAlignmentTop =0,// default
美容美发培训学校VerticalAlignmentMiddle,
VerticalAlignmentBottom,
} VerticalAlignment;
@interfaceBaUILabel :UILabel
{
@private
VerticalAlignment_verticalAlignment;
wonderful
}
@property(nonatomic)VerticalAlignmentverticalAlignment;
soup怎么读@end
#import"BaUILabel.h"
@implementationBaUILabel
peace of mind@synthesizeverticalAlignment =verticalAlignment_;
- (id)initWithFrame:(CGRect)frame {
nice是什么中文意思
zoroif(lf= [superinitWithFrame:frame]) {
lf.verticalAlignment=VerticalAlignmentMiddle;
}
returnlf;
}
- (void)tVerticalAlignment:(VerticalAlignment)verticalAlignment {
verticalAlignment_= verticalAlignment;
[lftNeedsDisplay];
pool是什么意思}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines { CGRecttextRect = [supertextRectForBounds:boundslimitedToNumberOfLines:numberOfLines];
switch(lf.verticalAlignment) {
caVerticalAlignmentTop:
break;
caVerticalAlignmentBottom:
break;glbt
caVerticalAlignmentMiddle:
// Fall through.
default:
}
returntextRect;
}
-(void)drawTextInRect:(CGRect)requestedRect {
CGRectactualRect = [lftextRectForBounds:requestedRectlimitedToNumberOfLines:lf.numberOfLines]; [superdrawTextInRect:actualRect];
}
@end
这样我们在使⽤的时候,只要继承这个label类,然后选择想要实现效果的属性即可