IOS UILabel的公用屬性及拓展屬性
前言
在IOS開發的過程中,UILabel是很常用的一個控制項,同時也是大量使用的一個控制項。建立一個UILabel一般需要五六句代碼,如果我們需要建立幾十個UILabel,就意味著我們要寫五六十句代碼,其實很多代碼是重複的,我們可以把類似的代碼寫到一個公用的方法中,以提高工作效率和降低代碼重複。官方提供UILabel的一些屬性有很大的局限性,有些在項目中開發中需要用到的一些拓展性的屬性,根據個人經驗,也順便一起總結在這裡。
一、建立UILabel公用的方法
1、標頭檔中聲明方法如下:
+ (UILabel *)commonLabelWithFrame:(CGRect)frame text:(NSString*)text color:(UIColor*)color font:(UIFont*)font textAlignment:(NSTextAlignment)textAlignment;
2、源檔案中實現該方法:
+ (UILabel *)commonLabelWithFrame:(CGRect)frame text:(NSString*)text color:(UIColor*)color font:(UIFont*)font textAlignment:(NSTextAlignment)textAlignment{ UILabel *label = [[UILabel alloc] initWithFrame:frame]; label.text = text; label.textColor = color; label.font = font; label.textAlignment = textAlignment; label.backgroundColor = [UIColor clearColor]; return label;}
二、動態設定UILabel高度
1、標頭檔申明方法如下:
/** * 建立一個動態高度的UILabel * * @param pointX Label的橫座標 * @param pointY Label的縱座標 * @param width Label的寬度 * @param strContent 內容 * @param color 字型顏色 * @param font 字型大小 * @param textAlignmeng 對齊 * * @return 返回一個UILabel */+ (UILabel *)dynamicHeightLabelWithPointX:(CGFloat)pointX pointY:(CGFloat)pointY width:(CGFloat)width strContent:(NSString *)strContent color:(UIColor *)color font:(UIFont *)font textAlignmeng:(NSTextAlignment)textAlignmeng;
2、源檔案中實現該方法:
//動態設定Label的高度+ (UILabel *)dynamicHeightLabelWithPointX:(CGFloat)pointX pointY:(CGFloat)pointY width:(CGFloat)width strContent:(NSString *)strContent color:(UIColor *)color font:(UIFont *)font textAlignmeng:(NSTextAlignment)textAlignmeng{ NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; NSDictionary *attributes = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paragraphStyle.copy}; CGSize labelSize = [strContent boundingRectWithSize:CGSizeMake(width,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size; UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(pointX, pointY, width, labelSize.height)]; [myLabel setNumberOfLines:0]; myLabel.text = strContent; myLabel.font = font; myLabel.textColor = color; return myLabel;}
3、測試結果:
- (void)viewDidLoad { [super viewDidLoad]; NSString *str = @6月初,華潤華髮聯合體以87.95億元拿下上海閘北地塊,地塊樓麵價38061元/平方米,重新整理了其自身於3月創下的上海總價“地王”紀錄。同日,招商平安聯合體則以高達2.3萬元/平方米的樓麵價,競得寶山大場鎮地塊,創出近90%的高溢價率。不僅是一線市場,杭州、蘇州等二線市場也在6月初集中推地。杭州西溪濕地旁低密度住宅地塊樓麵價9975元/平方米,溢價率33%,成為2014年春節以來杭州溢價率最高的住宅用地。; UILabel *label = [LTLabel dynamicHeightLabelWithPointX:5 pointY:20 width:self.view.frame.size.width-10 strContent:str color:[UIColor blackColor] font:[UIFont systemFontOfSize:20.0] textAlignmeng:NSTextAlignmentLeft]; label.backgroundColor = [UIColor groupTableViewBackgroundColor]; [self.view addSubview:label]; }
(1)字型大小為15號,與邊距間隔為5,測試結果如下:
(2)字型大小為20號,於邊距間隔為5,測試結果如下:
(3)字型大小為20號,於邊距間隔為50,測試結果如下:
(4)字型大小為20號,於邊距間隔為5,增加常值內容,測試結果如下:
三、設定UILabel的對齊
對於官方已經提供UILabel的一些對齊,在這裡就不做說明了,這裡主要補充官方沒有提供的對齊。主要提供了三種常用的對齊:垂直頂端對齊、頂端置中對齊、頂端靠右對齊。
1、標頭檔申明方法如下:
@interface DpLabel : UILabeltypedef enum{ VerticalAlignmentTop = 0, //default 垂直頂端對齊 VerticalAlignmentMidele, //頂端置中對齊 VerticalAlignmentBottom, //頂端靠右對齊}VerticalAlignment;@property (nonatomic, assign) VerticalAlignment verticalAlignment;
2、源檔案實現該方法:
#import DpLabel.h@implementation DpLabel@synthesize verticalAlignment;- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { // Initialization code verticalAlignment = VerticalAlignmentTop; } return self;}- (VerticalAlignment)verticalAlignment{ return verticalAlignment;}- (void)setVerticalAlignment:(VerticalAlignment)align{ verticalAlignment = align; [self setNeedsDisplay];}- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines{ CGRect rc = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines]; switch (verticalAlignment) { case VerticalAlignmentTop: rc.origin.y = bounds.origin.y; break; case VerticalAlignmentBottom: rc.origin.y = bounds.origin.y + bounds.size.height - rc.size.height; break; default: rc.origin.y = bounds.origin.y + (bounds.size.height - rc.size.height)/2; break; } return rc;}- (void)drawTextInRect:(CGRect)rect{ CGRect rc = [self textRectForBounds:rect limitedToNumberOfLines:self.numberOfLines]; [super drawTextInRect:rc];}//調整文本中的行距的方法/*使用方法 * *text參數 :常值內容 * *height參數:行距 * *name 參數:你使用的 UIlable 對象 */- (void) getlable_height :(NSString *) text uiheight:(NSInteger) height uilable:(UILabel*) name{ NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:height];//調整行間距 [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [text length])]; name.attributedText = attributedString; }@end
3、測試結果
- (void)viewDidLoad { [super viewDidLoad]; DpLabel *label = [[DpLabel alloc] initWithFrame:CGRectMake(20, 120, self.view.frame.size.width-40, 50)]; label.text = @測試對齊; label.textAlignment = VerticalAlignmentTop; label.backgroundColor = [UIColor redColor]; [self.view addSubview:label];}
(1)測試垂直頂端對齊,測試結果如下:
(2)測試頂端置中對齊,測試結果如下:
(3)測試頂端靠右對齊方式,測試結果如下: