-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the rich text alignment handling to not have any platform br…
…anching
- Loading branch information
1 parent
6710e4f
commit 3c0a29e
Showing
7 changed files
with
119 additions
and
149 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
47 changes: 47 additions & 0 deletions
47
Sources/RichTextKit/Component/RichTextViewComponent+Ranges.swift
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,47 @@ | ||
// | ||
// RichTextViewComponent+Ranges.swift | ||
// RichTextKit | ||
// | ||
// Created by Daniel Saidi on 2024-02-14. | ||
// | ||
|
||
import Foundation | ||
|
||
extension RichTextViewComponent { | ||
|
||
/// Get the line range at a certain text location. | ||
func lineRange(at location: Int) -> NSRange { | ||
guard | ||
let manager = layoutManagerWrapper, | ||
let storage = textStorageWrapper | ||
else { return NSRange(location: NSNotFound, length: 0) } | ||
let string = storage.string as NSString | ||
let locationRange = NSRange(location: location, length: 0) | ||
let lineRange = string.lineRange(for: locationRange) | ||
return manager.characterRange(forGlyphRange: lineRange, actualGlyphRange: nil) | ||
} | ||
|
||
/// Get the line range for a certain text range. | ||
func lineRange(for range: NSRange) -> NSRange { | ||
|
||
// Use the location-based logic if range is empty | ||
if range.length == 0 { | ||
return lineRange(at: range.location) | ||
} | ||
|
||
guard let manager = layoutManagerWrapper else { | ||
return NSRange(location: NSNotFound, length: 0) | ||
} | ||
|
||
var lineRange = NSRange(location: NSNotFound, length: 0) | ||
manager.enumerateLineFragments( | ||
forGlyphRange: range | ||
) { (_, _, _, glyphRange, stop) in | ||
lineRange = glyphRange | ||
stop.pointee = true | ||
} | ||
|
||
// Convert glyph range to character range | ||
return manager.characterRange(forGlyphRange: lineRange, actualGlyphRange: nil) | ||
} | ||
} |
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
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