iOS|iOS 富文本如何添加图片

//联系人:石虎 QQ:1224614774 昵称:嗡嘛呢叭咪哄
QQ群:807236138群称:iOS 技术交流学习群
一、概念 /**
1.添加图片效果图
2.富文本添加图片代码
3.富文本总结
4.直接拷贝代码就可以用

*/
二、添加图片效果图 图1:
iOS|iOS 富文本如何添加图片
文章图片
富文本添加图片-1 iOS|iOS 富文本如何添加图片
文章图片
富文本添加图片-1 三、富文本添加图片代码

//ViewController.m
//测试富文本
//
//Created by joyshow on 2018/7/10.
//Copyright ? 2018年 石虎. All rights reserved.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//1.设置标签
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; titleLabel.backgroundColor = [UIColor yellowColor];
titleLabel.text = @"石虎祝所有人步步高升,成为技术大神";
titleLabel.textColor = [UIColor redColor];
[self.view addSubview:titleLabel];


//2.初始化富文本对象
NSMutableAttributedString *attributedString = [[NSMutableAttributedStringalloc] initWithString:titleLabel.text];


//2.1修改富文本中的不同文字的样式
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0, 5)]; //字体颜色
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor] range:NSMakeRange(7, 6)]; //字体颜色
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22] range:NSMakeRange(0, 6)]; //字体大小
//3.初始化NSTextAttachment对象
NSTextAttachment *attchment = [[NSTextAttachment alloc]init];
attchment.bounds = CGRectMake(0, 0, 40, 40); //设置frame
attchment.image = [UIImage imageNamed:@"release_homework"]; //设置图片


//4.创建带有图片的富文本
NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attchment)];
[attributedString insertAttributedString:string atIndex:0]; //插入到第几个下标
[attributedString appendAttributedString:string]; //添加到尾部
//5.用label的attributedText属性来使用富文本
titleLabel.attributedText = attributedString;
}
@end
四、富文本总结
这是富文本的所有属性
属性Name干啥的类型
NSFontAttributeName字号UIFont 默认12
NSParagraphStyleAttributeName段落样式NSParagraphStyle
NSForegroundColorAttributeName前景色UIColor
NSBackgroundColorAttributeName背景色UIColor
NSObliquenessAttributeName字体倾斜NSNumber
NSExpansionAttributeName字体加粗NSNumber 比例 0就是不变 1增加一倍
NSKernAttributeName字间距CGFloat
NSUnderlineStyleAttributeName下划线1或0
【iOS|iOS 富文本如何添加图片】NSUnderlineColorAttributeName下划线颜色UIColor
NSStrikethroughStyleAttributeName删除线1或0
NSStrikethroughColorAttributeName删除线颜色UIColor
NSStrokeColorAttributeNamesame as ForegroundColorUIColor
NSStrokeWidthAttributeName字体描边CGFloat
NSLigatureAttributeName连笔字 没看出效果1或0
NSShadowAttributeName阴影NSShawdow
NSTextEffectAttributeName设置文本特殊效果,目前只有图版印刷效果可用NSString
NSAttachmentAttributeName设置文本附件,常用插入图片NSTextAttachment
NSLinkAttributeName链接NSURL (preferred) or NSString
NSBaselineOffsetAttributeName基准线偏移NSNumber
NSWritingDirectionAttributeName文字方向 分别代表不同的文字出现方向等等,我想你一定用不到它 - -@[@(1),@(2)]
NSVerticalGlyphFormAttributeName水平或者竖直文本 在iOS没卵用,不支持竖版1竖直 0水平
谢谢!!!

    推荐阅读