-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make use of NSTextLayoutFragment enumeration
- Loading branch information
1 parent
a2c485d
commit d81d888
Showing
4 changed files
with
73 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#if os(macOS) && !targetEnvironment(macCatalyst) | ||
import AppKit | ||
#elseif os(iOS) || os(visionOS) | ||
import UIKit | ||
#endif | ||
|
||
#if os(macOS) || os(iOS) || os(visionOS) | ||
@available(macOS 12.0, iOS 15.0, *) | ||
extension NSTextLayoutFragment { | ||
public func enumerateLineFragments(with provider: NSTextElementProvider, block: (NSTextLineFragment, CGRect, NSRange) -> Void) { | ||
let origin = layoutFragmentFrame.origin | ||
let location = provider.offset?(from: provider.documentRange.location, to: rangeInElement.location) ?? 0 | ||
|
||
// check to ensure our shift will always be valid | ||
precondition(location >= 0) | ||
precondition(location != NSNotFound) | ||
|
||
for textLineFragment in textLineFragments { | ||
let bounds = textLineFragment.typographicBounds.offsetBy(dx: origin.x, dy: origin.y) | ||
let range = NSRange( | ||
location: textLineFragment.characterRange.location + location, | ||
length: textLineFragment.characterRange.length | ||
) | ||
|
||
block(textLineFragment, bounds, range) | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters