Sto usando il codice successivo per aggiungere un'immagine a un'UITextView:Come recuperare la foto da UITextView dopo averla aggiunta utilizzando NSAttributedString?
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];
e il risultato si presenta così:
ciò che mi interessa in, è come posso sapere quale immagine è stata inserita nel campo di testo e nella posizione della strega? Stavo pensando di usare attributedText, come si può osservare nel codice, dal momento che registra:
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";
}
Aggiornamento
Estratto l'immagine utilizzando il codice:
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];
}
}];
Ma cosa succede se attributoString contiene più di 1 foto consecutiva? esempio:
- se la stringa contiene due foto consecutive poi viene aggiunto solo uno alla matrice come appare
codice
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Test with emoji "];
[attributedString replaceCharactersInRange:NSMakeRange(4, 1) withAttributedString:attrStringWithImage];
[attributedString replaceCharactersInRange:NSMakeRange(5, 1) withAttributedString:attrStringWithImage];
registro :
Image array: (
"<UIImage: 0x7fd4e3e56760>"
)
- se le foto non sono consecutivi l'entrambi sono aggiunti correttamente alla matrice come appare
codice
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"Test with emoji "];
[attributedString replaceCharactersInRange:NSMakeRange(4, 1) withAttributedString:attrStringWithImage];
[attributedString replaceCharactersInRange:NSMakeRange(16, 1) withAttributedString:attrStringWithImage];
registro
Image array: (
"<UIImage: 0x7f9ce35a4a70>",
"<UIImage: 0x7f9ce35a4a70>"
)
Quindi, c'è un bug in quello che sto facendo o uno con il metodo enumerateAttribute?
Update 2 riuscito a risolvere il problema se creo una nuova istanza textAttachment
e attrStringWithImage
per ogni foto aggiungo.
Enumerate il vostro 'NSAttributedString'. Qui, ho appena mantenuto l'immagine, ma potete anche salvare l'intervallo che è indicato nel blocco: http://stackoverflow.com/questions/29152660/extract -uiimage-from-nsattributed-string/29153172 # 29153172 – Larme
@Larme puoi formulare una risposta e includere anche la risposta alla mia seconda domanda su come ottenere tutte le immagini se ce ne sono più di una? Grazie in anticipo! –