文字居上显示的label

.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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,148评论 1 6
  • 1、禁止手机睡眠 [UIApplication sharedApplication].idleTimerDisab...
    FF_911阅读 1,432评论 0 3
  • 每天最开心的时候应该是下班骑着摩拜回家的时候吧 骑行在陌生的环境 身边的一切都与我无关 我没有身份 不是公司的员工...
    _嗡_阅读 377评论 0 0
  • 人是群居动物,做什么事都得有伴才有动力,才能做的好,至少不会做着做便失了兴趣。 写作也是如此,姑且算是写作吧,用我...
    古籍善本阅读 389评论 0 0
  • 可能是柯以敏的美丽吧。在那个情感萌芽的阶段,也自诩傲娇的时刻,歌词深入我心。也可能是任素汐的我要你,因为电影的女主...
    簡清阅读 125评论 0 1