也不知道为什么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模拟器上看到的效果如下图:
这显然不是我们要的效果,我们再添加一行代码:
myLabel.numberOfLines = 0;
结果是:
显示有点正常了,但如何使文字顶部对齐呢?
实现代码:
[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]
运行结果如下图:
盆友们,如果有更好的方法使UILabel垂直顶部对齐,一定要分享出来哦
在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐。具体如下:
- //
- // myUILabel.h
- //
- //
- // Created by yexiaozi_007 on 3/4/13.
- // Copyright (c) 2013 yexiaozi_007. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- typedef enum
- {
- VerticalAlignmentTop = 0, // default
- VerticalAlignmentMiddle,
- VerticalAlignmentBottom,
- } VerticalAlignment;
- @interface myUILabel : UILabel
- {
- @private
- VerticalAlignment _verticalAlignment;
- }
- @property (nonatomic) VerticalAlignment verticalAlignment;
- @end
- //
- // myUILabel.m
- //
- //
- // Created by yexiaozi_007 on 3/4/13.
- // Copyright (c) 2013 yexiaozi_007. All rights reserved.
- //
- #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:
- // Fall through.
- 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"]];//使用半透明图片作为label的背景色
- 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];