图文混排中,在textView的指定光标下插入文字或图片
2016-08-09 21:24
417 查看
override func viewDidLoad() {
super.viewDidLoad()
demoTextView.text = "我是大帅比,真的是一个大帅比,确实是一个大帅比"
}
//插入文字
@IBAction func insetText(sender: AnyObject) {
//创建属性字符串
let attrStr = NSAttributedString(string: "美女", attributes: [NSForegroundColorAttributeName:UIColor.redColor()])
let font = demoTextView.font!
//创建可变属性字符串
let attrMutableStr = NSMutableAttributedString(attributedString: demoTextView.attributedText)
//将图片属性字符串替换到可变属性字符串的某一个位置
//获取光标所在位置
let range = demoTextView.selectedRange
//替换属性字符串
attrMutableStr.replaceCharactersInRange(range, withAttributedString: attrStr)
//显示属性字符串
demoTextView.attributedText = attrMutableStr
//将文字大小重置
demoTextView.font = font
//将光标设置回原来的位置
demoTextView.selectedRange = NSRange(location: range.location + 2, length : 0)
}
//插入图片
@IBAction func insetImage(sender: AnyObject) {
//创建图片属性字符串
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "5")
let font = demoTextView.font!
attachment.bounds = CGRectMake(0, -4, font.lineHeight, font.lineHeight)
let attrImageStr = NSAttributedString(attachment: attachment)
//创建可变属性字符串
let attrMutableStr = NSMutableAttributedString(attributedString: demoTextView.attributedText)
//将图片属性字符串替换到可变属性字符串的某一个位置
//获取光标所在位置
let range = demoTextView.selectedRange
//替换属性字符串
attrMutableStr.replaceCharactersInRange(range, withAttributedString: attrImageStr)
//显示属性字符串
demoTextView.attributedText = attrMutableStr
//将文字大小重置
demoTextView.font = font
//将光标
4000
设置回原来的位置
demoTextView.selectedRange = NSRange(location: range.location + 1, length : 0)
}
super.viewDidLoad()
demoTextView.text = "我是大帅比,真的是一个大帅比,确实是一个大帅比"
}
//插入文字
@IBAction func insetText(sender: AnyObject) {
//创建属性字符串
let attrStr = NSAttributedString(string: "美女", attributes: [NSForegroundColorAttributeName:UIColor.redColor()])
let font = demoTextView.font!
//创建可变属性字符串
let attrMutableStr = NSMutableAttributedString(attributedString: demoTextView.attributedText)
//将图片属性字符串替换到可变属性字符串的某一个位置
//获取光标所在位置
let range = demoTextView.selectedRange
//替换属性字符串
attrMutableStr.replaceCharactersInRange(range, withAttributedString: attrStr)
//显示属性字符串
demoTextView.attributedText = attrMutableStr
//将文字大小重置
demoTextView.font = font
//将光标设置回原来的位置
demoTextView.selectedRange = NSRange(location: range.location + 2, length : 0)
}
//插入图片
@IBAction func insetImage(sender: AnyObject) {
//创建图片属性字符串
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "5")
let font = demoTextView.font!
attachment.bounds = CGRectMake(0, -4, font.lineHeight, font.lineHeight)
let attrImageStr = NSAttributedString(attachment: attachment)
//创建可变属性字符串
let attrMutableStr = NSMutableAttributedString(attributedString: demoTextView.attributedText)
//将图片属性字符串替换到可变属性字符串的某一个位置
//获取光标所在位置
let range = demoTextView.selectedRange
//替换属性字符串
attrMutableStr.replaceCharactersInRange(range, withAttributedString: attrImageStr)
//显示属性字符串
demoTextView.attributedText = attrMutableStr
//将文字大小重置
demoTextView.font = font
//将光标
4000
设置回原来的位置
demoTextView.selectedRange = NSRange(location: range.location + 1, length : 0)
}
相关文章推荐
- 一段文字中插入一张图片(lable,textview为例)
- 【Swift】图文混排,ios开发中在textfield或textView中插入图片
- MixtureTextView 支持Android图文混排、文字环绕图片等效果
- 一种TextView插入图片的途径(图片的显示高度和文字高度一致)
- 在textView中的光标位置插入表情或者文字
- 移动开发----支持Android图文混排、文字环绕图片等效果:MixtureTextView
- MixtureTextView 支持Android图文混排、文字环绕图片等效果
- 在Textview中获取指定文字位置(兼顾网址链接和emoji表情),并在其附近展示图片
- Android(java)学习笔记139:在TextView组件中利用Html插入文字或图片
- 安卓TextView显示图片与文字作为底部菜单
- TextView中显示不同颜色的文字和图片
- Android textview获取html文字 + 图片显示
- android launcher主菜单显示图片和文字之BubbleTextView
- android textView设置html,解决图片和文字不对齐的问题
- TextView显示HTML文本时<IMG>标签指定图片的显示处理
- TextView图文混排图片被截断的问题以及Android省略号只有一个点的问题
- Android TextView 嵌套图片及其点击,TextView 部分文字点击,文字多颜色
- android TextView中图片和文字的灰显
- Android学习札记48:将TextView中的指定文字转换为表情显示
- android textview混合显示图片和文字