也不知道爲何UILabel自己沒有提供文本垂直頂部對齊的方法,真的有點暈。咱們建立一個簡單的UILabel來看看:ios
[box type="info"]iphone
UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 100)];atom
[myLabel setText:@"蘋果iOS(iphone Operation System)是由蘋果公司開發的手持設備操做系統。蘋果公司最先於2007年1月9日的Macworld大會上公佈這個系統,最初是設計給iPhone使用的"];spa
[myLabel setFont:[UIFont fontWithName:@"Arial" size:14.0]];操作系統
[myLabel setBackgroundColor:[UIColor blueColor]];.net
[myLabel setTextColor:[UIColor whiteColor]];設計
[self.view addSubview:myLabel];blog
[/box]繼承
在ios模擬器上看到的效果以下圖:圖片
![Screenshot_4](http://static.javashuo.com/static/loading.gif)
這顯然不是咱們要的效果,咱們再添加一行代碼:
myLabel.numberOfLines = 0;
結果是:
![Screenshot_5](http://static.javashuo.com/static/loading.gif)
顯示有點正常了,但如何使文字頂部對齊呢?
實現代碼:
[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](http://static.javashuo.com/static/loading.gif)
盆友們,若是有更好的方法使UILabel垂直頂部對齊,必定要分享出來哦
在iOS中默認的UILabel中的文字在豎直方向上只能居中對齊,從UILabel繼承了一個新類,實現了居上對齊,居中對齊,居下對齊。具體以下:
-
- #import <UIKit/UIKit.h>
- typedef enum
- {
- VerticalAlignmentTop = 0,
- VerticalAlignmentMiddle,
- VerticalAlignmentBottom,
- } VerticalAlignment;
- @interface myUILabel : UILabel
- {
- @private
- VerticalAlignment _verticalAlignment;
- }
-
- @property (nonatomic) VerticalAlignment verticalAlignment;
-
- @end
-
- #import "myUILabel.h"
-
- @implementation myUILabel
- @synthesize verticalAlignment = verticalAlignment_;
-
- - (id)initWithFrame:(CGRect)frame {
- if (self = [super initWithFrame:frame]) {
- self.verticalAlignment = VerticalAlignmentMiddle;
- }
- return self;
- }
-
- - (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
- verticalAlignment_ = verticalAlignment;
- [self setNeedsDisplay];
- }
-
- - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
- CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
- switch (self.verticalAlignment) {
- case VerticalAlignmentTop:
- textRect.origin.y = bounds.origin.y;
- break;
- case VerticalAlignmentBottom:
- textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
- break;
- case VerticalAlignmentMiddle:
-
- default:
- textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
- }
- return textRect;
- }
-
- -(void)drawTextInRect:(CGRect)requestedRect {
- CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
- [super drawTextInRect:actualRect];
- }
-
-
- @end
在使用時:
- lbl_mylabel = [[myUILabel alloc] initWithFrame:CGRectMake(20, 50, 150, 600)];
- UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"halfTransparent.png"]];
- lbl_mylabel.backgroundColor = color;
- lbl_mylabel.textAlignment = UITextAlignmentLeft;
- lbl_mylabel.textColor = UIColor.whiteColor;
- lbl_mylabel.lineBreakMode = UILineBreakModeWordWrap;
- lbl_mylabel.numberOfLines = 0;
- [lbl_mylabel setVerticalAlignment:VerticalAlignmentTop];
- [self addSubview:lbl_mylabel];