我想创建一个带有自定义图标的UITextView
(或UITextField
)。
我知道如何向UIImageView
添加UITextView
,但光标会忽略UIImageView,我无法编辑或删除图标(UIImageView
)。
最好的方法是什么? 我知道这是可能的,因为苹果在iMessage中使用它(附图像),还有另一个应用程序名称" kik"具有相同的行为。
答案 0 :(得分: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;