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模拟器上看到的效果如下图:

这显然不是我们要的效果,我们再添加一行代码:
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];
UILabel文本垂直顶部对齐的方法的更多相关文章
- IOS UI篇—UILabel的文字顶部对齐
UILabel的文字顶部对齐 NOV 20TH, 2011 默认UILabel是垂直居中对齐的,如果你的UILabel高度有多行,当内容少的时候,会自动垂直居中. 如下图所示(图片来自stackove ...
- UILabel顶部对齐解决方法(转载)
问题 我有一个UILabel高度最多能显示两行,如果里面内容只有一行,它是垂直居中的.怎么能让它顶端对齐呢? 回答 答案1:用sizeToFit改变UILabel的高度 nevan king,19 ...
- 让UILabel的文字顶部对齐
参考资料 http://stackoverflow.com/questions/1054558/how-do-i-vertically-align-text-within-a-uilabel 方法一 ...
- 一个解决表单中的文字和文本区域(textarea)上对齐的方法
在进行表单布局的时候通常会遇到这样的情况 文本和textarea标签是底部对齐的 <p><em>邮箱</em><textarea style='height: ...
- 【msdn wpf forum翻译】TextBox中文本 中对齐 的方法
原文:[msdn wpf forum翻译]TextBox中文本 中对齐 的方法 原文链接:http://social.msdn.microsoft.com/Forums/en-US/wpf/threa ...
- [C#] StringFormat详解之文本方向、对齐
在使用GDI方式处理文本时,往往会用到StringFormat.里面的某些点有点反直觉,不够直观,所以本篇就通过图文的方式去讲解一下. 本篇内容仅涉及到文本方向.对齐的相关内容. 如有错误.不妥之处, ...
- js常用返回网页顶部几种方法
一.使用锚标记 此方法最简单,只需在body下放个隐藏的锚点标记,内容如下: 代码如下 复制代码 <a name="top" id="top">& ...
- HTML DIV中文字自动换行 , 顶部对齐
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...
- CCS实现input和img水平对齐的方法
在网页制作中,常将 input 和 img 放在同一行,img标签总是比input高出一个头,非常难看. CCS实现input和img水平对齐的方法 同时给input和img添加vertical-al ...
随机推荐
- 【一起学OpenFOAM】04 OpenFOAM的学习资源
OpenFOAM的学习资料并不多,个人猜测也许是与软件的类型有关系. 对于商用软件来讲,由于要占领市场,软件开发商自然是巴不得会用软件的人越多越好,因为他们卖的是软件,会用的人越多,软件卖得越好.他们 ...
- HTML5下通过response header解决跨域AJAX cookie的问题
ajax: 通过给Response Header添加Access-Control-Allow-Origin:* 来解决跨域请求,*代表允许所有的跨域请求,或者把*换成指定的域名 cookie: 服务 ...
- maven编译的时候排除junit测试类
maven项目中使用junit进行单元测试,在进行编译的时候,可以通过2种方式排除test测试类的编译. 有2种方式 : 使用命令的时候带上参数 mvn install -Dmaven.test.sk ...
- 【产品体验】eyepetizer开眼
第一次写博客,内心还有点小激动呢~~本人产品新人,学习中,希望大家多多指教! 先来两张开眼的界面图坐镇—— 开眼简介: appetizer for eyes 即 eyepetizer ...
- Hadoop Datanode节点无法启动(All directories in dfs.data.dir are invalid)
Hadoop Datanode节点无法启动(All directories in dfs.data.dir are invalid) java.io.IOException: All director ...
- Logback 将日志分级别打印
最近项目中用到了logback 记录日志, 关于为啥使用logback 请百度一下: logback与Log4J的区别 好了,废话不多说,直奔主题, 研究了好久,终于将日志按级别将日志分文件打印出 ...
- csdn搜索技巧
由于CSDN下载资源高级搜索,只能显示10页分页,所以我们换种方法去搜索csdn的资源. 第10页以后没了. 在百度框里面输入如下: jquery site:download.csdn.net 图:
- 关于JDNI、JMX
http://www.cnblogs.com/itech/archive/2010/09/16/1827999.html http://javacrazyer.iteye.com/blog/75948 ...
- 从ng-repeat到NgFor
看这篇文章基本明白怎么渲染模板,但是我的工程会报错说#号非法,这篇的写法也不好用. angular2.0.0的语法集: Angular for TypeScript 语法快速指南 (基于2.0.0版本 ...
- 如何设置、查看以及调试core文件
http://blog.csdn.net/xiaoxiaoniaoer1/article/details/7740820 1.core文件的生成开关和大小限制--------------------- ...