UILabel 多行文字自动换行 (自动折行)


1.UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 300, 180)];   
2. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 150)];
3. label.text = @"where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you? where are you?";
4. //清空背景颜色
5. label.backgroundColor = [UIColor clearColor];
6. //设置字体颜色为白色
7. label.textColor = [UIColor whiteColor];
8. //文字居中显示
9. label.textAlignment = UITextAlignmentCenter;
10. //自动折行设置
11. label.lineBreakMode = UILineBreakModeWordWrap;
12. label.numberOfLines = 0;



​iOS的UILabel设置居上对齐,居中对齐,居下对齐​

在iOS中默认的UILabel中的文字在竖直方向上只能居中对齐,博主参考国外网站,从UILabel继承了一个新类,实现了居上对齐,居中对齐,居下对齐。具体如下:

[cpp]​view plain​​​​copy​


  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



[cpp]​view plain​​​​copy​


  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



在使用时:


[cpp]​view plain​​​​copy​


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



ios UILabel 变量名不能为title

-[UILabel copyWithZone:]: unrecognized selector sent to instance

遇到了这样一个错误,找了半天没找到是什么错误,于是,Google搜索,打开第一个链接http://stackoverflow.com/questions/10784207/uilabel-copywithzone-unrecognized-selector-sent-to-instance UILabel 设置过长文本中间为省略号

label.lineBreakMode = NSLineBreakByTruncatingMiddle;