-
效果如下圖:
1.png 代碼如下
UILabel *label = @"86易清潔/";
NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString: label.text];
//設(shè)置平均分的字號(hào)
NSRange purpleRange = NSMakeRange(0, [[noteStr string] rangeOfString:@"\n"].location);
[noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Nexa Light" size:22] range:purpleRange];
//設(shè)置\n后面label的顏色為灰色
NSRange grayRange = NSMakeRange([[noteStr string] rangeOfString:@"\n"].location,[noteStr string].length-purpleRange.length);
[noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:grayRange];
[label setAttributedText:noteStr];文字剪裁方式
NSLineBreakByWordWrapping = 0,//以空格為邊界,保留單詞
NSLineBreakByCharWrapping, //保留整個(gè)字符NSLineBreakByClipping, //簡(jiǎn)單剪裁,到邊
界為止
NSLineBreakByTruncatingHead, //按照"……文字"顯示
NSLineBreakByTruncatingTail, //按照"文字……文字"顯示
NSLineBreakByTruncatingMiddle //按照"文字……"顯示
- 設(shè)置對(duì)齊基線(xiàn)
UIBaselineAdjustmentAlignBaselines //文本最上端與Label中線(xiàn)對(duì)齊,默認(rèn)值
UIBaselineAdjustmentAlignCenters //文本中線(xiàn)與Label中線(xiàn)對(duì)齊
UIBaselineAdjustmentNone //文本最下端與Label中線(xiàn)對(duì)齊
- 陰影
myLabel.shadowColor = [UIColor grayColor];//陰影顏色,默認(rèn)為
nilmyLabel.shadowOffset = CGSizeMake(1, 1);//陰影的偏移點(diǎn)
- 計(jì)算UIlabel 隨字體多行后的高度
CGRect result,bounds;bounds = CGRectMake(0, 0,200, 300);
heightLabel = [myLabel textRectForBounds:bounds limitedToNumberOfLines:20];//計(jì)算20行后的Label的Frame
NSLog(@"%f",heightLabel.size.height);
