Skip to content

Commit

Permalink
move files
Browse files Browse the repository at this point in the history
  • Loading branch information
noppefoxwolf committed May 6, 2024
1 parent 4f5dbad commit 11e8629
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 63 deletions.
43 changes: 43 additions & 0 deletions Sources/DAWNText2/Entity/NSTextContentStorageCoordinator.swift
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 Sources/DAWNText2/Entity/NSTextLayoutManagerCoordinator.swift
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
}
}
63 changes: 0 additions & 63 deletions Sources/DAWNText2/Entity/TextLayoutDataFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,66 +94,3 @@ final class TextLayoutDataFactory {
}
}

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
}
}

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
}
}

extension NSAttributedString {
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
}
}

0 comments on commit 11e8629

Please sign in to comment.