2014-11-09 181 views
-1

我想用自定义图标创建UITextView(或UITextField)。ObjectiveC中的UITextView的自定义图标

我知道如何将UIImageView添加到UITextView,但光标忽略UIImageView,我不能编辑或删除图标(UIImageView)。

这样做的最佳方法是什么? 我知道这是可能的,因为苹果在iMessage中使用它(附图所示),并且存在具有相同行为的另一个应用程序名称“kik”。

enter image description here

+0

ü不能传递图像文本框...当前键盘的扩展只能通过一个普通的NSString主机应用程序。有些人使用NSPasteboard来交换数据,但是这需要主机应用程序来执行“粘贴”。 – Helix 2014-11-09 08:44:44

+0

同意@Helix,你不可以。“kik”所做的是,使用'UITextField'上方的单独视图来插入图像仔细观察。 – Peter 2014-11-09 13:42:17

回答

1

试试这个gever):

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"before after"]; 
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init]; 
textAttachment.image = [UIImage imageNamed:@"download.jpeg"]; 

CGFloat oldWidth = textAttachment.image.size.width; 

CGFloat scaleFactor = oldWidth/(self.textField.frame.size.width - 10); 
textAttachment.image = [UIImage imageWithCGImage:textAttachment.image.CGImage scale:scaleFactor orientation:UIImageOrientationUp]; 
NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment]; 
[attributedString replaceCharactersInRange:NSMakeRange(6, 1) withAttributedString:attrStringWithImage]; 
self.textField.attributedText = attributedString;