继承UILabel类实现Label竖直对齐

原文地址: http://blog.youkuaiyun.com/sakulafly/article/details/16331037

(别人写的是原创, 但是这个方法网上到处都是,不清楚是否是第一原创了)


  UILabel是个很常见的控件,使用也挺简单的,iOS提供了一系列的设置以方便用户使用——但没竖直方向的对齐。默认的行为是竖直方向自动居中,但有时我们的格式就是要实现竖直的对齐(顶端对齐,底端对齐,中间对齐),虽然有些变通的方法,比如添加\n之类的方法,但仔细考虑觉得还是写一个比较通用的类更方便使用。

     这样一个扩展的继承类实现很简单,直接上代码:

#import <UIKit/UIKit.h>

[html]  view plain copy
  1. typedef enum{  
  2.     VerticalAlignmentTop = 0,  
  3.     VerticalAlignmentMidele,  
  4.     VerticalAlignmentBottom,  
  5.     VerticalAlignmentMax  
  6. }VerticalAlignment;  
  7.   
  8. @interface DPLabel : UILabel  
  9. {  
  10.     VerticalAlignment _verticalAlignment;  
  11. }  
  12.   
  13. @property (nonatomic, assign)VerticalAlignment verticalAlignment;  
  14.   
  15. @end  

[html]  view plain copy
  1. #import "DPLabel.h"  
  2.   
  3. @implementation DPLabel  
  4. @synthesize verticalAlignment;  
  5.   
  6. - (id)initWithFrame:(CGRect)frame  
  7. {  
  8.     self = [super initWithFrame:frame];  
  9.     if (self) {  
  10.         // Initialization code  
  11.         _verticalAlignment = VerticalAlignmentTop;  
  12.     }  
  13.     return self;  
  14. }  
  15.   
  16. - (void)dealloc  
  17. {  
  18.     [super dealloc];  
  19. }  
  20.   
  21. - (VerticalAlignment)verticalAlignment  
  22. {  
  23.     return _verticalAlignment;  
  24. }  
  25.   
  26. - (void)setVerticalAlignment:(VerticalAlignment)align  
  27. {  
  28.     _verticalAlignment = align;  
  29.     [self setNeedsDisplay];  
  30. }  
  31.   
  32. - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines  
  33. {  
  34.     CGRect rc = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];  
  35.     switch (_verticalAlignment) {  
  36.         case VerticalAlignmentTop:  
  37.             rc.origin.y = bounds.origin.y;  
  38.             break;  
  39.         case VerticalAlignmentBottom:  
  40.             rc.origin.y = bounds.origin.y + bounds.size.height - rc.size.height;  
  41.             break;  
  42.         case VerticalAlignmentMidele:  
  43.         default:  
  44.             rc.origin.y = bounds.origin.y + (bounds.size.height - rc.size.height)/2;  
  45.             break;  
  46.     }  
  47.       
  48.     return rc;  
  49. }  
  50.   
  51. - (void)drawTextInRect:(CGRect)rect  
  52. {  
  53.     CGRect rc = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines];  
  54.     [super drawTextInRect:rc];  
  55. }  
  56.   
  57. @end  

      这样我们就实现了自己的Label类,并且具有了一个verticalAlignment的属性。使用起来和UILabel没有什么区别


[html]  view plain copy
  1. testLabel = [[DPLabel alloc] initWithFrame:CGRectMake(0, 200, 320, 60)];  
  2. testLabel.text = @"Hello, DPLabel";  
  3. testLabel.backgroundColor = [UIColor whiteColor];  
  4. testLabel.textAlignment = NSTextAlignmentCenter;  
  5. [self.view addSubview:testLabel];  

        

      这里产生一个新的问题,用IB怎么编辑界面呢?UILabel可以从控件库里面拖过去,DPLabel怎么办呢?答案是设置这个控件的类名,这里就是把选中的控件类名从UILabel改成DPLabel

      

      这样这个功能和UILabel完全兼容,仅增加了竖直对齐的DPLabel继承类就完全可以取代UILabel了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值