FontModifier.swift 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. extension View {
  2. /// Sets the font of contained text. Can be overridden by other font
  3. /// modifiers within the contained view, unlike other font-related
  4. /// modifiers such as ``View/fontWeight(_:)`` and ``View/emphasized()``
  5. /// which override the font properties of all contained text.
  6. ///
  7. /// - Parameter font: The font to set.
  8. public func font(_ font: Font) -> some View {
  9. EnvironmentModifier(self) { environment in
  10. environment.with(\.font, font)
  11. }
  12. }
  13. /// Overrides the font weight of any contained text.
  14. ///
  15. /// - Parameter weight: The font weight to set. If `nil`, this modifier
  16. /// does nothing.
  17. public func fontWeight(_ weight: Font.Weight?) -> some View {
  18. EnvironmentModifier(self) { environment in
  19. environment.with(\.fontOverlay.weight, weight)
  20. }
  21. }
  22. /// Overrides the font design of any contained text.
  23. ///
  24. /// - Parameter design: The font design to set. If `nil`, this modifier
  25. /// does nothing.
  26. public func fontDesign(_ design: Font.Design?) -> some View {
  27. EnvironmentModifier(self) { environment in
  28. environment.with(\.fontOverlay.design, design)
  29. }
  30. }
  31. /// Forces any contained text to be bold, or if the a contained font is a
  32. /// ``Font/TextStyle``, forces the style's emphasized weight to be used.
  33. ///
  34. /// Deprecated and renamed for clarity. Use ``fontWeight(_:)`` to make text
  35. /// bold.
  36. @available(
  37. *,
  38. deprecated,
  39. message: "Use View.emphasized() instead",
  40. renamed: "View.emphasized()"
  41. )
  42. public func bold() -> some View {
  43. emphasized()
  44. }
  45. /// Forces any contained text to become emphasized. For text that uses
  46. /// ``Font/TextStyle``-based fonts, this means using the text style's
  47. /// emphasized weight. For all other text, this means using
  48. /// ``Font/Weight/bold``.
  49. public func emphasized() -> some View {
  50. EnvironmentModifier(self) { environment in
  51. return environment.with(
  52. \.fontOverlay.emphasize,
  53. true
  54. )
  55. }
  56. }
  57. /// Forces any contained text to become italic.
  58. public func italic() -> some View {
  59. EnvironmentModifier(self) { environment in
  60. return environment.with(
  61. \.fontOverlay.italicize,
  62. true
  63. )
  64. }
  65. }
  66. }