美文网首页
iOS 之-富文本 (NSAttributedString)

iOS 之-富文本 (NSAttributedString)

作者: _清墨 | 来源:发表于2017-03-20 12:09 被阅读381次

    关于富文本能做什么我就不多说了,我们一般遇到的图文混排等,这个还是很好做到的。

    好,开讲了:
    在讲之前,我们得先明白他的属性和一些方法

    1.属性:

        NSFontAttributeName                设置字体属性,默认值:字体:Helvetica(Neue) 字号:12
        NSForegroundColorAttributeNam      设置字体颜色,取值为 UIColor对象,默认值为黑色
        NSBackgroundColorAttributeName     设置字体所在区域背景颜色,取值为 UIColor对象,默认值为nil, 透明色
        NSLigatureAttributeName            设置连体属性,取值为NSNumber 对象(整数),0 表示没有连体字符,1 表示使用默认的连体字符
        NSKernAttributeName                设定字符间距,取值为 NSNumber 对象(整数),正值间距加宽,负值间距变窄
        NSStrikethroughStyleAttributeName  设置删除线,取值为 NSNumber 对象(整数)
        NSStrikethroughColorAttributeName  设置删除线颜色,取值为 UIColor 对象,默认值为黑色
        NSUnderlineStyleAttributeName      设置下划线,取值为 NSNumber 对象(整数),枚举常量 NSUnderlineStyle中的值,与删除线类似
        NSUnderlineColorAttributeName      设置下划线颜色,取值为 UIColor 对象,默认值为黑色
        NSStrokeWidthAttributeName         设置笔画宽度,取值为 NSNumber 对象(整数),负值填充效果,正值中空效果
        NSStrokeColorAttributeName         填充部分颜色,不是字体颜色,取值为 UIColor 对象
        NSShadowAttributeName              设置阴影属性,取值为 NSShadow 对象
        NSTextEffectAttributeName          设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用:
        NSBaselineOffsetAttributeName      设置基线偏移值,取值为 NSNumber (float),正值上偏,负值下偏
        NSObliquenessAttributeName         设置字形倾斜度,取值为 NSNumber (float),正值右倾,负值左倾
        NSExpansionAttributeName           设置文本横向拉伸属性,取值为 NSNumber (float),正值横向拉伸文本,负值横向压缩文本
        NSWritingDirectionAttributeName    设置文字书写方向,从左向右书写或者从右向左书写
        NSVerticalGlyphFormAttributeName   设置文字排版方向,取值为 NSNumber 对象(整数),0 表示横排文本,1 表示竖排文本
        NSLinkAttributeName                设置链接属性,点击后调用浏览器打开指定URL地址
        NSAttachmentAttributeName          设置文本附件,取值为NSTextAttachment对象,常用于文字图片混排
        NSParagraphStyleAttributeName      设置文本段落排版格式,取值为 NSParagraphStyle 对象
    

    其中常用的:

    NSFontAttributeName 
    字体
    NSParagraphStyleAttributeName 
    段落格式 
    NSForegroundColorAttributeName 
    字体颜色
    NSBackgroundColorAttributeName  
    背景颜色
    NSStrikethroughStyleAttributeName
    删除线格式
    NSUnderlineStyleAttributeName     
    下划线格式
    NSStrokeColorAttributeName       
    删除线颜色
    NSStrokeWidthAttributeName
    删除线宽度
    NSShadowAttributeName 
    阴影
    

    2.一些方法:

    @interface NSMutableAttributedString (NSExtendedMutableAttributedString)
    
    @property (readonly, retain) NSMutableString *mutableString;
    
    - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
    - (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
    - (void)removeAttribute:(NSString *)name range:(NSRange)range;
    
    - (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString;
    - (void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc;
    - (void)appendAttributedString:(NSAttributedString *)attrString;
    - (void)deleteCharactersInRange:(NSRange)range;
    - (void)setAttributedString:(NSAttributedString *)attrString;
    
    - (void)beginEditing;
    - (void)endEditing;
    
    @end
    

    其中常用的:

    为某一范围内文字设置多个属性
    - (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;
    

    接下来,举个小栗子:

    NSString *title = @"瑟吉欧多久啊到时见殴打舒服哈师范生佛我ofhis和覅瑟吉欧多久啊到时见殴打舒服哈师范生佛我ofhis和覅瑟吉欧多久啊到时见殴打舒服哈师范生佛我ofhis和覅瑟吉欧多久啊到时见殴打舒服哈师范生佛我ofhis和覅瑟吉欧多久啊到时见殴打舒服哈师范生佛我ofhis和覅瑟吉欧多久啊到时见殴打舒服哈师范生佛我ofhis和覅";
        
        //1.创建富文本
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:title];
        
        //2.添加属性
        /* 从0开始到后10位font为15 */
        [attStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, 10)];
        
        /* 从10开始到后20位font为加粗15 */
        [attStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:15] range:NSMakeRange(10, 20)];
        
        /* 从10开始到后20位颜色为白色 */
        [attStr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(10, 20)];
       
        /* 从19开始到后5位给个下划线,下划线样式为single(其他自己尝试),颜色为蓝色 */
        [attStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(19, 5)];
        [attStr addAttribute:NSUnderlineColorAttributeName value:[UIColor blueColor] range:NSMakeRange(19, 5)];
        
        /* 设置整段字间距 */
        [attStr addAttribute:NSKernAttributeName value:@1.1 range:NSMakeRange(0, title.length)];
        
        //3.设置段落样式
        NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc]init];
        paragraph.lineSpacing = 10;  //行间距
        paragraph.paragraphSpacing = 20;  //段间距
        paragraph.alignment = NSTextAlignmentLeft; //对齐方式
        paragraph.firstLineHeadIndent = 30;  //首行缩进
        [attStr addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, title.length)];
        
        //4.Label给个url (只会有超链接的样式出来,点击是点击不了的)
        NSString *urlStr = @"www.innsmap.com";
        NSURL *url = [NSURL URLWithString:urlStr];
        [attStr addAttribute:NSLinkAttributeName value:url range:NSMakeRange(0, 7)];
        
        //5.我就要点击
        UIButton *Btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    Btn.frame = 刚刚好那段文字的frame;  --- 别问我怎么算...
        Btn.backgroundColor = [UIColor grayColor];
        [Btn addTarget:self action:@selector(Btn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:Btn];
        
        //6.给Label带上图片 (需要创建另一个富文本,带图片的)
        NSTextAttachment *attach = [[NSTextAttachment alloc]init];
        attach.image = [UIImage imageNamed:@"定位蓝"];
        attach.bounds = CGRectMake(0, 0, 100, 80);
        NSAttributedString *imageStr = [NSAttributedString attributedStringWithAttachment:attach];
        /* 将这个图片直接添加到attStr后面 */
        [attStr appendAttributedString:imageStr];
        
        //7.创建Label来显示富文本
        UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 40, 300, 20)];
        myLabel.backgroundColor = [UIColor grayColor];
        myLabel.attributedText = attStr;
        myLabel.numberOfLines = 0;
        [myLabel sizeToFit];
        [self.view addSubview:myLabel];
        
        //这里可以获取到label的高度
        CGFloat height = myLabel.frame.size.height;
        NSLog(@"%f",height);
    

    结果是这样的:

    428CE703-0DE5-496B-87D0-9D5F19F4B371.png

    相关文章

      网友评论

          本文标题:iOS 之-富文本 (NSAttributedString)

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