I’ve the next UITextView subclass:
class CustomTextView: UITextView {
required init?(coder: NSCoder) {
tremendous.init(coder: coder)
customInit()
}
override init(body: CGRect, textContainer: NSTextContainer?) {
tremendous.init(body: body, textContainer: textContainer)
customInit()
}
func customInit(){
contentMode = .redraw
isSelectable = true
isEditable = false
isScrollEnabled = false
let padding = textContainer.lineFragmentPadding
textContainerInset = UIEdgeInsets(prime: 0, left: -padding, backside: 0, proper: -padding)
}
override func draw(_ rect: CGRect) {
tremendous.draw(rect)
let vary = NSRange(location: 10, size: 5)
let starting = beginningOfDocument
let begin = place(from: starting, offset: vary.location)
let finish = place(from: begin!, offset: vary.size)
let textRange = textRange(from: begin!, to: finish!)
let frames = selectionRects(for: textRange!)
print("draw!!! (frames.first?.rect)") // after scrolling, this prints: draw!!! Elective((inf, inf, 0.0, 0.0))
frames.forEach { body in
let bpath = UIBezierPath(rect: body.rect)
UIColor.pink.set()
bpath.stroke()
}
}
}
This CustomTextView
is in every cell of a UITableView
and the attributedText is about as such:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell") as! Cell
cell.myTextView.attributedText = gadgets[indexPath.row].0
cell.layoutIfNeeded()
cell.myTextView.setNeedsDisplay()
print("setNeedsDisplay!!!")
return cell
}
The primary time when the desk is displayed, it really works effective. Nonetheless, every cell which will get displayed after scrolling not has the rectangles. After some debugging, I discovered that the rects that are returned from selectionRects(for: textRange!)
after scrolling are all invalid with infinite
values within the rect. After scrolling, it prints:
draw!!! Elective((inf, inf, 0.0, 0.0))
What am I doing unsuitable?
Screenshot (discover the pink blocks aren’t there within the backside few cells):