在一個UILabel中實現不一樣的字體和顏色

   關於如何在一個UILabel中實現不一樣字體和顏色的問題一直困擾了我好久,以前一直想着如何自定義一個UILabelView來實現,結果老是失敗,知道最近我深刻接觸了NSMutableAttributedString以後,才發現要實現它原來是那麼的簡單。字體

    遙想實現它,咱們得換一種思路,那就是從要輸入的字符串下手,而不是一味的從UILabel找突破。那好,一個例子就能夠說明一切問題:spa

    NSString *title = @"Please rank from most to least the personality below you like your partner to have";
code

    NSMutableAttributedString* string = [[NSMutableAttributedString alloc]initWithString:title];
    
    
NSRange range1, range2;
    range1 = 
NSMakeRange(17, 13);//經過NSRange來劃分片斷
    range2 = 
NSMakeRange(62, 12);
    
    
UIColor*    color1 = TITLE_TEXT_COLLOR; //TITLE_TEXT_COLLOR 是我自定義的顏色
    
    [string 
addAttribute:NSForegroundColorAttributeName value:color1 range:range1];//給不一樣的片斷設置不一樣的顏色
    [string 
addAttribute:NSForegroundColorAttributeName value:color1 range:range2];
    [string 
addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeueLTStd-BdCn" size:15] range:range1];//給不一樣的片斷設置不一樣的字體
    [string 
addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeueLTStd-BdCn" size:15] range:range2];

orm

    [lblTitle setAttributedText:string];字符串

    好了,以上實例叫咱們如何來給一個UILableView同事設定不一樣的顏色和字體。string

相關文章
相關標籤/搜索