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

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)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,284评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,115评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,614评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,671评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,699评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,562评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,309评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,223评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,668评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,859评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,981评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,705评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,310评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,904评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,023评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,146评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,933评论 2 355