私のアプリでは、テキストビューのすぐ下に、編集中UITextView
に写真を挿入するためのボタンがあります。UITextView
私の要件は、ユーザーユーザーがテキストを編集し、必要に応じて画像を挿入できることです。
StackOverflow のアプリ独自のものに似ていUITextView
ます。
私のアプリでは、テキストビューのすぐ下に、編集中UITextView
に写真を挿入するためのボタンがあります。UITextView
私の要件は、ユーザーユーザーがテキストを編集し、必要に応じて画像を挿入できることです。
StackOverflow のアプリ独自のものに似ていUITextView
ます。
画像ビューを のサブビューとして追加できます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];
ios-5-rich-text-editing-series をチェックしてください。iOS 5 では、画像を挿入して HTML テキストを使用できます。UIWebview
と webkitを使用する必要がある場合があります。
豊富なテキスト編集機能を備えたEGOTextViewで確認することもできます。
次のように TextView のサブビューとして追加するだけです..
[yourTextView addSubview:yourImageView];
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;
}
}