文字居上显示的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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,235评论 1 6
  • 1、禁止手机睡眠 [UIApplication sharedApplication].idleTimerDisab...
    FF_911阅读 1,518评论 0 3
  • 每天最开心的时候应该是下班骑着摩拜回家的时候吧 骑行在陌生的环境 身边的一切都与我无关 我没有身份 不是公司的员工...
    _嗡_阅读 412评论 0 0
  • 可能是柯以敏的美丽吧。在那个情感萌芽的阶段,也自诩傲娇的时刻,歌词深入我心。也可能是任素汐的我要你,因为电影的女主...
    簡清阅读 152评论 0 1
  • 二乔滴露黄莺语,独立春风日落隅。 团扇携来云几朵,回眸君步伴香徐。 注:新韵,首句平起不入韵。 1、二乔,牡丹花的...
    幽小窗阅读 676评论 27 39

友情链接更多精彩内容