如何使用NSAttributedString添加它后从UITextView中检索照片?
我正在使用下一个代码将图像添加到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];
}
}];
但是如果attributesString包含多个连续的照片呢? 例:
码
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
实例,则可以解决该问题。
这里解释检索图像。
你的新问题是,如果两个图像是连续的和相同的。
所以,而不是:
if (image)
[imagesArray addObject:image];
你需要进行其他检查,这将对两张图像有效,但是你不知道它们是否连续。
if (image)
{
if ([imagesArray lastObject] != image)
[imagesArray addObject:image];
}
所以你需要保持NSRange
引用。
if (image)
{
if ([imagesArray count] > 0)
{
NSDictionary *lastFound = [imagesArray lastObject];
NSRange lastRange = [lastFound[@"range"] rangeValue];
UIImage *lastImage = lastFound[@"image"];
if (lastImage == image && lastRange.location+lastRange.length == range.location)
{ //Two images same & consecutive}
else
{
[imagesArray addObject:@{@"image":image, @"range":[NSValue valueWithRange:range]}];
}
}
else
{
[imagesArray addObject:@{@"image":image, @"range":[NSValue valueWithRange:range]}];
}
}
仅检索图像:
NSArray *onlyImages = [imagesArray valueForKey:@"image"];
注意:我没有检查这段代码是否可以编译,但是你应该明白。 我的范围计算可能是错误的(某些+ 1 / -1丢失,但没有什么难以通过测试来验证),以及如果两个相同的连续图像之间存在空间会怎样? 您可能希望获取( NSString *stringBetween = [[attributedString string] substringWithRange:NSMakeRange(lastRange.location+lastRange.length, range.location-lastRange.location+lastRange.length)]
之间的NSString *stringBetween = [[attributedString string] substringWithRange:NSMakeRange(lastRange.location+lastRange.length, range.location-lastRange.location+lastRange.length)]
,并检查空格,字符串字符(有很多方法可以做到这一点)。
附加说明:就您的情况而言,仅比较image != newImage
可能就足够了,但是如果您使用web图像,或者甚至包含两个名称不同的图像,但这些图像是相同的,那么这是另一个要知道它们是否相同的问题。 关于比较两幅图像,有几个问题需要考虑,但这需要一些时间/资源。
上一篇: How to retrieve photo from UITextView after adding it using NSAttributedString?