UILabel的經常使用屬性及方法:
一、text //設置和讀取文本內容,默認爲nil label.text = @」文本信息」; //設置內容 NSLog(@」%@」, label.text); //讀取內容
二、textColor //設置文字顏色,默認爲黑色 lable.textColor = [UIColor redColor];
三、font //設置字體大小,默認17 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(-5, -5)];
十、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;
十二、adjustsFontSizeToFitWidth //改變字母之間的間距來適應Label大小
myLabel.adjustsFontSizeToFitWidth = 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 *str = @"text"; NSMutableAttributedString *textLabelStr = [[NSMutableAttributedString alloc] initWithString:str]; [textLabelStrsetAttributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor],
NSFontAttributeName :[UIFont systemFontOfSize:17]}
range:NSMakeRange(11,10)]; labe.attributedText = textLabelStr;
1六、計算UIlabel 隨字體多行後的高度 CGRect bounds = CGRectMake(0, 0, 100, 100); heightLabel = [myLabel textRectForBounds:boundslimitedToNumberOfLines:20]; //計算20行後的Label的Frame,經常使用語UITableViewCell的可變高度 NSLog(@"%f",heightLabel.size.height);//輸出
1七、UILabel根據字數多少自動實現適應高度 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 0, 0)];
label.backgroundColor = [UIColor redColor];
[label setNumberOfLines:0];
label.lineBreakMode = UILineBreakModeWordWrap;
label.font = [UIFont fontWithName:@"Arial" size:15];
CGSize size = CGSizeMake(200, 1000);
label.text = @「labelText」;
CGSize size = [label.text sizeWithFont:fonts constrainedToSize:size];
[label setFrame:CGRectMake(10, 10, 200, size.height)];
1八、漸變字體Label
UIColor *titleColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"pic.png"]];
NSString *title = @"titleText";
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
titleLabel.textColor = titleColor;
titleLabel.text = title;
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:titleLabel];
1九、Label添加邊框 titleLabel.layer.borderColor = [[UIColor grayColor] CGColor]; titleLabel.layer.borderWidth = 2;
//初始化 myLabel = [[UILabel alloc] initWithFrame:(CGRect){20,50,300,50}]; [self.view addSubview:myLabel]; //背景顏色 myLabel.backgroundColor = [UIColor purpleColor]; //文本 myLabel.text = @"鋤禾日當午,汗滴禾下土."; //文本顏色 myLabel.textColor = [UIColor grayColor]; //字體大小 myLabel.font = [UIFont boldSystemFontOfSize:20]; //對齊方式 myLabel.textAlignment = NSTextAlignmentCenter; //行數 myLabel.numberOfLines = 2; //圓角 myLabel.layer.cornerRadius = 0; myLabel.layer.masksToBounds = YES; //高亮 myLabel.highlighted = YES; myLabel.highlightedTextColor = [UIColor greenColor]; //自動折行設置 myLabel.lineBreakMode = NSLineBreakByCharWrapping; //陰影 [myLabel setShadowOffset:(CGSize){4,4}]; [myLabel setShadowColor:[UIColor blueColor]]; //文本基線 myLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters; //是否自動收縮 myLabel.minimumScaleFactor = 0.4; //寬度自適應 myLabel.adjustsFontSizeToFitWidth = NO;