Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic three-rect selection #30

Merged
merged 1 commit into from
Jul 8, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion Source/StyledTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,37 @@ open class StyledTextView: UIView {

let range = NSRange(location: min, length: max - min)

var firstRect: CGRect = CGRect.null
var bodyRect: CGRect = CGRect.null
var lastRect: CGRect = CGRect.null

let path = UIBezierPath()

renderer.layoutManager.enumerateEnclosingRects(
forGlyphRange: range,
withinSelectedGlyphRange: NSRange(location: NSNotFound, length: 0),
in: renderer.textContainer
) { (rect, stop) in
path.append(UIBezierPath(roundedRect: rect.insetBy(dx: -2, dy: -2), cornerRadius: 3))
if firstRect.isNull {
firstRect = rect
} else if lastRect.isNull {
lastRect = rect
} else {
// We have a lastRect that was previously filled, now it needs to be dumped into the body
bodyRect = lastRect.intersection(bodyRect)
// and save the current rect as the new "lastRect"
lastRect = rect
}
}

if !firstRect.isNull {
path.append(UIBezierPath(roundedRect: firstRect.insetBy(dx: -2, dy: -2), cornerRadius: 3))
}
if !bodyRect.isNull {
path.append(UIBezierPath(roundedRect: bodyRect.insetBy(dx: -2, dy: -2), cornerRadius: 3))
}
if !lastRect.isNull {
path.append(UIBezierPath(roundedRect: lastRect.insetBy(dx: -2, dy: -2), cornerRadius: 3))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up: put -2 and 3 into local vars

}

highlightLayer.frame = bounds
Expand Down