亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

CoreText(六)長按一段文字出現光標,并移動光標改變選中文字范圍

標簽:
Java

1. 介绍

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

webp

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 = falseprivate var touchRightCursor = falseprivate 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: - 获取点击位置的两个字符的rangeprivate 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所占用的rectsprivate 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)



作者:三_木子_
链接:https://www.jianshu.com/p/625794ed8d1d


點擊查看更多內容
TA 點贊

若覺得本文不錯,就分享一下吧!

評論

作者其他優質文章

正在加載中
  • 推薦
  • 評論
  • 收藏
  • 共同學習,寫下你的評論
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦
今天注冊有機會得

100積分直接送

付費專欄免費學

大額優惠券免費領

立即參與 放棄機會
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號

舉報

0/150
提交
取消