【发布时间】:2015-07-22 01:05:34
【问题描述】:
我正在使用下一个代码将图像添加到 UITextView:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(200,200,140,140)];
textView.font = [UIFont systemFontOfSize:20.0f];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Test with emoji"];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:@"Angel.png"];
//for the padding inside the textView
textAttachment.image = [UIImage imageWithCGImage:textAttachment.image.CGImage scale:3.0 orientation:UIImageOrientationUp];
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
[attributedString replaceCharactersInRange:NSMakeRange(5, 1) withAttributedString:attrStringWithImage];
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:17] range:NSMakeRange(0, attributedString.length)];
textView.attributedText = attributedString;
NSLog(@"Text view: %@", textView.attributedText);
[self.view addSubview:textView];
结果如下:
我感兴趣的是,我如何知道在文本字段中插入了什么图片以及在女巫位置? 正如您在代码中观察到的那样,我正在考虑使用属性文本,因为它会记录:
Text view: Test {
NSFont = "<UICTFont: 0x7ff0324f2110> font-family: \".HelveticaNeueInterface-Regular\"; font-weight: normal; font-style: normal; font-size: 17.00pt";
}{
NSAttachment = "<NSTextAttachment: 0x7ff032682bc0>";
NSFont = "<UICTFont: 0x7ff0324f2110> font-family: \".HelveticaNeueInterface-Regular\"; font-weight: normal; font-style: normal; font-size: 17.00pt";
}with emoji{
NSFont = "<UICTFont: 0x7ff0324f2110> font-family: \".HelveticaNeueInterface-Regular\"; font-weight: normal; font-style: normal; font-size: 17.00pt";
}
更新
使用代码检索图像:
NSMutableArray *imagesArray = [[NSMutableArray alloc] init];
[attributedString enumerateAttribute:NSAttachmentAttributeName
inRange:NSMakeRange(0, [attributedString length])
options:0
usingBlock:^(id value, NSRange range, BOOL *stop)
{
if ([value isKindOfClass:[NSTextAttachment class]])
{
NSTextAttachment *attachment = (NSTextAttachment *)value;
UIImage *image = nil;
if ([attachment image])
image = [attachment image];
else
image = [attachment imageForBounds:[attachment bounds]
textContainer:nil
characterIndex:range.location];
if (image)
[imagesArray addObject:image];
}
}];
但是如果属性字符串包含多于 1 张连续照片怎么办? 示例:
- 如果字符串包含两张连续的照片,那么只有一张被添加到数组中 外观
代码
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Test with emoji "];
[attributedString replaceCharactersInRange:NSMakeRange(4, 1) withAttributedString:attrStringWithImage];
[attributedString replaceCharactersInRange:NSMakeRange(5, 1) withAttributedString:attrStringWithImage];
日志:
Image array: (
"<UIImage: 0x7fd4e3e56760>"
)
- 如果照片不连续,则它们都被正确添加到数组中 外观
代码
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Test with emoji "];
[attributedString replaceCharactersInRange:NSMakeRange(4, 1) withAttributedString:attrStringWithImage];
[attributedString replaceCharactersInRange:NSMakeRange(16, 1) withAttributedString:attrStringWithImage];
日志
Image array: (
"<UIImage: 0x7f9ce35a4a70>",
"<UIImage: 0x7f9ce35a4a70>"
)
那么,我正在做的事情或 enumerateAttribute 方法是否存在错误?
更新 2
如果我为我添加的每张照片创建一个新的 textAttachment 和 attrStringWithImage 实例,则设法解决了这个问题。
【问题讨论】:
-
枚举你的
NSAttributedString。在这里,我只保留了图像,但您也可以保存块中给出的range:stackoverflow.com/questions/29152660/… -
@Larme 您能否制定一个答案,并包括我的第二个问题的答案,即如果有多个图像,如何获取所有图像?提前谢谢!
标签: ios objective-c uitextview nsattributedstring