亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS中UILabel設(shè)置居上對齊、居中對齊、居下對齊及文字置頂顯示

 更新時(shí)間:2017年12月17日 10:30:35   作者:J_Knight_  
這篇文章主要給大家介紹了關(guān)于iOS中UILabel如何設(shè)置居上對齊、居中對齊、居下對齊及文字置頂顯示效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

iOS中UILabel設(shè)置居上對齊、居中對齊、居下對齊

在iOS中默認(rèn)的UILabel中的文字在豎直方向上只能居中對齊,博主參考國外網(wǎng)站,從UILabel繼承了一個新類,實(shí)現(xiàn)了居上對齊,居中對齊,居下對齊。

具體如下:

// 
// myUILabel.h 
// 
// 
// Created by yexiaozi_007 on 3/4/13. 
// Copyright (c) 2013 yexiaozi_007. All rights reserved. 
// 
#import <UIKit/UIKit.h> 
typedef enum 
{ 
 VerticalAlignmentTop = 0, // default 
 VerticalAlignmentMiddle, 
 VerticalAlignmentBottom, 
} VerticalAlignment; 
@interface myUILabel : UILabel 
{ 
@private 
VerticalAlignment _verticalAlignment; 
} 
@property (nonatomic) VerticalAlignment verticalAlignment; 
@end 
// 
// myUILabel.m 
// 
// 
// Created by yexiaozi_007 on 3/4/13. 
// Copyright (c) 2013 yexiaozi_007. All rights reserved. 
// 
#import "myUILabel.h" 
@implementation myUILabel 
@synthesize verticalAlignment = verticalAlignment_; 
 
- (id)initWithFrame:(CGRect)frame { 
 if (self = [super initWithFrame:frame]) { 
 self.verticalAlignment = VerticalAlignmentMiddle; 
 } 
 return self; 
} 
- (void)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; 
  break; 
 case VerticalAlignmentBottom: 
  textRect.origin.y = bounds.origin.y + bounds.size.height - textRect.size.height; 
  break; 
 case VerticalAlignmentMiddle: 
  // Fall through. 
 default: 
  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 

在使用時(shí):

lbl_mylabel = [[myUILabel alloc] initWithFrame:CGRectMake(20, 50, 150, 600)]; 
UIColor *color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"halfTransparent.png"]];//使用半透明圖片作為label的背景色 
lbl_mylabel.backgroundColor = color; 
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 讓文字置頂顯示

我們經(jīng)常會遇到將Label中文字置頂,也就是將文字頂?shù)絃able框的最頂端顯示的需求,UILabel是無法對內(nèi)容文字進(jìn)行置頂處理的,所以,如果我們不對Label加以額外的設(shè)置,就會出現(xiàn)如下情況:

置頂前

解決辦法:我們可以通過sizeToFit方法解決它:

- (void)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];  
 //讓內(nèi)容置頂
 [label sizeToFit];
 [self.view addSubview:label];
}

效果圖:

置頂后

但是有些小伙伴會對內(nèi)容置頂后的Label的frame有些顧慮,筆者也有,所以就在Label后方放置了一個和初始Label具有相同frame的紅色背景,那么如果設(shè)置sizeToFit方法后,即使Label的frame有變化,我們也可以通過和紅色背景的frame相對比而看出:

置頂前后frame對比

我們可以看到,文字內(nèi)容置頂后,原Label的origin幾乎沒有變化,而bounds適應(yīng)了文字,大小改變了。
所以不難看出,通過sizeToFit方法,我們可以將Label的大小“剛好”緊貼文字外部,從而實(shí)現(xiàn)了置頂?shù)男Ч?/p>

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論