【发布时间】:2014-03-28 04:36:00
【问题描述】:
我正在使用UItextview 并为工具栏按钮添加额外的inputAccessoryview 以执行其他操作。
我的工具栏有一个相机图标,可以从UIImagePickerController 获取图像,我必须将此图像添加到我的UItextview。
现在我能够做到这一点:
[myTextView addSubView:imageView];
但是那样,总是在文本视图的开头添加图像,我想要的是在当前光标位置添加它。我也想删除这张图片,就像我从 textview 中删除文本一样。
为了记录,我还尝试获取UItextview 文本的选定范围,并尝试在该位置插入我的imageview,但没有成功。
编辑代码:
NSMutableAttributedString * mas = [[NSMutableAttributedString alloc]initWithString:self.commentsTextView.text];
NSRange cursorPoistion = [self.commentsTextView selectedRange];
UIImage *chosenImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSTextAttachment* onionatt = [NSTextAttachment new];
onionatt.image = chosenImage;
onionatt.bounds = CGRectMake(0,-5,200,200);
NSAttributedString* onionattchar = [NSAttributedString attributedStringWithAttachment:onionatt];
[mas insertAttributedString:onionattchar atIndex:(cursorPoistion.location + cursorPoistion.length)];
self.commentsTextView.attributedText = mas;
【问题讨论】:
-
你知道如何删除图像吗?
标签: ios objective-c uiimageview uitextview uiimagepickercontroller