swift图文混排时,问题相关解决。
2015-10-21 22:01
609 查看
// 从问题出发找需要的源头.
//1.textView.attribute -> 需要 NSAttributeText ->
// ->replaceCharactersInRange()需要范围和图片属性 ->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.replaceCharactersInRange(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
//1.textView.attribute -> 需要 NSAttributeText ->
// ->replaceCharactersInRange()需要范围和图片属性 ->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.replaceCharactersInRange(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
相关文章推荐
- Swift-EasingAnimation
- swift-正则验证手机号码
- swift-计算字符串长度
- Swift UITableView相关功能(八)添加索引
- swift-导航栏添加自定义返回按钮
- swift-UITextfield控件的基本属性设置
- 【备忘】oc 调用 swift
- 关于swift 底部工具栏图标锯齿模糊问题。
- swift 之闭包的学习笔记
- swift(一)天
- Swift UITableView相关功能(七)数据编辑功能
- Swift - 使用CoreLocation实现定位(经纬度、海拔、速度、距离等)
- Swift - 使用CoreLocation实现定位(经纬度、海拔、速度、距离等)
- Swift控制语句之switch-case
- swift 简单实现coreData数据库增删改查和关系表
- 《从零开始学Swift》学习笔记(Day 25)——类和结构体定义
- 《从零开始学Swift》学习笔记(Day 25)——类和结构体定义
- 关于数据保存的方式(沙盒,解档归档(swift方法))
- [Swift]UIKit学习之警告框:UIAlertController和UIAlertView
- OpenStack: Storage 组件 Swift 和 Cinder(转)