打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
iOS的UILabel设置居上对齐,居中对齐,居下对齐
11032人阅读 评论(1) 收藏 举报

在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,博主参考国外网站,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐。具体如下:

  1. //  
  2. //  myUILabel.h  
  3. //    
  4. //  
  5. //  Created by yexiaozi_007 on 3/4/13.  
  6. //  Copyright (c) 2013 yexiaozi_007. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. typedef enum  
  11. {  
  12.     VerticalAlignmentTop = 0, // default  
  13.     VerticalAlignmentMiddle,  
  14.     VerticalAlignmentBottom,  
  15. } VerticalAlignment;  
  16. @interface myUILabel : UILabel  
  17. {  
  18. @private  
  19. VerticalAlignment _verticalAlignment;  
  20. }  
  21.   
  22. @property (nonatomic) VerticalAlignment verticalAlignment;  
  23.   
  24. @end  

  1. //  
  2. //  myUILabel.m  
  3. //    
  4. //  
  5. //  Created by yexiaozi_007 on 3/4/13.  
  6. //  Copyright (c) 2013 yexiaozi_007. All rights reserved.  
  7. //  
  8.   
  9. #import "myUILabel.h"  
  10.   
  11. @implementation myUILabel  
  12. @synthesize verticalAlignment = verticalAlignment_;  
  13.   
  14. - (id)initWithFrame:(CGRect)frame {  
  15.     if (self = [super initWithFrame:frame]) {  
  16.         self.verticalAlignment = VerticalAlignmentMiddle;  
  17.     }  
  18.     return self;  
  19. }  
  20.   
  21. - (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {  
  22.     verticalAlignment_ = verticalAlignment;  
  23.     [self setNeedsDisplay];  
  24. }  
  25.   
  26. - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {  
  27.     CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];  
  28.     switch (self.verticalAlignment) {  
  29.         case VerticalAlignmentTop:  
  30.             textRect.origin.y = bounds.origin.y;  
  31.             break;  
  32.         case VerticalAlignmentBottom:  
  33.             textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;  
  34.             break;  
  35.         case VerticalAlignmentMiddle:  
  36.             // Fall through.  
  37.         default:  
  38.             textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;  
  39.     }  
  40.     return textRect;  
  41. }  
  42.   
  43. -(void)drawTextInRect:(CGRect)requestedRect {  
  44.     CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];  
  45.     [super drawTextInRect:actualRect];  
  46. }  
  47.   
  48.   
  49. @end  

在使用时:

  1. lbl_mylabel = [[myUILabel alloc] initWithFrame:CGRectMake(20, 50, 150, 600)];  
  2. UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"halfTransparent.png"]];//使用半透明图片作为label的背景色  
  3. lbl_mylabel.backgroundColor = color;  
  4. lbl_mylabel.textAlignment = UITextAlignmentLeft;  
  5. lbl_mylabel.textColor = UIColor.whiteColor;  
  6. lbl_mylabel.lineBreakMode = UILineBreakModeWordWrap;  
  7. lbl_mylabel.numberOfLines = 0;  
  8. [lbl_mylabel setVerticalAlignment:VerticalAlignmentTop];  
  9. [self addSubview:lbl_mylabel];  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
[iOS]UILabel取消自动Trim空格和换行等空白字符
提高设计还原度!写给设计师的iOS前端教程(二)
动态添加UILabel
UILabel的常见用法
iOS 在UILabel显示不同的字体和颜色
iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服