2012-11-07 21 views
11

w moim app mam UITextView oraz przycisk tuż poniżej widoku tekstu, aby wstawić zdjęcie do UITextView while edycji.Dodawanie zdjęć do UITextView

Moje wymogiem jest, że użytkownik użytkownik może edytować tekst wewnątrz i wstawić obrazy, gdy są potrzebne.

podobne do stackoverflow App własnej UITextView:

enter image description here

Odpowiedz

17

Można dodać widok obrazu jako podrzędny o UITextView.

Załóż ImageView z obrazka:

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

EDIT:

Dla uniknięcia nakładającą wykorzystania (dzięki @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

Ale w jaki sposób zapobiec ImageView z obejmujące tekst w tekście tekstowym? Jak uzyskać tekst płynący wokół widoku obrazu? –

+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

to sprawdzić, ios-5-rich-text-editing-series. W iOS 5 można wstawiać obrazy i korzystać z tekstów HTML. Być może będziesz musiał użyć UIWebview i webkita.

Można również skontaktować się z EGOTextView który ma wiele bogatych funkcji edycji tekstu.

+0

@ user1645721, Jeśli chcesz, aby użytkownik zaczął wprowadzać tekst po obrazie, być może będziesz musiał użyć powyższych sugestii w mojej odpowiedzi. – iDev

1

wystarczy dodać jako podrzędny z TextView jak mieszka ..

[yourTextView addSubview:yourImageView]; 
9

Jeśli dodać go tylko jako podrzędny, jakiś tekst może być „za” obraz. więc dodać kod, który będzie „Tell” tekst, który obszar obrazu jest niedostępne:

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

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

Tworzenie podklasy UITextView i zastąpić tę metodę

- (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; 

    } 
} 
Powiązane problemy