// 从问题出发找需要的源头. //1.textView.attribute -> 需要 NSAttributeText -> // ->replaceCh
aractersInRange()需要范围和
图片属性 ->textView.selectedRange -> // -> 由于要在光标处,输入
图片而且原文本不变 -> // ->需要
一个NSMutableText(attributedString: textView.attributedText) // ->2.建立
一个NSAttributedString -> attachment:缺少
一个 NSTextAttachment // -> 3. 初始化
一个NSTextAttachment()对象 attachment -> 4. 对象
属性中有image可以做图文混排 // let attachment = NSTextAttachment() attachment.image = UIImage(contentsOfFile: emoticon.imagePath) //问题二 设置苹果的高度 let height = textView.font!.lineHeight // bounds 的x / y 就是scrollView的contentOffset,苹果利用bounds 的x / y 能够调整空间内部 // 的偏移量位置 attachment.bounds = CGRect(x: 0,y: -4,width: height,height: height) //问题三
图片属性字符串,没设字体大小,导致插入
图片属性高低不小 let imageText = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment)) //
解决问题三,插入
图片大小问题 imageText.addAttribute(NSFontAttributeName,value: height,range: NSRange(location: 0,length: 1)) let mutableText = NSMutableAttributedString(attributedString: textView.attributedText) mutableText.replaceCh
aractersInRange(textView.selectedRange,withAttributedString: imageText) //问题一。记录光标 //1). 记录光标 let range = textView.selectedRange textView.attributedText = mutableText //2).恢复光标,同时让光标跑到原光标的位置的后
一个位置(range.location + 1),同时不保留length(0) textView.selectedRange = NSRange(location: range.location + 1,length: 0) tu
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。