iOS筆記之UILabel(富文本)

1、常見的屬性及說明

NSFontAttributeName  //字體
NSParagraphStyleAttributeName  //段落格式 
NSForegroundColorAttributeName  //字體顏色
NSBackgroundColorAttributeName  //背景顏色
NSStrikethroughStyleAttributeName  //刪除線格式
NSUnderlineStyleAttributeName  //下劃線格式
NSStrokeColorAttributeName  //刪除線顏色
NSStrokeWidthAttributeName  //刪除線寬度
NSShadowAttributeName  //陰影

2、常見方法:

//為某一范圍內文字設置多個屬性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
//為某一范圍內文字添加某個屬性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
//為某一范圍內文字添加多個屬性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
//移除某范圍內的某個屬性
- (void)removeAttribute:(NSString *)name range:(NSRange)range;

更多方法和屬性說明詳見蘋果官方說明文檔

3、使用示例:

NSString *str = @"犯我中華者,雖遠必誅!";
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] 
      initWithString:str];
/*說明:NSAttributedString也能設置,與NSMutableAttributedString的關系類似于NSArray和NSMutableArray*/

(1)、添加字體和設置字體的范圍

[attrStr addAttribute:NSFontAttributeName value:
  [UIFont systemFontOfSize:20.0f] range:NSMakeRange(0, 3)];  //字體大小為20.0f
[attrStr addAttribute:NSFontAttributeName value:
  [UIFont boldSystemFontOfSize:20.0f] range:NSMakeRange(0, 3)];  //字體大小為20.0f并且加粗

(2)、添加文字顏色

[attrStr addAttribute:NSForegroundColorAttributeName value:
  [UIColor redColor] range:NSMakeRange(0, 7)];

(3)、添加下劃線

[attrStr addAttribute:NSUnderlineStyleAttributeName value:
  [NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, 7)];

(4)、設置段落樣式

NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
//行間距
paragraph.lineSpacing = 10;
//段落間距
paragraph.paragraphSpacing = 20;
//對齊方式
paragraph.alignment = NSTextAlignmentLeft;
//指定段落開始的縮進像素
paragraph.firstLineHeadIndent = 30;
//調整全部文字的縮進像素paragraph.headIndent = 10;

(5)、添加段落設置

[attrStr addAttribute:NSParagraphStyleAttributeName value:paragraph 
  range:NSMakeRange(0, [str length])];

(6)、添加鏈接
label添加鏈接注意:label鏈接是可以顯示出來,但是不能點擊,而textView是可以點擊的,因為里面有shouldInteractWithURL代理方法回調。

NSString *urlStr = @"www.baidu.com";
NSURL *url = [NSURL URLWithString:urlStr];
[attrStr addAttribute:NSLinkAttributeName value:url range:NSMakeRange(2, 7)];

(7)、一次性搞定:設字號為20,字體顏色為紅色

NSDictionary *attDict = [NSDictionary dictionaryWithObjectsAndKeys:
  [UIFont systemFontOfSize:20.0],NSFontAttributeName,
  [UIColor redColor],NSForegroundColorAttributeName,
  nil];
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc] 
  initWithString:@"犯我華夏者,雖遠必誅!" attributes:attDict];

4、label其他一些常用屬性:

//創建label
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 100, 200, 0)];
//設置背景顏色
label.backgroundColor = [UIColor lightGrayColor];
//自動換行
label.numberOfLines = 0;
//設置label的富文本
label.attributedText = attrStr;
//label高度自適應
[label sizeToFit];

//打印高度
CGFloat height = label.frame.size.height;
NSLog(@"height = %f",height);

**
PS:設置sizeToFit之后是可以取出label的高度的,這樣做label高度自適應。但是如果你用第三方框架(如:Masonry)給其加約束,因為約束優先級最高,所以這句會失效
**

5、設置行間距

        NSString *textStr = @":設置sizeToFit之后是可以取出label的高度的,這樣做label高度自適應。但是如果你用第三方框架(如:Masonry)給其加約束,因為約束優先級最高,所以這句會失效";  
        UIFont *textFont = [UIFont systemFontOfSize:14];  
        CGSize textSize = [textStr sizeWithFont:textFont  
                              constrainedToSize:CGSizeMake(bounds.size.width - 40, QZONE_SCREEN_HEIGHT)];;  
        UILabel *openMicPrivilegeTipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, textSize.width, textSize.height)];  
        openMicPrivilegeTipsLabel.textColor = DefaultDescriptionText2ColorInDefaultTheme;  
        openMicPrivilegeTipsLabel.text = textStr;  
        openMicPrivilegeTipsLabel.backgroundColor = [UIColor clearColor];  
        openMicPrivilegeTipsLabel.textAlignment = UITextAlignmentLeft;  
        openMicPrivilegeTipsLabel.font = [UIFont systemFontOfSize:14];  
        openMicPrivilegeTipsLabel.numberOfLines = 0;  
          
        // 調整行間距  
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textStr];  
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];  
        [paragraphStyle setLineSpacing:6];  
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [textStr length])];  
        openMicPrivilegeTipsLabel.attributedText = attributedString;  
          
        [_tipsBG addSubview:openMicPrivilegeTipsLabel];  
        [openMicPrivilegeTipsLabel sizeToFit];  
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,981評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,466評論 25 708
  • 餐桌上,飯菜擺放齊全,沒人動筷…… 你對著手機屏幕笑的異常燦爛, 我很好奇問你啥事那么開心分享一下啊,然后很期待的...
    郁恬閱讀 158評論 0 3
  • 不知道每個人平淡無奇的外表下到底藏了多少不安分因子,不知道他們何時會迸發,只知道若再不瘋狂我們就老了,每個...
    木子天道酬勤閱讀 418評論 0 0
  • 上周,我們班去爬了學校后面的君子峰。 君子峰不高,海拔也就兩百來米,從學校后門出去就有一條小路通往峰頂。路并不是由...
    d30b80c10dd4閱讀 210評論 0 0