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繼承了一個新類,實現了居上對齊,居中對齊,居下對齊。具體以下: // // 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 在使用時: 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]; 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; 參考:iOS組件之UILabel iOS UILabel詳解 ·UILable是iPhone界面最基本的控件,主要用來顯示文本信息。 ·經常使用屬性和方法有 : 一、建立 CGRect rect = CGRectMake(100, 200, 50, 50); UILabel *label = [[UILabel alloc] initWithFrame:rect]; 二、text //設置和讀取文本內容,默認爲 nillabel.text = @」文本信息」; //設置內容 NSLog(@」%@」, label.text); //讀取內容 三、textColor //設置文字顏色,默認爲黑色 lable.textColor = [UIColor redColor]; 四、font //設置字體大小,默認 label.font = [UIFont systemFontOfSize:20]; //⼀通常方法 label.font = [UIFont boldSystemFontOfSize:20]; //加粗方法 label.font = [UIFont fontWithName:@"Arial" size:16]; //指定字體的方法//還有⼀一種從外部導入字體的方法。 五、textAlignment //設置標籤文本對齊方式。 label.textAlignment = NSTextAlignmentCenter; //還有NSTextAlignmentLeft、 NSTextAlignmentRight. 六、numberOfLines //標籤最多顯示行數,若是爲0則表示多行。 label.numberOfLines = 2; 七、enabled //只是決定了Label的繪製方式,將它設置爲NO將會使文本變暗,表示它沒有激活,這時向它設置顏色值是無效的。 label.enable = NO; 八、highlighted //是否高亮顯示label.highlighted = YES; label.highlightedTextColor = [UIColor orangeColor]; //高亮顯示時的文本顏色 九、ShadowColor //設置陰影顏色 [label setShadowColor:[UIColor blackColor]]; 十、ShadowOffset //設置陰影偏移量 [label setShadowOffset:CGSizeMake(-1, -1)]; 十一、baselineAdjustment //若是adjustsFontSizeToFitWidth屬性設置爲YES,這個屬性就來控制文本基線的行爲。 label.baselineAdjustment = UIBaselineAdjustmentNone; UIBaselineAdjustmentAlignBaselines = 0,默認,文本最上端與中線對齊。 UIBaselineAdjustmentAlignCenters, 文本中線與label中線對齊。 UIBaselineAdjustmentNone, 文本最低端與label中線對齊。 十二、Autoshrink //是否自動收縮Fixed Font Size 默認,若是Label寬度小於文字長度時時,文字大小不自動縮放minimumScaleFactor 設置最小收縮比例,若是Label寬度小於文字長度時,文字進行收縮,收縮超過比例後,中止收縮。minimumFontSize 設置最小收縮字號,若是Label寬度小於文字長度時,文字字號減少,低於設定字號後,再也不減少。 //6.0之後再也不使用了。 label.minimumScaleFactor = 0.5; 1三、adjustsLetterSpacingToFitWidth //改變字母之間的間距來適應Label大小 myLabel.adjustsLetterSpacingToFitWidth = NO; 1四、 lineBreakMode //設置文字過長時的顯示格式 label.lineBreakMode = NSLineBreakByCharWrapping;以字符爲顯示單位顯示,後面部分省略不顯示。 label.lineBreakMode = NSLineBreakByClipping;剪切與文本寬度相同的內容長度,後半部分被刪除。 label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字以……方式省略,顯示尾部文字內容。 label.lineBreakMode = NSLineBreakByTruncatingMiddle;中間的內容以……方式省略,顯示頭尾的文字內容。 label.lineBreakMode = NSLineBreakByTruncatingTail;結尾部分的內容以……方式省略,顯示頭的文字內容。 label.lineBreakMode = NSLineBreakByWordWrapping;以單詞爲顯示單位顯示,後面部分省略不顯示。 1五、adjustsFontSizeToFitWidth //設置字體大小適應label寬度 label.adjustsFontSizeToFitWidth = YES; 1六、attributedText:設置標籤屬性文本。 NSString *text = @"first"; NSMutableAttributedString *textLabelStr =[[NSMutableAttributedString alloc]initWithString:text]; [textLabelStr setAttributes:@{NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : [UIFont systemFontOfSize:17]} range:NSMakeRange(11,10)]; label.attributedText = textLabelStr; 1七、豎排文字顯示每一個文字加一個換行符,這是最方便和簡單的實現方式。 label.text = @"請\n豎\n直\n方\n向\n排\n列"; label.numberOfLines = [label.text length]; 1八、計算UIlabel 隨字體多行後的高度 CGRect bounds = CGRectMake(0, 0, 200, 300); heightLabel = [myLabel textRectForBounds:bounds limitedToNumberOfLines:20]; //計算20行後的Label的Frame NSLog(@"%f",heightLabel.size.height); 1九、UILabel根據字數多少自動實現適應高度 UILabel *msgLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 45, 0, 0)]; msgLabel.backgroundColor = [UIColor lightTextColor]; [msgLabel setNumberOfLines:0]; msgLabel.lineBreakMode = UILineBreakModeWordWrap; msgLabel.font = [UIFont fontWithName:@"Arial" size:12]; CGSize size = CGSizeMake(290, 1000); msgLabel.text = @"獲取到的deviceToken,咱們能夠經過webservice服務提交給.net應用程序,這裏我簡單處理,直接打出來,拷貝到.net應用環境中使用。"; CGSize msgSie = [msgLabel.text sizeWithFont:fonts constrainedToSize:size]; [msgLabel setFrame:CGRectMake(15, 45, 290, msgSie.height)]; 20、漸變字體Label UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"btn.png"]]; NSString *title = @"Setting"; UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 44)]; titleLabel.textColor = titleColor; titleLabel.text = title; titleLabel.font = [UIFont boldSystemFontOfSize:20]; titleLabel.backgroundColor = [UIColor clearColor]; [self.view addSubview:titleLabel]; [titleLabel release]; 2一、Label添加邊框 titleLabel.layer.borderColor = [[UIColor grayColor] CGColor]; titleLabel.layer.borderWidth = 2;