图文混排

1.NSTextAttachment 实现图文混排

  • 图片附件

  • lineHeight 大致和字体的大小相等

  • bounds ,x,y 决定内部子控件相对远点的位置

  • 图片属性文本

    func textTextKit(){
        let attchment = NSTextAttachment()
        // 图片附件
        attchment.image = UIImage(named: "hehheheh");
        // lineHeight 大致和字体的大小相等
        let height = lable.font.lineHeight
        // frame x,y 决定当前控件,相对父控件的位置
        // bounds ,x,y 决定内部子控件相对远点的位置
        attchment.bounds = CGRect(x: 0, y: -4, width: height, height: height)
        //图片属性文本
        let attst1 = NSAttributedString(attachment: attchment)
        let attrStrM = NSMutableAttributedString(string: "我")
        attrStrM.append(attst1)
        attrStrM.append(NSAttributedString(string: "99999"))
        lable.attributedText = attrStrM
        }
    复制代码

2.TextKit 重绘实现图文混排

  • NSTextStorage 管理NSLayoutManager 对象,字符或者属性变化的时候,通知LayoutManager重新布局并显示文本

  • NSLayoutManager 负责协调布局,显示NSTextStorage对象中保存的字符,将字形设置为一系列NSTextContainer 对象,然后以系列文本视图对象显示

  • NStextContainer 也可以定义一个排除路径,定义一下矩形区域,在layoutSubviews中更新调用

    // 属性文本存储  是 NSMutableAttributedString子类
    private lazy var textStore = NSTextStorage()
    
    // 负责文本“字形”布局
    private lazy var layoutManager = NSLayoutManager()
    
    // 设定文本绘制的的范围
    private lazy var textContainer = NSTextContainer ()
    复制代码

  • 代码实现

  • 在init方法中设置prepareTextSystem

  • 1.准备文本

  • 2.设置对象关系

  • 3.在layoutSubviews指定区域

  • 4.重写drawText 方法,重新绘制 绘制类似油画,后绘制的内容,会把之前绘制的内容覆盖

  • 定义只读属性urlRanges:[NSRange]? 字符串中的网址range

  • 在touchesBegan 中判断点击的index 在不在urlRanges 中,如果在,显示点击变色

  • 获取点击的索引

  • 判断点击的字符 indx 是不是再urlRanges范围内,如果在就高亮

  • 需要重绘,调用setNeedsDisplay函数,但是不是drawrect

3.UITextView 的XIB处理

  • 禁止滚动
  • 拖拽回收键盘

4.切换键盘

  • 自定义view

  • 点击切换,如果是nil显示,如果不是nil,隐藏

  • 刷新键盘 reloadInputViews

5. UITextView 的常用的三个方法

  • 是否为空 hasText() -> Bool

  • 插入字符串 insertText(_ text: String )

  • 向前删除 deleteBackward()

  • 插入字符串 textVIew.replace(<#T##range: UITextRange##UITextRange#>, withText: <#T##String#>)

6.监听键盘的方法

  • 监听键盘通知,

  • 获取键盘高度和键盘动画

  • 设置底部约束高度

  • 更新约束

  • 执行动画

    // 监听键盘通知,
    NotificationCenter.default.addObserver(self, selector: #selector(keybordChanged), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil);
    复制代码
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    复制代码
    guard let  rect = (noti.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
            let duration = (noti.userInfo?[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue else{ return }
    // 设置底部约束高度
    let offsite = view.bounds.height - rect.origin.y
    
    // 更新约束
    viewToButtonH.constant = offsite
    
    // 执行动画
    UIView.animate(withDuration: duration) {
    
        self.view.layoutIfNeeded()
    }
    复制代码

7.图文混排,图片插入文字中

  • 获取表情中的图像属性文本 使用attemnet

  • 设置当前的textview的属性文本,可变的

  • 将图像的属性文本插入到当前的光标位置

  • 记录光标位置

  • 重新设置文本

  • 回复光标位置

    // 获取表情中的图像属性文本 使用attemnet
    
    let imageText =  NSAttributedString(string: "ceshi", attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 12)])
    
    // 设置当前的textview的属性文本,可变的
    
    let attrStrM = NSMutableAttributedString(attributedString: textVIew.attributedText)
    
    // 将图像的属性文本插入到当前的光标位置
    
    attrStrM.replaceCharacters(in: textVIew.selectedRange, with: imageText)
    
    // 记录光标位置
    let range = textVIew.selectedRange
    
    //  重新设置文本
    textVIew.attributedText = attrStrM
    
    // 回复光标位置
    textVIew.selectedRange = NSRange(location: range.location + 1, length: 0)
    
    复制代码

8. 变量可变字符串寻找图片附件

  • 字典中包含 NSAttachment key 说明是图片,否则是文本

    let aAttr =  NSAttributedString(string: "ceshi", attributes: [NSFontAttributeName:UIFont.systemFont(ofSize: 12)])
    
    aAttr.enumerateAttributes(in: NSRange(location: 0, length: aAttr.length), options: []) { (dic, range, _) in
    
        // 字典中包含 NSAttachment key 说明是图片,否则是文本
    
        let arrrment = dic["NSAttachment"]                      
    }
    复制代码

9.UIcode编码

  • uiicode 的编码,展现是啊用UTF8 1-4个字节表示一个字符
  • 实例化字符扫描
  • 从code中 扫描出16进制的数值
  • 使用uint32的数值,生成UTF8 的字符
    let code  = "0x1f633"
    let scanner = Scanner(string: code)
    var result :uint  = 0
    scanner.scanHexInt32(&result)
    let c  = Character(UnicodeScalar(result)!)
    let emojy = String(c)
    print("\(emojy)")
    复制代码
  • uibutton 的设置nil
  • 设置图像,如果图像为nil,会清空图像,避免复用
  • 设置标题

转载于:https://juejin.im/post/5b8f8b82f265da0acc793c53

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值