iOS NSAttributedString富文本总结

作者: 酒茶白开水 | 来源:发表于2017-01-12 15:42 被阅读153次

    代码下载

    代码下载请点击

    富文本的属性介绍

            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 对象
    

    富文本段落排版格式属性介绍

            lineSpacing 字体的行间距
            firstLineHeadIndent  首行缩进
            alignment (两端对齐的)文本对齐方式:(左,中,右,两端对齐,自然)
            lineBreakMode 结尾部分的内容以……方式省略 ( "...wxyz" ,"abcd..." ,"ab...yz")
            headIndent 整体缩进(首行除外)
            tailIndent
            minimumLineHeight 最低行高
            maximumLineHeight 最大行高
            paragraphSpacing 段与段之间的间距
            paragraphSpacingBefore 段首行空白空间
            baseWritingDirection 书写方向(一共三种)
            lineHeightMultiple
            hyphenationFactor 连字属性 在iOS,唯一支持的值分别为0和1
    

    上几张效果图

    Simulator Screen Shot 2017年1月12日 下午3.13.04.png
    Simulator Screen Shot 2017年1月12日 下午3.13.49.png
    Simulator Screen Shot 2017年1月12日 下午3.14.08.png

    编码使用举例

        说明:由于富文本的属性太多,使用也相当简单,就不一一举例,用法都相差不多,示例代码都有相应的例子,可以去下载。
    

    富文本一般是给控件的attributedText赋值的,也可以绘制到控件上。
    1.最普遍的属性使用(如文本颜色)

        NSAttributedString *attributedStr = [[NSAttributedString alloc] initWithString:@"我是红色文字" attributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
        self.label.attributedText = attributedStr;
    

    2.文本附件和链接
    先为textView设置富文本

       NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
       UIImage *image = [UIImage imageNamed:@"Weizhifu"];
       attachment.image = image;
       attachment.bounds = CGRectMake(0, 0, image.size.width, image.size.height);
       NSAttributedString *attributedStr = [NSAttributedString attributedStringWithAttachment:attachment];
       NSMutableAttributedString *mattributedStr = [[NSMutableAttributedString alloc] initWithString:@"我是带附件的文字" attributes:nil];
        [mattributedStr insertAttributedString:attributedStr atIndex:mattributedStr.length];
    
        self.attachmentTextView.attributedText = mattributedStr;
    
        attributedStr = [[NSAttributedString alloc] initWithString:@"我是带链接的文字" attributes:@{NSLinkAttributeName:@"https://www.baidu.com"}];
        self.linkTextView.attributedText = mattributedStr;
    

    然后在UITextViewDelegate的如下两个代理方法中可以对附件和链接进行交互

    - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
    {
        NSRange range = NSMakeRange(0, 0);
        NSDictionary *attributes;
        while (range.location + range.length < self.model.attributedModel.effectString.length) {
            attributes = [self.model.attributedModel.effectString attributesAtIndex:range.location + range.length effectiveRange:&range];
            if ([attributes valueForKey:NSLinkAttributeName]) {
                return YES;
            }
        }
        
        return NO;
    }
    - (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange
    {
        NSRange range = NSMakeRange(0, 0);
        NSDictionary *attributes;
        while (range.location + range.length < self.model.attributedModel.effectString.length) {
            attributes = [self.model.attributedModel.effectString attributesAtIndex:range.location + range.length effectiveRange:&range];
            if ([attributes valueForKey:NSAttachmentAttributeName]) {
                return YES;
            }
        }
        
        return NO;
    }
    

    相关文章

      网友评论

        本文标题:iOS NSAttributedString富文本总结

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