2014-11-09 184 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;