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, 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, 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, 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.. [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.. 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)