.h
typedefenum
{
VerticalAlignmentTop =0,// default
VerticalAlignmentMiddle,
VerticalAlignmentBottom,
} VerticalAlignment;
@interfaceMyLabel :UILabel
{
@private
VerticalAlignment_verticalAlignment;
}
@property(nonatomic)VerticalAlignmentverticalAlignment;
@end
.m
@implementationMyLabel
@synthesizeverticalAlignment =verticalAlignment_;
- (id)initWithFrame:(CGRect)frame {
if(self= [superinitWithFrame:frame]) {
self.verticalAlignment=VerticalAlignmentMiddle;
}
returnself;
}
- (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
verticalAlignment_= verticalAlignment;
[selfsetNeedsDisplay];
}
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
CGRecttextRect = [supertextRectForBounds:boundslimitedToNumberOfLines:numberOfLines];
switch(self.verticalAlignment) {
caseVerticalAlignmentTop:
textRect.origin.y= bounds.origin.y;
break;
caseVerticalAlignmentBottom:
textRect.origin.y= bounds.origin.y+ bounds.size.height- textRect.size.height;
break;
caseVerticalAlignmentMiddle:
// Fall through.
default:
textRect.origin.y= bounds.origin.y+ (bounds.size.height- textRect.size.height) /2.0;
}
returntextRect;
}
-(void)drawTextInRect:(CGRect)requestedRect {
CGRectactualRect = [selftextRectForBounds:requestedRectlimitedToNumberOfLines:self.numberOfLines];
[superdrawTextInRect
网友评论