UILabel關(guān)鍵字標(biāo)紅和改變字體效果

  • 效果如下圖:


    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);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容