UILabel关键字标红和改变字体效果

  • 效果如下图:


    1.png
  • 代码如下
    UILabel *label = @"86易清洁/";
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString: label.text];
    //设置平均分的字号
    NSRange purpleRange = NSMakeRange(0, [[noteStr string] rangeOfString:@"\n"].location);
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Nexa Light" size:22] range:purpleRange];
    //设置\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, //保留整个字符NSLineBreakByClipping, //简单剪裁,到边
界为止
NSLineBreakByTruncatingHead, //按照"……文字"显示
NSLineBreakByTruncatingTail, //按照"文字……文字"显示
NSLineBreakByTruncatingMiddle //按照"文字……"显示
  • 设置对齐基线
UIBaselineAdjustmentAlignBaselines //文本最上端与Label中线对齐,默认值
UIBaselineAdjustmentAlignCenters //文本中线与Label中线对齐
UIBaselineAdjustmentNone //文本最下端与Label中线对齐
  • 阴影
myLabel.shadowColor = [UIColor grayColor];//阴影颜色,默认为
nilmyLabel.shadowOffset = CGSizeMake(1, 1);//阴影的偏移点
  • 计算UIlabel 随字体多行后的高度
CGRect result,bounds;bounds = CGRectMake(0, 0,200, 300);
heightLabel = [myLabel textRectForBounds:bounds limitedToNumberOfLines:20];//计算20行后的Label的Frame
NSLog(@"%f",heightLabel.size.height);

你可能感兴趣的:(UILabel关键字标红和改变字体效果)