简体   繁体   English

如何在NSAttributedString中删除和替换NSTextAttachment

[英]How do you remove and replace an NSTextAttachment in an NSAttributedString

I have a mutable attributed string which I have added an image text attachment to. 我有一个可变的属性字符串,我添加了图像文本附件。

NSMutableAttributedString* attributedString = [_textField.attributedText mutableCopy];

NSMutableDictionary* dict = [_homeFeedCommentsViewController.inputData.array_extraElements objectAtIndex:0];

NSNumber* indexObject = [NSNumber numberWithUnsignedLong:attributedString.length];

[dict setObject:indexObject forKey:@"LocationIndex"];

[attributedString insertAttributedString:imageAsAttributedString atIndex:attributedString.length];

How do i now get at that text attachment & replace it with a string at the location where the image is supposed to be located in the body of the text? 我现在如何获取该文本附件并将其替换为图像应位于文本正文中的位置的字符串?

只需告诉文本存储,您更新了相应附件字符范围的属性:

textStorage.edited([.EditedAttributes], range: range, changeInLength: 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测NSAttributedString是否包含NSTextAttachment并将其删除? - How to detect if a NSAttributedString contains a NSTextAttachment and remove it? 如何将NSTextAttachment插入NSAttributedString? - How to insert NSTextAttachment into NSAttributedString? 你如何使用 NSAttributedString? - How do you use NSAttributedString? NSAttributedString上的模糊图像(NSTextAttachment) - Blurry image (NSTextAttachment) on NSAttributedString 如何调整图像大小或作为 NSAttributedString NSTextAttachment 完成(或设置其初始大小) - how to resize an image or done as a NSAttributedString NSTextAttachment (or set its initital size) 如何将特定字符串替换为NSTextAttachment的显示图像 - How to replace specific strings to NSTextAttachment for show image 使用NSTextAttachment为NSAttributedString设置截断尾部的垂直对齐 - Setting vertical align of truncated tails for NSAttributedString with NSTextAttachment 可以存储(或恢复)包含 NSTextAttachment 的 NSAttributedString 吗? - Can NSAttributedString which contains NSTextAttachment be stored(or restored)? 如何从 NSAttributedString swift 中删除属性? - How to remove attributes from the NSAttributedString swift? 使用 NSTextAttachment 时使用 NSKeyedUnarchiver 在 iOS13 上取消归档 NSAttributedString 时出错 - Error unarchiving NSAttributedString on iOS13 with NSKeyedUnarchiver when NSTextAttachment is used
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM