美文网首页傲视苍穹iOS《Objective-C》VIP专题
CoreText(六)长按一段文字出现光标,并移动光标改变选中文

CoreText(六)长按一段文字出现光标,并移动光标改变选中文

作者: 向日葵的夏天_summer | 来源:发表于2018-12-21 18:24 被阅读27次

1. 介绍

在阅读小说的过程中,可能对一段文字长按复制的功能,长按时,出现光标,并可以通过移动光标更改选中文字的范围,具体效果图如下:

2. 实现过程

  • 给view添加长按手势,默认选中两个字符,并显示光标;
  • 在touchesBegan时,记录原始选中的位置;
  • touchesMoved中计算移动的范围并渲染;
  • 主要就在移动过程中,判断移动的范围,计算出范围的rect,并渲染出来;

3. 实现代码

class DrawCursorView: UIView {

private var ctFrame: CTFrame?
private var rects: [CGRect] = [CGRect]()
private var selectedRange = NSRange(location: 0, length: 0)
private var originRange = NSRange(location: 0, length: 0)

// 移动光标
private var isTouchCursor = false
private var touchRightCursor = false
private var touchOriginRange = NSRange(location: 0, length: 0)

private var longPress: UILongPressGestureRecognizer!

private var leftCursor: CustomCursorView!
private var rightCursor: CustomCursorView!

private var attributeString = NSMutableAttributedString(string: "")

override init(frame: CGRect) {
    super.init(frame: frame)
    
    backgroundColor = UIColor.white
    
    if longPress == nil {
        longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressAction(longGesture:)))
        addGestureRecognizer(longPress)
    }
    
    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(tapAction))
    addGestureRecognizer(tapGesture)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let point = touches.first?.location(in: self) else { return }
    guard leftCursor != nil && rightCursor != nil else { return }
    
    if rightCursor.frame.insetBy(dx: -30, dy: -30).contains(point) {
        touchRightCursor = true
        isTouchCursor = true
    } else if leftCursor.frame.insetBy(dx: -30, dy: -30).contains(point) {
        touchRightCursor = false
        isTouchCursor = true
    }

    touchOriginRange = selectedRange
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let point = touches.first?.location(in: self) else { return }
    guard leftCursor != nil && rightCursor != nil else { return }
    
    if isTouchCursor {
        let finalRange = getTouchLocationRange(point: point, str: attributeString.string)
 
        if (finalRange.location == 0 && finalRange.length == 0) || finalRange.location == NSNotFound {
            return
        }
        var range = NSRange(location: 0, length: 0)
        
        if touchRightCursor { // 移动右边光标
            if finalRange.location >= touchOriginRange.location {
                range.location = touchOriginRange.location
                range.length = finalRange.location - touchOriginRange.location + 1
            } else {
                range.location = finalRange.location
                range.length = touchOriginRange.location - range.location
            }
        } else {  // 移动左边光标
            
            if finalRange.location <= touchOriginRange.location {
                range.location = finalRange.location
                range.length = touchOriginRange.location - finalRange.location + touchOriginRange.length

            } else if finalRange.location > touchOriginRange.location {

                if finalRange.location <= touchOriginRange.location + touchOriginRange.length - 1 {
                    range.location = finalRange.location
                    range.length = touchOriginRange.location + touchOriginRange.length - finalRange.location
                } else {
                    range.location = touchOriginRange.location + touchOriginRange.length
                    range.length = finalRange.location - range.location
                }
            }
        }

        selectedRange = range
        rects = getRangeRects(range: selectedRange, ctframe: ctFrame)
        
        // 显示光标
        showCursorView()
        setNeedsDisplay()
    }

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    isTouchCursor = false
    touchOriginRange = selectedRange
}

func rangeMoved(point: CGPoint) {
    
    let finalRange = getTouchLocationRange(point: point, str: attributeString.string)
    if finalRange.location == 0 || finalRange.location == NSNotFound {
        return
    }
    var range = NSRange(location: 0, length: 0)
    range.location = min(finalRange.location, originRange.location)
    if finalRange.location > originRange.location {
        range.length = finalRange.location - originRange.location + finalRange.length
    } else {
        range.length = originRange.location - finalRange.location + originRange.length
    }
    
    selectedRange = range
    rects = getRangeRects(range: selectedRange, ctframe: ctFrame)
    
    // 显示光标
    showCursorView()
    setNeedsDisplay()
}

@objc func tapAction() {
    reset()
}

@objc func longPressAction(longGesture: UILongPressGestureRecognizer) {
    var originPoint = CGPoint.zero
    switch longPress.state {
    case .began:
        originPoint = longGesture.location(in: self)
        originRange = getTouchLocationRange(point: originPoint, str: attributeString.string)
        selectedRange = originRange
        rects = getRangeRects(range: selectedRange, ctframe: ctFrame)
        
        // 显示光标
        showCursorView()
        
        setNeedsDisplay()
        
    case .changed:
        
        let finalRange = getTouchLocationRange(point: longGesture.location(in: self), str: attributeString.string)
        if finalRange.location == 0 || finalRange.location == NSNotFound {
            return
        }
        var range = NSRange(location: 0, length: 0)
        range.location = min(finalRange.location, originRange.location)
        if finalRange.location > originRange.location {
            range.length = finalRange.location - originRange.location + finalRange.length
        } else {
            range.length = originRange.location - finalRange.location + originRange.length
        }
        
        selectedRange = range
        rects = getRangeRects(range: selectedRange, ctframe: ctFrame)
        
        // 显示光标
        showCursorView()
        setNeedsDisplay()
    case .ended:
        print("longPress-Ended")
    case .cancelled:
        print("longPress-Cancelled")
    default:
        break
    }
}

//MARK: - 显示光标
func showCursorView() {
    guard rects.count > 0 else { return }
    let leftRect = rects.first!
    let rightRect = rects.last!
    
    if leftCursor == nil {
        let rect = CGRect(x: leftRect.minX - 4, y: self.bounds.height - leftRect.origin.y - rightRect.height, width: 4, height: leftRect.height)
        leftCursor = CustomCursorView(frame: rect, circleOnBottom: false)
        addSubview(leftCursor)
    } else {
        leftCursor.frame = CGRect(x: leftRect.minX - 4, y: self.bounds.height - leftRect.origin.y - rightRect.height, width: 4, height: leftRect.height)
    }
    if rightCursor == nil {
        let rect = CGRect(x: rightRect.maxX - 2, y: self.bounds.height - rightRect.origin.y - rightRect.height, width: 4, height: rightRect.height)
        rightCursor = CustomCursorView(frame: rect, circleOnBottom: true)
        addSubview(rightCursor)
    } else {
        rightCursor.frame = CGRect(x: rightRect.maxX - 2, y: self.bounds.height - rightRect.origin.y - rightRect.height, width: 4, height: rightRect.height)
    }
}

//MARK: - 隐藏光标
func hideCursorView() {
    if leftCursor != nil {
        leftCursor.removeFromSuperview()
        leftCursor = nil
    }
    if rightCursor != nil {
        rightCursor.removeFromSuperview()
        rightCursor = nil
    }
}

//MARK: - 获取点击位置的两个字符的range
private func getTouchLocationRange(point: CGPoint, str: String = "") -> NSRange {
    var resultRange = NSRange(location: 0, length: 0)
    guard let ctFrame = ctFrame else { return resultRange }
    
    var lines = CTFrameGetLines(ctFrame) as Array
    var origins = [CGPoint](repeating: CGPoint.zero, count: lines.count)
    CTFrameGetLineOrigins(ctFrame, CFRange(location: 0, length: 0), &origins)
    
    for i in 0..<lines.count {
        let line = lines[i] as! CTLine
        let origin = origins[i]
        
        var ascent: CGFloat = 0
        var descent: CGFloat = 0
        
        CTLineGetTypographicBounds(line, &ascent, &descent, nil)
        
        let lineRect = CGRect(x: origin.x, y: self.frame.height - origin.y - (ascent + descent), width: CTLineGetOffsetForStringIndex(line, 100000, nil), height: ascent + descent)
        
        if lineRect.contains(point) {
            
            let lineRange = CTLineGetStringRange(line)
            for j in 0..<lineRange.length {
                
                let index = lineRange.location + j
                
                var offsetX = CTLineGetOffsetForStringIndex(line, index, nil)
                var offsetX2 = CTLineGetOffsetForStringIndex(line, index + 1, nil)
                
                offsetX += origin.x
                offsetX2 += origin.x
                
                let runs = CTLineGetGlyphRuns(line) as Array
                
                for k in 0..<runs.count {
                    let run = runs[k] as! CTRun
                    let runRange = CTRunGetStringRange(run)
                    
                    if runRange.location <= index && index <= (runRange.location + runRange.length - 1) {
                      
                        // 说明在当前的run中
                        var ascent: CGFloat = 0
                        var descent: CGFloat = 0
                        
                        CTRunGetTypographicBounds(run, CFRange(location: 0, length: 0), &ascent, &descent, nil)
                        
                        let frame = CGRect(x: offsetX, y: self.frame.height - origin.y - (ascent + descent), width: (offsetX2 - offsetX) * 2, height: ascent + descent)
                        
                        if frame.contains(point) {
                            // 每次获取两个字符的长度
                            
                            resultRange = NSRange(location: index, length: min(2, lineRange.length + lineRange.location - index))
                        }
                        
                    }
                    
                }
            }
            
        }
    }
    
    return resultRange
}

//MARK: - 获取range所占用的rects
private func getRangeRects(range: NSRange, ctframe: CTFrame?) -> [CGRect] {
    var rects = [CGRect]()
    guard let ctframe = ctframe else { return rects }
    guard range.location != NSNotFound else { return rects }
    
    var lines = CTFrameGetLines(ctframe) as Array
    var origins = [CGPoint](repeating: CGPoint.zero, count: lines.count)
    CTFrameGetLineOrigins(ctframe, CFRange(location: 0, length: 0), &origins)
    
    for i in 0..<lines.count {
        let line = lines[i] as! CTLine
        let origin = origins[i]
        let lineCFRange = CTLineGetStringRange(line)
        
        if lineCFRange.location != NSNotFound {
            let lineRange = NSRange(location: lineCFRange.location, length: lineCFRange.length)
            
            if lineRange.location + lineRange.length > range.location && lineRange.location < (range.location + range.length) {
                
                var ascent: CGFloat = 0
                var descent: CGFloat = 0
                var startX: CGFloat = 0
                
                var contentRange = NSRange(location: range.location, length: 0)
                let end = min(lineRange.location + lineRange.length, range.location + range.length)
                contentRange.length = end - contentRange.location
                
                CTLineGetTypographicBounds(line, &ascent, &descent, nil)
                
                let y = origin.y - descent
                
                startX = CTLineGetOffsetForStringIndex(line, contentRange.location, nil)
                
                let endX = CTLineGetOffsetForStringIndex(line, contentRange.location + contentRange.length, nil)
                
                let rect = CGRect(x: origin.x + startX, y: y, width: endX - startX, height: ascent + descent)
                
                rects.append(rect)
                
            }
            
        }
        
    }
    
    return rects
}

func reset() {
    originRange = NSRange(location: 0, length: 0)
    selectedRange = NSRange(location: 0, length: 0)
    rects.removeAll()
    hideCursorView()
    setNeedsDisplay()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func draw(_ rect: CGRect) {
    attributeString = NSMutableAttributedString(string: "标题特朗普:美国防部长马蒂斯将在明年2月底去职,海外网12月21日电 据美国《国会山报》消息,\r\n当地时间周四(20日)晚间,美国总统特朗普宣布美国国防部长马蒂斯将于明年2月底退休。报道称,该消息恰好在美国白宫宣布从叙利亚撤军之后。特朗普宣布消息时表示:“马蒂斯将于明年2月底退休,在过去担任美国防部长期间,马蒂斯取得突出的工作成果,特别是在购买新的战斗装备方面。此外,特朗普还表示,不久后将会任命新的国防部长。(海外网/李萌)报道称,\r\n该消息恰好在美国白宫宣布从叙利亚撤军之后。特朗普宣布消息时表示:“马蒂斯将于明年2月底退休,在过去担任美国防部长期间,马蒂斯取得突出的工作成果,特别是在购买新的战斗装备方面")
    
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineSpacing = 6
    paragraphStyle.paragraphSpacing = 20
    attributeString.addAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 15), NSAttributedString.Key.foregroundColor: UIColor.black, NSAttributedString.Key.paragraphStyle: paragraphStyle], range: NSRange(location: 0, length: attributeString.length))
    
    
    let context = UIGraphicsGetCurrentContext()
    context?.textMatrix = .identity
    context?.translateBy(x: 0, y: self.bounds.size.height)
    context?.scaleBy(x: 1.0, y: -1.0)
    
    let path = UIBezierPath(rect: self.bounds)
    let framesetter = CTFramesetterCreateWithAttributedString(attributeString)
    let frame = CTFramesetterCreateFrame(framesetter, CFRange(location: 0, length: 0), path.cgPath, nil)
    ctFrame = frame
    CTFrameDraw(frame, context!)
    
    
    guard rects.count > 0 else { return }
    let lineRects = rects.map { rect in
        return CGRect(x: rect.origin.x, y: rect.origin.y, width: rect.width, height: 1)
    }
    let fillPath = CGMutablePath()
    UIColor.blue.withAlphaComponent(0.7).setFill()
    fillPath.addRects(lineRects)
    context?.addPath(fillPath)
    context?.fillPath()
    
}
}

4. 在控制器中使用

  let v = DrawCursorView(frame: CGRect(x: 50, y: NavBarHeight + 20, width: SCREEN_WIDTH - 100, height: SCREEN_HEIGHT - NavBarHeight - BottomSafeAreaHeight - 20))
  view.addSubview(v)

相关文章

  • CoreText(六)长按一段文字出现光标,并移动光标改变选中文

    1. 介绍 在阅读小说的过程中,可能对一段文字长按复制的功能,长按时,出现光标,并可以通过移动光标更改选中文字的范...

  • 一个Android长文本光标选中神器

    Android长文本输入栏神器:通过拖动进度条移动光标或选中文本。 Preview 短按拖动:移动光标长按拖动:选...

  • Linux云计算学习笔记day08

    vim 一、 光标移动操作 h 光标向左移动 l 光标向右移动 j 光标向下移动 k 光标向上移动 gg 1G光标...

  • EditText使用总结

    获取光标位置 在光标处插入字符 删除光标前字符 选中文字 点击EditText获取焦点显示光标且不弹出软键盘 处理...

  • vim setnu-快捷键

    vimh 光标向左移动 l光标向右移动j 光标向下移动 k光标向上移动gg光标移动到文本行首或1G1...

  • Linux59期day08

    1、vim编辑器的快捷键光标移动操作: l:光标向右移动h:光标向左移动j:光标向下移动k:光标向上移动gg或1G...

  • Terminal基础命令

    光标操作control + A //移动光标至行首control + E //移动光标至行尾control + U...

  • 光标操作

    光标移到开头或者结尾 Ctrl+a 光标移动到开始位置Ctrl+e 光标移动到最末尾 光标移动一个单词() ESC...

  • ubuntu16.04或ubuntu17.10bug--长按方向

    问题描述 近期遇到一个问题,RT,在系统中任意可以输入文字的页面,长按方向键光标不能连续移动,有的时候仅能移动几个...

  • vim移动、定位命令与快捷键速查表(简练通俗)

    光标位置移动(方向) 注:也可用小键盘区的方向键移动光标方向,但不建议,因为离得太远了 光标位置移动(行) 光标位...

网友评论

    本文标题:CoreText(六)长按一段文字出现光标,并移动光标改变选中文

    本文链接:https://www.haomeiwen.com/subject/gzevkqtx.html