ios 同一label 局部字體加粗

富文本.png

這只是一個簡單的demo,用相同的思路也可以構建炫酷的特效,這里就展示最容易的入門代碼

代碼如下:

  • (void)viewDidLoad {
    [super viewDidLoad];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 260)];
    label.text = @"Label Text Content, This is a text label things attribute";//默認為空
    label.font = [UIFont systemFontOfSize:17];//默認使用系統的17
    label.textColor = [UIColor orangeColor];//默認使用文本黑色
    label.textAlignment = NSTextAlignmentCenter;//默認是左對齊
    [self.view addSubview:label];

    NSString *string = label.text;
    const CGFloat fontSize = 16.0;
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string];
    NSUInteger length = [string length];
    //設置字體
    UIFont *baseFont = [UIFont systemFontOfSize:fontSize];
    [attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];//設置所有的字體
    UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
    [attrString addAttribute:NSFontAttributeName value:boldFont range:[string rangeOfString:@"Text"]];//設置Text這四個字母的字體為粗體
    label.attributedText = attrString;

    // Do any additional setup after loading the view, typically from a nib.
    }
    對你有幫助的話,甩個贊??吧

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容