NSTextAttachment 需求:图文混排 初始实现的代码如下: let label = UILabel() label.frame = CGRect(x: 50.0, y: 150.0, width 根据宽高比,算出图片应该显示的高度 let imageWidth = image.size.width * imageHeight / image.size.height let attach = NSTextAttachment
1.1 初始化NSTextAttachment对象 1.2 创建带有图片的富文本 1.3 例子:展示信用卡标签 布局小技巧 iOS 自定义视图:《用户协议及隐私政策》弹框(包含超链接属性)【本文包含完整 1.1 初始化NSTextAttachment对象 NSTextAttachment *attchment = [[NSTextAttachment alloc]init];
NSForegroundColorAttributeName value:RGB_COLOR(@"#969696", 1) range:NSMakeRange(0, 2)]; NSTextAttachment *attach2 = [[NSTextAttachment alloc] init]; UIImage *image2 = [UIImage imageNamed:@" NSForegroundColorAttributeName value:RGB_COLOR(@"#969696", 1) range:NSMakeRange(0, tempStr.length)]; NSTextAttachment *attach = [[NSTextAttachment alloc] init]; UIImage *image = [UIImage imageNamed:@"relpay_三角下.png
首先,iOS7中新添加了一类NSTextAttachment,从类名理解它是一个文本附件,其实也正是如此,NSTextAttachment类可以向文本中添加一些附件,这有些向邮件系统,寄信者可以向邮件中添加附件一同发送出去 NSTextAttachment类并不直接参与富文本的渲染与布局,渲染和布局依然由NSAttributedString类来完成,NSAttributedString类中提供了方法将NSTextAttachment 以一个简单的图文混排为例: - (void)viewDidLoad { [super viewDidLoad]; //进行NSTextAttachment的创建 NSTextAttachment "]; //设置尺寸 attach.bounds = CGRectMake(0, 0, 120, 60); NSTextAttachment * attach2 = [[NSTextAttachment 结合使用NSTextAttachment与UITextView,这些都能实现。
//face[i][@"gif"]就是我们要加载的图片 //新建文字附件来存放我们的图片 NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; //给附件添加图片
//face[i][@"gif"]就是我们要加载的图片 //新建文字附件来存放我们的图片 NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; //给附件添加图片
现在的做法 苹果在iOS7中推出了一个新的类NSTextAttachment,它是做图文混排的利器,本文就是用这个类,只用50行代码实现文字与表情混排,当然也可以实现段落中的图文混排,与CoreText face[i][@"cht"] isEqualToString:subStr]) { //face[i][@"png"]就是我们要加载的图片 //新建文字附件来存放我们的图片,iOS7才新加的对象 NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; //给附件添加图片 textAttachment.image= [UIImage imageNamed
NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址 NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment 四、TextKit实例 1、添加图片-NSTextAttachment NSTextAttachment用于添加图片 //添加表情 UIImage * image = [UIImage imageNamed :@"011"]; NSTextAttachment * attachment1 = [[NSTextAttachment alloc] init]; #表情的大小 attachment.bounds
NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址 NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment UIControlEventTouchUpInside]; [self.view addSubview:Btn]; //6.给Label带上图片 (需要创建另一个富文本,带图片的) NSTextAttachment *attach = [[NSTextAttachment alloc]init]; attach.image = [UIImage imageNamed:@"定位蓝"]; attach.bounds
NSMutableAttributedString, NSString*, kn_link); // 图文混排等 k_PropertyStatement(strong, NSMutableAttributedString, NSTextAttachment return self; }; } - (void)setKn_attachment:(NSMutableAttributedString * _Nonnull (^)(NSTextAttachment * _Nonnull))kn_attachment{} - (NSMutableAttributedString * _Nonnull (^)(NSTextAttachment * _Nonnull ))kn_attachment{ return ^id(NSTextAttachment *attachment){ NSAttributedString * imageStr
regexStr:KRegularMatcheHttpUrl, title: title) for range in urlRanges { let attchimage:NSTextAttachment = NSTextAttachment() attchimage.image = UIImage.init(named: "photo") attchimage.bounds attributedString.length) for range in emotionRanges { //表情附件 let attchimage:NSTextAttachment = NSTextAttachment() attchimage.image = UIImage.init(named: "爱你") attchimage.bounds
alloc] initWithString:sendGiftNumStr]; UIImage *giftNumIcon = [UIImage imageNamed:imageName]; NSTextAttachment *giftXAttachment = [[NSTextAttachment alloc] init]; giftXAttachment.image = giftNumIcon; NSAttributedString
attributes:dictAttr8]; [attributedString appendAttributedString:attr8]; 图文混排 //聊天的表情文字混排 //设置文本附件,取值为NSTextAttachment 对象,常用于文字的图文混排 NSString *str9 = @"文字的图文混排\n"; NSTextAttachment *textAttachment = [[NSTextAttachment alloc
reloadInputViews]; break; default: break; } } 去除表情的拖拽交互 在iOS11上,UITextView上的NSTextAttachment 一番查找之后才找到一个比较隐蔽的属性:textDragInteraction,直接设置为NO就能禁止掉NSTextAttachment的拖拽交互。 也就是说,我们设置到输入框的NSAttributedString中的每一个NSTextAttachment都有一个"隐藏的"属性—表情的文本描述,这里对NSAttributedString进行拓展就能实现
//face[i][@"gif"]就是我们要加载的图片 18 //新建文字附件来存放我们的图片 19 NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; 20 21 //给附件添加图片 22
NSShawdow //NSTextEffectAttributeName 设置文本特殊效果,取值为 NSString 对象,目前只有图版印刷效果可用: //NSAttachmentAttributeName NSTextAttachment
NSLinkAttributeName 设置链接属性,点击后调用浏览器打开指定URL地址 NSAttachmentAttributeName 设置文本附件,取值为NSTextAttachment
//face[i][@"gif"]就是我们要加载的图片 50 //新建文字附件来存放我们的图片 51 NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; 52 53 //给附件添加图片
alloc] initWithString:@"你好"]; [attributedText appendAttributedString:first]; // 2 - 图片 // 带有图片的附件对象 NSTextAttachment *attachment = [[NSTextAttachment alloc] init]; attachment.image = [UIImage imageNamed:@"header_cry_icon