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

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

CoreText(五)-自定義光標

標簽:
Java

在阅读电子书的时候,可能有一个功能是长按一段文字,并出现自定义的光标。

效果图

webp

[email protected]

具体代码如下:

class CustomCursorView: UIView {// 圈圈在下边private var circleOnBottom: Bool = false// 光标颜色var cursorColor: UIColor = .red {
    didSet {
        setNeedsDisplay()
    }
}

init(frame: CGRect, circleOnBottom: Bool) {    super.init(frame: frame)    
    self.circleOnBottom = circleOnBottom
    backgroundColor = UIColor.clear
}

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


override func draw(_ rect: CGRect) {

    let context = UIGraphicsGetCurrentContext()
    cursorColor.set()

    let cursorWidth = bounds.width / 2
 
    context?.addRect(CGRect(x: cursorWidth * 0.5, y: circleOnBottom ? 0 : 1, width: cursorWidth, height: bounds.height - 1))
    context?.fillPath()    if circleOnBottom {
        context?.addEllipse(in: CGRect(x: 0, y: bounds.height - bounds.width, width: bounds.width, height: bounds.width))
    } else {
        context?.addEllipse(in: CGRect(x: 0, y: 0, width: bounds.width, height: bounds.width))
    }

    context?.fillPath()
    cursorColor.set()
}

}

使用方法:

  let leftCursor = CustomCursorView(frame: CGRect(x: 100, y: 100, width: 8, height: 30), circleOnBottom: false)
  view.addSubview(leftCursor)    
  let rightCursor = CustomCursorView(frame: CGRect(x: 200, y: 100, width: 8, height: 30), circleOnBottom: true)
  view.addSubview(rightCursor)



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


點擊查看更多內容
TA 點贊

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

評論

作者其他優質文章

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

100積分直接送

付費專欄免費學

大額優惠券免費領

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

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

幫助反饋 APP下載

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

公眾號

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

舉報

0/150
提交
取消