iOS之label中設置某段文字的顏色,大小


/**
 改變label文字中某段文字的顏色,大小
 label 傳入label(傳入前要有文字)
 oneW  從第一個文字開始
 twoW  到最后一個文字結束
 color 顏色
 size 尺寸
 */
+(void)LabelAttributedString:(UILabel*)label firstW:(NSString *)oneW toSecondW:(NSString *)twoW color:(UIColor *)color size:(CGFloat)size{
    // 創建Attributed
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:label.text];
    // 需要改變的第一個文字的位置
    NSUInteger firstLoc = [[noteStr string] rangeOfString:oneW].location;
    // 需要改變的最后一個文字的位置
    NSUInteger secondLoc = [[noteStr string] rangeOfString:twoW].location+1;
    // 需要改變的區間
    NSRange range = NSMakeRange(firstLoc, secondLoc - firstLoc);
    // 改變顏色
    [noteStr addAttribute:NSForegroundColorAttributeName value:[UIColor orangeColor] range:range];
    // 改變字體大小及類型
    [noteStr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Helvetica-BoldOblique" size:size] range:range];
    // 為label添加Attributed
    [label setAttributedText:noteStr];
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM