NSAttributedString实现图文混排

/*

     

     常用属性

     // 字体

     NSFontAttributeName               // UIFont, default Helvetica(Neue) 12

     // 段落

     NSParagraphStyleAttributeName     // NSParagraphStyle, default defaultParagraphStyle

     // 文字颜色

     NSForegroundColorAttributeName    // UIColor, default blackColor

     // 背景颜色

     NSBackgroundColorAttributeName    // UIColor, default nil: no background

     // 描边颜色

     NSStrokeColorAttributeName        // UIColor, default nil: same as foreground color

     // 描边宽度

     NSStrokeWidthAttributeName        // NSNumber containing floating point value, default 0

     // 阴影

     NSShadowAttributeName             // NSShadow, default nil: no shadow

     // 附件

     NSAttachmentAttributeName         // NSTextAttachment, default nil

     // 链接URL

     NSLinkAttributeName               // NSURL (preferred) or NSString

     // 基线偏移量

     NSBaselineOffsetAttributeName     // NSNumber containing floating point value,default 0

     // 下划线

     NSUnderlineColorAttributeName      // UIColor, default nil: same as foreground color

     

     */

    

    

    

    NSString *textString = @"wdadjaisjdaowjd我的爱的哦说sado我啊啊啊http://www.baidu.com";

    //初始化可变属性字符串

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:textString];

    

    //设置属性

//    [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, textString.text.length)];

//    [attributedString addAttribute:NSKernAttributeName value:@1 range:NSMakeRange(0, textString.text.length)];

    

    //合并设置属性

//    [attributedString addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14],NSKernAttributeName: @1} range:NSMakeRange(0, textString.text.length)];

    

    //通过字符串获取范围

//    [attributedString addAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:26],NSForegroundColorAttributeName: [UIColor blueColor]} range:[textString rangeOfString:@"我的爱的哦说"]];

    

    //获取文字并修改属性

//    NSRange startRange = [textString localizedStandardRangeOfString:@"owjd"];

//    NSRange endRange = [textString localizedStandardRangeOfString:@"sado"];

//    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSUnionRange(startRange, endRange)];

    

    // 设置文本下划线

//    NSRange startRange1 = [textString localizedStandardRangeOfString:@"owjd"];

//    NSRange endRange1 = [textString localizedStandardRangeOfString:@"sado"];

//    [attributedString addAttribute:NSUnderlineStyleAttributeName value:@1 range:NSUnionRange(startRange1, endRange1)];

    

//    // 插入图片附件

//    NSTextAttachment *imageAtta = [[NSTextAttachment alloc] init];

//    imageAtta.bounds = CGRectMake(0, 0, 10, 12);

//    imageAtta.image = [UIImage imageNamed:@"修改价格"];

//    NSAttributedString *attach = [NSAttributedString attributedStringWithAttachment:imageAtta];

//    //将图片放在最后一位

//    [attributedString appendAttributedString:attach];

////    [attributedString insertAttributedString:attach atIndex:0];

    

    //设置段落属性

//    // 段落样式

//    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];

//    // 行间距

//    [style setLineSpacing:3];

//    // 段落间距

//    [style setParagraphSpacing:6];

//    // 首行缩进

//    [style setFirstLineHeadIndent:25];

//    [attributedString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(1, textString.length)];

    

    //添加网址链接

    NSRange urlRange = [textString rangeOfString:@"http://www.baidu.com"];

    [attributedString addAttribute:NSLinkAttributeName value:[NSURL URLWithString:@"http://www.baidu.com"] range:NSMakeRange(urlRange.location, 20)];

    [attributedString addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(urlRange.location, 20)];

    

    _textView.attributedText = attributedString;

猜你喜欢

转载自blog.csdn.net/quanhaoH/article/details/78653904