iOS中UILabel設(shè)置居上對(duì)齊、居中對(duì)齊、居下對(duì)齊及文字置頂顯示
iOS中UILabel設(shè)置居上對(duì)齊、居中對(duì)齊、居下對(duì)齊
在iOS中默認(rèn)的UILabel中的文字在豎直方向上只能居中對(duì)齊,博主參考國外網(wǎng)站,從UILabel繼承了一個(gè)新類,實(shí)現(xiàn)了居上對(duì)齊,居中對(duì)齊,居下對(duì)齊。
具體如下:
//
// 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)常會(huì)遇到將Label中文字置頂,也就是將文字頂?shù)絃able框的最頂端顯示的需求,UILabel是無法對(duì)內(nèi)容文字進(jìn)行置頂處理的,所以,如果我們不對(duì)Label加以額外的設(shè)置,就會(huì)出現(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];
}
效果圖:

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

置頂前后frame對(duì)比
我們可以看到,文字內(nèi)容置頂后,原Label的origin幾乎沒有變化,而bounds適應(yīng)了文字,大小改變了。
所以不難看出,通過sizeToFit方法,我們可以將Label的大小“剛好”緊貼文字外部,從而實(shí)現(xiàn)了置頂?shù)男Ч?/p>
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- iOS基于 UILabel實(shí)現(xiàn)文字添加描邊功能
- iOS如何封裝帶復(fù)制功能的UILabel示例代碼
- iOS開發(fā)總結(jié)之UILabel常用屬性介紹
- iOS動(dòng)態(tài)調(diào)整UILabel高度的幾種方法
- iOS UILabel 設(shè)置內(nèi)容的間距及高度的計(jì)算示例
- iOS中UILabel實(shí)現(xiàn)長按復(fù)制功能實(shí)例代碼
- IOS 開發(fā)之UILabel 或者 UIButton加下劃線鏈接
- iOS UILabel根據(jù)內(nèi)容自動(dòng)調(diào)整高度
- iOS兩丫技術(shù)之UILabel性能不夠的解決方法
相關(guān)文章
IOS網(wǎng)絡(luò)請(qǐng)求之NSURLSession使用詳解
這篇文章主要介紹了IOS網(wǎng)絡(luò)請(qǐng)求之NSURLSession使用詳解,今天使用NSURLConnection分別實(shí)現(xiàn)了get、post、表單提交、文件上傳、文件下載,有興趣的可以了解一下。2017-02-02
在iOS中使用OpenGL ES實(shí)現(xiàn)繪畫板的方法
這篇文章主要介紹了在iOS中使用OpenGL ES實(shí)現(xiàn)繪畫板的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
objective-c實(shí)現(xiàn)點(diǎn)到直線的距離及與垂足的交點(diǎn)
這篇文章主要給大家介紹了利用objective-c實(shí)現(xiàn)點(diǎn)到直線的距離及與垂足的交點(diǎn)的相關(guān)資料,文中給出了詳細(xì)的實(shí)現(xiàn)思路和實(shí)現(xiàn)代碼,對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-04-04
iOS發(fā)送驗(yàn)證碼倒計(jì)時(shí)應(yīng)用
點(diǎn)擊發(fā)送驗(yàn)證碼button。倒計(jì)時(shí)開始,這篇文章就為大家詳細(xì)介紹了iOS發(fā)送驗(yàn)證碼倒計(jì)時(shí)應(yīng)用的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
iOS應(yīng)用中UICollectionViewCell定制Button
這篇文章主要介紹了iOS應(yīng)用中UICollectionViewCell如何定制Button,設(shè)置每行顯示的按鈕的個(gè)數(shù),自定制按鈕的顯示樣式,感興趣的小伙伴們可以參考一下2016-08-08

