美文网首页iOS DeveloperiOS 开发
文字居上显示的label

文字居上显示的label

作者: w佐w | 来源:发表于2016-09-18 17:23 被阅读325次

.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

相关文章

网友评论

    本文标题:文字居上显示的label

    本文链接:https://www.haomeiwen.com/subject/azkgettx.html