美文网首页iOS经验总结
iOS笔记之UILabel(富文本)

iOS笔记之UILabel(富文本)

作者: SuAdrenine | 来源:发表于2016-10-14 11:41 被阅读1585次

    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];  
    

    相关文章

      网友评论

        本文标题:iOS笔记之UILabel(富文本)

        本文链接:https://www.haomeiwen.com/subject/tyvgyttx.html