2012-11-07 12 views
11

Nella mia app ho uno UITextView e un pulsante proprio sotto la visualizzazione di testo per inserire una foto nello UITextView durante la modifica.Aggiunta di immagini a UITextView

Il mio requisito è che l'utente sia in grado di modificare il testo all'interno e inserire le immagini quando necessario.

Simile alla propria UITextView app di StackOverflow:

enter image description here

risposta

17

È possibile aggiungere la visualizzazione dell'immagine come una visualizzazione secondaria di UITextView.

Creare un IMAGEVIEW con un'immagine:

UIImageView *imageView = [[UIImageView alloc] initWithImage:yourImage]; 
[imageView setFrame:yourFrame]; 
[yourTextView addSubview:imageView]; 

Edit:

Per evitare l'uso di sovrapposizione (grazie @ Chris):

CGRect aRect = CGRectMake(156, 8, 16, 16); 
[imageView setFrame:aRect]; 
UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(CGRectGetMinX(imageView.frame), CGRectGetMinY(imageView.frame), CGRectGetWidth(yourTextView.frame), CGRectGetHeight(imageView.frame))]; 
yourTextView.textContainer.exclusionPaths = @[exclusionPath]; 
[yourTextView addSubview:imageView]; 
+9

Ma come si fa a evitare che l'ImageView da copre il testo nella visualizzazione testo? Come fai a far scorrere il testo attorno alla visualizzazione di immagini? –

+5

CGRect aRect = CGRectMake (156, 8, 16, 16); [attachmentView setFrame: aRect]; UIBezierPath * exclusionPath = [UIBezierPath bezierPathWithRect: CGRectMake (CGRectGetMinX (attachmentView.frame), CGRectGetMinY (attachmentView.frame), CGRectGetWidth (internalTextView.frame), CGRectGetHeight (attachmentView.frame))]; internalTextView.textContainer.exclusionPaths = @ [exclusionPath]; [internalTextView addSubview: attachmentView]; –

1

check this out, ios-5-rich-text-editing-series. In iOS 5 puoi inserire immagini e utilizzare testi HTML. Potrebbe essere necessario utilizzare UIWebview e webkit.

È anche possibile verificare con EGOTextView che ha molte funzioni di modifica di testo avanzato.

+0

@ user1645721, Se si desidera che l'utente inizi a inserire il testo dopo l'immagine, potrebbe essere necessario utilizzare i suggerimenti sopra riportati nella mia risposta. – iDev

1

Basta aggiungere come una visualizzazione secondaria di TextView come muggito ..

[yourTextView addSubview:yourImageView]; 
9

Se si aggiunge solo come una visualizzazione secondaria, un testo può essere "dietro" l'immagine. Quindi aggiungere il codice che "raccontare" il testo, che area dell'immagine è inaccessibile:

UIBezierPath *exclusionPath = [UIBezierPath bezierPathWithRect:CGRectMake(CGRectGetMinX(imageView.frame),  
CGRectGetMinY(imageView.frame), CGRectGetWidth(imageView.frame), 
CGRectGetHeight(imageView.frame))]; 

textView.textContainer.exclusionPaths = @[exclusionPath]; 
0

Creare sottoclasse di UITextView e sovrascrivere questo metodo

- (void)paste:(id)sender 
{ 
    NSData *data = [[UIPasteboard generalPasteboard] dataForPasteboardType:@"public.png"]; 

    if (data) 
    { 

     NSMutableAttributedString *attributedString = [[self attributedText] mutableCopy]; 

     NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; 

     textAttachment.image = [UIImage imageWithData:data scale:5]; 

     NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment]; 

     [attributedString replaceCharactersInRange:self.selectedRange withAttributedString:attrStringWithImage]; 

     self.attributedText = attributedString; 

    } 
    else 
    { 

     UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard]; 

     NSAttributedString *text = [[NSAttributedString alloc] initWithString:pasteBoard.string]; 

     NSMutableAttributedString *attributedString = [self.attributedText mutableCopy]; 

     [attributedString replaceCharactersInRange:self.selectedRange withAttributedString:text]; 

     self.attributedText = attributedString; 

    } 
} 
Problemi correlati