iOS 在UILabel顯示不同的字體和顏色和大小

一個(gè)label上不同顏色 和字體

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
attrLabel.attributedText = str;

label上不同大小

-(NSMutableAttributedString*) changeLabelWithText:(NSString*)needText
{
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:needText];
    UIFont *font = [UIFont systemFontOfSize:20];
    [attrString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0,4)];
    [attrString addAttribute:NSFontAttributeName value:[UIFont fontWithHiraKakuProNW3:12] range:NSMakeRange(4,needText.length-4)];
    
    return attrString;
}

最后編輯于
?著作權(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)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 項(xiàng)目中很多會(huì)見(jiàn)到同一個(gè)Label上,為了強(qiáng)調(diào)重點(diǎn)或引起注意等,label的text的字體和顏色都會(huì)不同。初學(xué)者可能...
    Jalynn葸閱讀 5,338評(píng)論 0 3
  • UILabel是一個(gè)常用的控件,它的屬性設(shè)置的方法在純代碼中經(jīng)常使用。在storyboard中,使用UILabel...
    坤哥lqk閱讀 946評(píng)論 0 2
  • 先觀察,再畫,不然比例不準(zhǔn)確
    十二的煙火生活閱讀 155評(píng)論 0 0
  • 晚風(fēng)啊 你帶她要去哪兒 求求你了 把她帶到她夢(mèng)想的遠(yuǎn)方 讓她過(guò)著她想要的生活 朝暉呀 你怎的如此耀眼 昨夜 我丟了...
    顧憶星閱讀 259評(píng)論 1 2

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