从Swift中的UITextView给出NSRange,我如何检索将其包裹的矩形的中心坐标? 我在这里能找到的所有答案都是针对Objective-c的。
答案 0 :(得分:0)
let range: NSRange = (txtView.text as NSString).range(of: text)
let beginning: UITextPosition? = txtView.beginningOfDocument
let start: UITextPosition? = txtView.position(from: beginning!, offset: range.location)
let end: UITextPosition? = txtView.position(from: start!, offset: range.length)
let textRange: UITextRange? = txtView.textRange(from: start!, to: end!)
let rect: CGRect = txtView.firstRect(for: textRange!)