也不知道为什么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. entity framework extended library , bulk execute,deleting and updating ,opensource

    http://weblogs.asp.net/pwelter34/entity-framework-batch-update-and-future-queries

  2. 解决win8 plsql无法登录

    今天安装完oracle客户端,然后打开 plsql 之后,就一个提示框,提示没有登录,后来解决方法如下: 在plsql的图标上点右键,以管理员身份运行,即可! 如果不想一直点右键执行,就图标上点右键- ...

  3. 中文输入法在vs2010中失效解决方案

    这样你就可以用切换输入法的方式,输入中文咯.     后来用了2次发现还是有问题,后来我就直接把输入法的切换改成ctrl+1,后来使用就一直没有问题.总之,解决方案视具体情况解决.

  4. ANDROID_MARS学习笔记_S01原始版_012_广播机制一

    一.简介 二.代码1.xml(1)activity_main.xml <?xml version="1.0" encoding="utf-8"?> ...

  5. c while 循环

    c代码: #include <stdio.h> int main(void){ unsigned long sum=1UL; unsigned int j=1U; unsigned ; p ...

  6. Android 内核初识(8)Binder

    简介 Binder是Android系统提供的一种IPC(进程间通信)机制.由于Android是基于Linux内核的,因此,除了Binder外,还存在其他的IPC机制,例如管道和socket等.Bind ...

  7. 【POJ】1054 The Troublesome Frog

    题目是非常经典的搜索+剪枝.题意简言之就是,青蛙需要沿着直线踩着踏点通过田地,并且踏点需要至少为3.问哪条路径青蛙踩坏的作物最多.很好的一个条件是青蛙每次移动都是等间距的.题目需要注意将其排序. #i ...

  8. 【HDOJ】1003 Max Sum

    最开始使用递归DP解,stack overflow.化简了一些,复杂度为O(n)就过了. #include <stdio.h> int main() { int case_n, n; in ...

  9. WCF - Consuming WCF Service

    WCF services allow other applications to access or consume them. A WCF service can be consumed by ma ...

  10. bzoj1492

    好题+神题,首先肯定是dp,我们设f[i]为到第i天能获得的最多的B卷(设获得的钱数亦可)由题目hint可知,要么全买要么全卖,我们有f[i]=max(maxmoney,f[j]*b[i]+f[j]* ...