Skip to content

Instantly share code, notes, and snippets.

@mntone
Last active December 27, 2021 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mntone/9e0f57964cb30324eec1247e796a5e7c to your computer and use it in GitHub Desktop.
Save mntone/9e0f57964cb30324eec1247e796a5e7c to your computer and use it in GitHub Desktop.
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