iOS UILabel設定居上對齊,置中對齊,居下對齊

來源:互聯網
上載者:User

標籤:track   views   clipboard   bsp   cpp   mod   ica   ott   raw   

在iOS中預設的UILabel中的文字在豎直方向上僅僅能置中對齊,博主參考國外網站。從UILabel繼承了一個新類,實現了居上對齊,置中對齊,居下對齊。詳細例如以下:

[cpp] view plaincopy
  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  

[cpp] view plaincopy
  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  

在使用時:

[cpp] view plaincopy
  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]; 

iOS UILabel設定居上對齊,置中對齊,居下對齊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.