I am using the next code to add an image to a 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];
and the result looks like this:
what I am interested in, is how can I know what picture was inserted in the text field and at witch position? I was thinking about using attributedText, as you can observe in the code, since it logs:
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";
}
Update
Retrieved the image using the code:
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];
}
}];
But what if attributedString contains more than 1 consecutive photo? example:
- if the the string contains two consecutive photos then only one is added to the array how it looks
code
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Test with emoji "];
[attributedString replaceCharactersInRange:NSMakeRange(4, 1) withAttributedString:attrStringWithImage];
[attributedString replaceCharactersInRange:NSMakeRange(5, 1) withAttributedString:attrStringWithImage];
log:
Image array: (
"<UIImage: 0x7fd4e3e56760>"
)
- if the photos are not consecutive the both of them are added correctly to the array how it looks
code
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Test with emoji "];
[attributedString replaceCharactersInRange:NSMakeRange(4, 1) withAttributedString:attrStringWithImage];
[attributedString replaceCharactersInRange:NSMakeRange(16, 1) withAttributedString:attrStringWithImage];
log
Image array: (
"<UIImage: 0x7f9ce35a4a70>",
"<UIImage: 0x7f9ce35a4a70>"
)
So, is there a bug in what I am doing or one with the enumerateAttribute method?
Update 2
Managed to fix the issue if I create a new textAttachment
and attrStringWithImage
instance for each photo I add.