iOS筆記之UILabel(富文本)

1、常見的屬性及說明

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

2、常見方法:

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

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

3、使用示例:

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

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

[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)、設(shè)置段落樣式

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

(5)、添加段落設(shè)置

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

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

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

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

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

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

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

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

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

5、設(shè)置行間距

        NSString *textStr = @":設(shè)置sizeToFit之后是可以取出label的高度的,這樣做label高度自適應(yīng)。但是如果你用第三方框架(如:Masonry)給其加約束,因為約束優(yōu)先級最高,所以這句會失效";  
        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;  
          
        // 調(diào)整行間距  
        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];  
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容