12

私のアプリでは、テキストビューのすぐ下に、編集中UITextViewに写真を挿入するためのボタンがあります。UITextView

私の要件は、ユーザーユーザーがテキストを編集し、必要に応じて画像を挿入できることです。

StackOverflow のアプリ独自のものに似ていUITextViewます。

ここに画像の説明を入力

4

5 に答える 5

18

画像ビューを のサブビューとして追加できますUITextView

画像で imageView を作成します。

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

編集:

重複使用を避けるため (@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]; 
于 2012-11-07T05:49:40.623 に答える
1

ios-5-rich-text-editing-series をチェックしてください。iOS 5 では、画像を挿入して HTML テキストを使用できます。UIWebviewと webkitを使用する必要がある場合があります。

豊富なテキスト編集機能を備えたEGOTextViewで確認することもできます。

于 2012-11-07T05:51:06.207 に答える
1

次のように TextView のサブビューとして追加するだけです..

    [yourTextView addSubview:yourImageView];
于 2012-11-07T05:52:37.217 に答える
0

UITextView のサブクラスを作成し、このメソッドをオーバーライドします

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

    }
}
于 2015-05-12T11:17:02.357 に答える