也不知道为什么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继承了一个新类,实现了居上对齐,居中对齐,居下对齐。具体如下:

  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. #import <UIKit/UIKit.h>
  9. typedef enum
  10. {
  11. VerticalAlignmentTop = 0, // default
  12. VerticalAlignmentMiddle,
  13. VerticalAlignmentBottom,
  14. } VerticalAlignment;
  15. @interface myUILabel : UILabel
  16. {
  17. @private
  18. VerticalAlignment _verticalAlignment;
  19. }
  20. @property (nonatomic) VerticalAlignment verticalAlignment;
  21. @end
  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. #import "myUILabel.h"
  9. @implementation myUILabel
  10. @synthesize verticalAlignment = verticalAlignment_;
  11. - (id)initWithFrame:(CGRect)frame {
  12. if (self = [super initWithFrame:frame]) {
  13. self.verticalAlignment = VerticalAlignmentMiddle;
  14. }
  15. return self;
  16. }
  17. - (void)setVerticalAlignment:(VerticalAlignment)verticalAlignment {
  18. verticalAlignment_ = verticalAlignment;
  19. [self setNeedsDisplay];
  20. }
  21. - (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
  22. CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
  23. switch (self.verticalAlignment) {
  24. case VerticalAlignmentTop:
  25. textRect.origin.y = bounds.origin.y;
  26. break;
  27. case VerticalAlignmentBottom:
  28. textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height;
  29. break;
  30. case VerticalAlignmentMiddle:
  31. // Fall through.
  32. default:
  33. textRect.origin.y = bounds.origin.y + (bounds.size.height - textRect.size.height) / 2.0;
  34. }
  35. return textRect;
  36. }
  37. -(void)drawTextInRect:(CGRect)requestedRect {
  38. CGRect actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
  39. [super drawTextInRect:actualRect];
  40. }
  41. @end

在使用时:

  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];

UILabel文本垂直顶部对齐的方法的更多相关文章

  1. IOS UI篇—UILabel的文字顶部对齐

    UILabel的文字顶部对齐 NOV 20TH, 2011 默认UILabel是垂直居中对齐的,如果你的UILabel高度有多行,当内容少的时候,会自动垂直居中. 如下图所示(图片来自stackove ...

  2. UILabel顶部对齐解决方法(转载)

    问题 我有一个UILabel高度最多能显示两行,如果里面内容只有一行,它是垂直居中的.怎么能让它顶端对齐呢?   回答 答案1:用sizeToFit改变UILabel的高度 nevan king,19 ...

  3. 让UILabel的文字顶部对齐

    参考资料 http://stackoverflow.com/questions/1054558/how-do-i-vertically-align-text-within-a-uilabel 方法一 ...

  4. 一个解决表单中的文字和文本区域(textarea)上对齐的方法

    在进行表单布局的时候通常会遇到这样的情况 文本和textarea标签是底部对齐的 <p><em>邮箱</em><textarea style='height: ...

  5. 【msdn wpf forum翻译】TextBox中文本 中对齐 的方法

    原文:[msdn wpf forum翻译]TextBox中文本 中对齐 的方法 原文链接:http://social.msdn.microsoft.com/Forums/en-US/wpf/threa ...

  6. [C#] StringFormat详解之文本方向、对齐

    在使用GDI方式处理文本时,往往会用到StringFormat.里面的某些点有点反直觉,不够直观,所以本篇就通过图文的方式去讲解一下. 本篇内容仅涉及到文本方向.对齐的相关内容. 如有错误.不妥之处, ...

  7. js常用返回网页顶部几种方法

    一.使用锚标记 此方法最简单,只需在body下放个隐藏的锚点标记,内容如下:  代码如下 复制代码 <a name="top" id="top">& ...

  8. HTML DIV中文字自动换行 , 顶部对齐

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...

  9. CCS实现input和img水平对齐的方法

    在网页制作中,常将 input 和 img 放在同一行,img标签总是比input高出一个头,非常难看. CCS实现input和img水平对齐的方法 同时给input和img添加vertical-al ...

随机推荐

  1. 【一起学OpenFOAM】04 OpenFOAM的学习资源

    OpenFOAM的学习资料并不多,个人猜测也许是与软件的类型有关系. 对于商用软件来讲,由于要占领市场,软件开发商自然是巴不得会用软件的人越多越好,因为他们卖的是软件,会用的人越多,软件卖得越好.他们 ...

  2. HTML5下通过response header解决跨域AJAX cookie的问题

    ajax: 通过给Response Header添加Access-Control-Allow-Origin:*  来解决跨域请求,*代表允许所有的跨域请求,或者把*换成指定的域名 cookie: 服务 ...

  3. maven编译的时候排除junit测试类

    maven项目中使用junit进行单元测试,在进行编译的时候,可以通过2种方式排除test测试类的编译. 有2种方式 : 使用命令的时候带上参数 mvn install -Dmaven.test.sk ...

  4. 【产品体验】eyepetizer开眼

    第一次写博客,内心还有点小激动呢~~本人产品新人,学习中,希望大家多多指教!  先来两张开眼的界面图坐镇——         开眼简介: appetizer for eyes 即 eyepetizer ...

  5. 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 ...

  6. Logback 将日志分级别打印

    最近项目中用到了logback 记录日志,  关于为啥使用logback 请百度一下:  logback与Log4J的区别 好了,废话不多说,直奔主题, 研究了好久,终于将日志按级别将日志分文件打印出 ...

  7. csdn搜索技巧

    由于CSDN下载资源高级搜索,只能显示10页分页,所以我们换种方法去搜索csdn的资源. 第10页以后没了. 在百度框里面输入如下: jquery site:download.csdn.net 图:

  8. 关于JDNI、JMX

    http://www.cnblogs.com/itech/archive/2010/09/16/1827999.html http://javacrazyer.iteye.com/blog/75948 ...

  9. 从ng-repeat到NgFor

    看这篇文章基本明白怎么渲染模板,但是我的工程会报错说#号非法,这篇的写法也不好用. angular2.0.0的语法集: Angular for TypeScript 语法快速指南 (基于2.0.0版本 ...

  10. 如何设置、查看以及调试core文件

    http://blog.csdn.net/xiaoxiaoniaoer1/article/details/7740820 1.core文件的生成开关和大小限制--------------------- ...