UILabel文本垂直頂部對齊的方法


也不知道為什么UILabel本身沒有提供文本垂直頂部對齊的方法,真的有點暈。我們創建一個簡單的UILabel來看看:

[box type="info"]

UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 100)];

[myLabel setText:@"蘋果iOS(iphone Operation System)是由蘋果公司開發的手持設備操作系統。蘋果公司最早於2007年1月9日的Macworld大會上公布這個系統,最初是設計給iPhone使用的"];

[myLabel setFont:[UIFont fontWithName:@"Arial" size:14.0]];

[myLabel setBackgroundColor:[UIColor blueColor]];

[myLabel setTextColor:[UIColor whiteColor]];

[self.view addSubview:myLabel];

[/box]

在ios模擬器上看到的效果如下圖:

Screenshot_4

 

這顯然不是我們要的效果,我們再添加一行代碼:

myLabel.numberOfLines = 0;

結果是:

Screenshot_5

 

顯示有點正常了,但如何使文字頂部對齊呢?

實現代碼:

[box type="info"]

UILabel *myLabel = [[UILabel alloc]init];//WithFrame:CGRectMake(10, 10, 300, 100)];

 

UIFont *strFont = [UIFont fontWithName:@"Arial" size:14.0];

 

[myLabel setFont:strFont];

[myLabel setBackgroundColor:[UIColor blueColor]];

[myLabel setTextColor:[UIColor whiteColor]];

myLabel.numberOfLines = 0;

 

CGSize maximumSize = CGSizeMake(300, 999);

NSString *string = @”蘋果iOS(iphone Operation System)是由蘋果公司開發的手持設備操作系統。蘋果公司最早於2007年1月9日的Macworld大會上公布這個系統,最初是設計給iPhone使用的”;

CGSize stringSize = [string sizeWithFont:strFont

constrainedToSize:maximumSize

lineBreakMode:myLabel.lineBreakMode];

[myLabel setText:string];

CGRect strFrame = CGRectMake(10, 10, 300, stringSize.height);

myLabel.frame = strFrame;

[self.view addSubview:myLabel];

 

[/box]

運行結果如下圖:

Screenshot_6

 

盆友們,如果有更好的方法使UILabel垂直頂部對齊,一定要分享出來哦

 

在iOS中默認的UILabel中的文字在豎直方向上只能居中對齊,從UILabel繼承了一個新類,實現了居上對齊,居中對齊,居下對齊。具體如下:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM