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

Highlight Part of String #492

Merged
merged 2 commits into from
May 12, 2023
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: 26 additions & 0 deletions Sources/Core/FeaturePrelude/AttributedString+Extra.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,30 @@ extension AttributedString {
public init(_ string: some StringProtocol, foregroundColor: Color) {
self = with(AttributedString(string)) { $0.foregroundColor = foregroundColor }
}

public init(markdown: some StringProtocol, replaceItalicsWith italicsColor: Color) {
let string = String(markdown)
guard let attributed = try? AttributedString(markdown: string) else {
self.init(string)
return
}

self = attributed.replacingAttributes(.italics, with: .foregroundColor(italicsColor))
}
}

extension AttributeContainer {
public static let italics: AttributeContainer = intent(.emphasized)

public static func intent(_ intent: InlinePresentationIntent) -> AttributeContainer {
var result = AttributeContainer()
result.inlinePresentationIntent = intent
return result
}

public static func foregroundColor(_ color: Color) -> AttributeContainer {
var result = AttributeContainer()
result.foregroundColor = color
return result
}
}