也不知道为什么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. ACM-进阶之路

    ACM进阶计划 ACM队不是为了一场比赛而存在的,为的是队员的整体提高. 大学期间,ACM队队员必须要学好的课程有: l C/C++两种语言 l 高等数学 l 线性代数 l 数据结构 l 离散数学 l ...

  2. shell学习总结之自定义函数

    shell学习总结之自定义函数 Myfun (){ echo -n "now i is $i " ! [ "$i" ] && exit ; ec ...

  3. BIOS

    转自BIOS BIOS(Basic Input/Output System的缩写.中文:基本输入输出系统),在IBM PC兼容机上,是一种业界标准的固件接口.BIOS这个字眼是在1975第一次由CP/ ...

  4. php抓取页面的几种方法详解

    本篇文章是对php抓取页面的几种方法进行了详细的分析介绍,需要的朋友参考下 在 做一些天气预报或者RSS订阅的程序时,往往需要抓取非本地文件,一般情况下都是利用php模拟浏览器的访问,通过http请求 ...

  5. linux zip 命令详解

    功能说明:压缩文件. 语 法:zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][-b <工作目录>][-ll][-n <字尾字符串>][-t <日期时 ...

  6. Excel Cannot Connect to SharePoint List

    As I am working in SharePoint support, I come across so many issues on day 2 day basis and always tr ...

  7. Introducing RecyclerView(一)

    RecyclerView 是Android L版本中新添加的一个用来取代ListView的SDK,它的灵活性与可替代性比listview更好.接下来通过一系列的文章讲解如何使用RecyclerView ...

  8. Linux内核中流量控制

    linux内核中提供了流量控制的相关处理功能,相关代码在net/sched目录下:而应用层上的控制是通过iproute2软件包中的tc来实现, tc和sched的关系就好象iptables和netfi ...

  9. 【HDOJ】1171 Big Event in HDU

    母函数,先要算搞清楚组合数可能的最大值.非常大.N种设备的最大VAL*最大数量. #include <stdio.h> #include <string.h> #define ...

  10. 先贴出代码C++ 中的单例模式

    先贴出代码,代码后面是讲解 自己编写的单例模式: #include <iostream> #include <stdio.h> #include <string> ...