-
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.
- Loading branch information
1 parent
4f5dbad
commit 11e8629
Showing
3 changed files
with
66 additions
and
63 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
Sources/DAWNText2/Entity/NSTextContentStorageCoordinator.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,43 @@ | ||
import UIKit | ||
|
||
final class NSTextContentStorageCoordinator: NSObject, NSTextContentStorageDelegate { | ||
let traitCollection: UITraitCollection | ||
|
||
init(traitCollection: UITraitCollection) { | ||
self.traitCollection = traitCollection | ||
} | ||
|
||
func textContentStorage(_ textContentStorage: NSTextContentStorage, textParagraphWith range: NSRange) -> NSTextParagraph? { | ||
var paragraphWithDisplayAttributes: NSTextParagraph? = nil | ||
let originalText = textContentStorage.textStorage!.attributedSubstring(from: range) | ||
|
||
paragraphWithDisplayAttributes = NSTextParagraph( | ||
attributedString: originalText.colorResolved(traitCollection) | ||
) | ||
return paragraphWithDisplayAttributes | ||
} | ||
} | ||
|
||
extension NSAttributedString { | ||
fileprivate func colorResolved(_ traitCollection: UITraitCollection) -> NSAttributedString { | ||
let attributedString = NSMutableAttributedString(attributedString: self) | ||
let range = NSRange(location: 0, length: attributedString.length) | ||
attributedString.enumerateAttributes(in: range) { attributes, range, _ in | ||
if let color = attributes[.foregroundColor] as? UIColor { | ||
attributedString.addAttribute( | ||
.foregroundColor, | ||
value: color.resolvedColor(with: traitCollection), | ||
range: range | ||
) | ||
} else { | ||
// default text color | ||
attributedString.addAttribute( | ||
.foregroundColor, | ||
value: UIColor.label.resolvedColor(with: traitCollection), | ||
range: range | ||
) | ||
} | ||
} | ||
return attributedString | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
Sources/DAWNText2/Entity/NSTextLayoutManagerCoordinator.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,23 @@ | ||
import UIKit | ||
|
||
final class NSTextLayoutManagerCoordinator: NSObject, NSTextLayoutManagerDelegate { | ||
let tintColor: UIColor | ||
let buttonShapesEnabled: Bool | ||
|
||
init(tintColor: UIColor, buttonShapesEnabled: Bool) { | ||
self.tintColor = tintColor | ||
self.buttonShapesEnabled = buttonShapesEnabled | ||
} | ||
|
||
func textLayoutManager( | ||
_ textLayoutManager: NSTextLayoutManager, | ||
renderingAttributesForLink link: Any, | ||
at location: any NSTextLocation, | ||
defaultAttributes renderingAttributes: [NSAttributedString.Key : Any] = [:] | ||
) -> [NSAttributedString.Key : Any]? { | ||
var defaultAttributes = renderingAttributes | ||
defaultAttributes[.foregroundColor] = tintColor | ||
defaultAttributes[.underlineStyle] = buttonShapesEnabled ? NSUnderlineStyle.single.rawValue : nil | ||
return defaultAttributes | ||
} | ||
} |
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