【问题标题】:Custom Icon For UITextView in ObjectiveCObjective C中TextView的自定义图标
【发布时间】:2014-11-09 08:12:37
【问题描述】:

我想创建一个带有自定义图标的UITextView(或UITextField)。

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

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

【问题讨论】:

  • 你不能在文本字段中传递图像...目前键盘扩展只能将一个普通的 NSString 传递给主机应用程序。有些人使用 NSPasteboard 交换数据,但这需要宿主应用程序进行“粘贴”。
  • 同意@Helix,你不能。 “kik”所做的是,如果您仔细观察,请使用UITextField上方的单独视图插入图像。

标签: ios objective-c iphone xcode uiimageview


【解决方案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;

【讨论】:

    猜你喜欢
    • 2015-09-07
    • 1970-01-01
    • 2011-08-16
    • 2013-03-28
    • 1970-01-01
    • 2011-09-02
    • 1970-01-01
    • 2023-03-12
    • 2012-11-17
    相关资源
    最近更新 更多