iOS中UILabel如何设置居上对齐,居中对齐,居下对齐及文字置顶显示

  介绍

这篇文章主要为大家展示了“iOS中UILabel如何设置居上对齐,居中对齐,居下对齐及文字置顶显示”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“iOS中UILabel如何设置居上对齐,居中对齐,居下对齐及文字置顶显示”这篇文章吧。

<强> iOS中UILabel设置居上对齐,居中对齐,居下对齐

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

<强>具体如下:

//,//,myUILabel.h //,//,//,Created  by  yexiaozi_007 提醒3/4/13又是;//,Copyright  (c), 2013年,yexiaozi_007只All  rights 只保留;//,   # import  & lt; UIKit/UIKit.h>,   typedef  enum    {,=,VerticalAlignmentTop  0,,//, default    VerticalAlignmentMiddle,大敌;   VerticalAlignmentBottom,大敌;   },VerticalAlignment;,   @interface  myUILabel : UILabel    {,   @private    VerticalAlignment  _verticalAlignment,   },   @property (原子),VerticalAlignment  verticalAlignment;,   @end //,//,myUILabel.m //,//,//,Created  by  yexiaozi_007 提醒3/4/13又是;//,Copyright  (c), 2013年,yexiaozi_007只All  rights 只保留;//,   # import “myUILabel.h",   @implementation  myUILabel    @synthesize  verticalAlignment =时间verticalAlignment_;大敌;;   ,   安康;(id) initWithFrame:(CGRect中)frame  {,   ,if  (self =, [super  initWithFrame:框架]),{,=,,self.verticalAlignment  VerticalAlignmentMiddle;,   }大敌;   ,return 自我;   },   安康;(空白)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;,   ,,休息;   case  VerticalAlignmentBottom:大敌;   textRect.origin.y 才能=,bounds.origin.y  +, bounds.size.height 作用;textRect.size.height;,   ,,休息;   case  VerticalAlignmentMiddle:大敌;//才能,Fall 通过又是;   默认值:大敌;   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"]];//使用半透明图片作为标签的背景色,   lbl_mylabel.backgroundColor =,颜色,,   时间=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让文字置顶显示

我们经常会遇到将标签中文字置顶,也就是将文字顶到标签框的最顶端显示的需求,UILabel是无法对内容文字进行置顶处理的,所以,如果我们不加对标签以额外的设置,就会出现如下情况:

 iOS中UILabel如何设置居上对齐,居中对齐,居下对齐及文字置顶显示

置顶前<强>

解决办法:我们可以通过sizeToFit方法解决它:

安康;(空白)viewDidLoad  {   ,[super  viewDidLoad];   ,UILabel  * label =, [[UILabel  alloc], initWithFrame: CGRectMake ((self.view.bounds.size.width 作用;200)/2,,100,,200,,150));=,label.backgroundColor  [UIColor  yellowColor];   ,NSString  * labelText =, @"我不知道如何置顶,谁来告诉我?“;   ,[label  setText labelText):;   ,[label  setNumberOfLines: 0];,,   ,//让内容置顶   ,[label  sizeToFit];   ,(self.view  addSubview:标签);   null

iOS中UILabel如何设置居上对齐,居中对齐,居下对齐及文字置顶显示