閱讀1000 返回首頁    go 技術社區[雲棲]


UILabel設置多種字體、顏色

前言

開發中常用到的設置UILabel的文本樣式代碼片段:


NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String,try your best to test attributed string text"];

[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" size:30.0]
            range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeName
            value:[UIFont fontWithName:@"Arial" size:30.0]
            range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeName
            value:[UIFont fontWithName:@"Arial" size:30.0]
            range:NSMakeRange(19, 6)];

UILabel *attrLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 320 - 40, 90)];
attrLabel.attributedText = str;
attrLabel.numberOfLines = 0;


說明

NSMutableAttributedString類可以添加各種樣式,常用的設置key有:

  • NSForegroundColorAttributeName 設置前景色,也就是文本顏色
  • NSFontAttributeName 設置字體
  • NSBackgroundColorAttributeName 設置背景色

更多內容點擊NSMutableAttributedString進去查看聲明。

原文來自:標哥的技術博客


最後更新:2017-04-03 05:39:38

  上一篇:go 【Tsinghua】樹重建(Rebuild)
  下一篇:go HDU1072-Nightmare【廣度優先搜索】