Skip to content

Instantly share code, notes, and snippets.

@mntone
Last active Dec 27, 2021
Embed
What would you like to do?
Copyright (C) 2021 mntone. All right reserved. This source code is under MIT license.
@available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *)
func coloredPrimaryColor(localized: String.LocalizationValue) -> AttributedString {
var attributedString: AttributedString = AttributedString(localized: localized)
guard let firstBoldRange = attributedString.range(of: "**") else { return attributedString }
let orig: AttributedString = attributedString
attributedString.removeSubrange(firstBoldRange)
guard let nextBoldRange = attributedString.range(of: "**") else { return orig }
attributedString.removeSubrange(nextBoldRange)
var attribute: AttributeContainer = AttributeContainer()
attribute.foregroundColor = .primary
attribute.font = .footnote.bold() // MARK: AttributeContainer を生成するのにオリジナルの Font 情報が必要になる
attributedString[firstBoldRange.lowerBound..<nextBoldRange.lowerBound].mergeAttributes(attribute)
return attributedString
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment