SwiftUI Label and Button Style... Note

SwiftUI Label and Button Style View Modifiers

The article discusses creating custom SwiftUI Button and Label styles, including an adaptive label style that switches to a vertical layout for compact horizontal size classes and a custom capsule button style with a filled yellow capsule and monospaced font design. These styles can be applied to views using the .labelStyle and .buttonStyle modifiers. However, it is less common to create convenience extensions on LabelStyle or ButtonStyle, which would allow for a more compact form of the modifier. To achieve this, the article suggests extending ButtonStyle and LabelStyle, similar to how Apple defines built-in styles. This involves adding a static property or function to the extension that returns an instance of the custom style. For example, the capsule button style can be extended with a static property capsule that returns a CapsuleButtonStyle instance. This allows the style to be applied using the compact form .buttonStyle(.capsule). If the custom style has parameters, such as a configurable color, the extension can be modified to include a function that takes the parameter and returns an instance of the custom style. This enables the style to be applied with the parameter, such as .buttonStyle(.capsule(.orange)). The article provides examples of how to extend ButtonStyle and LabelStyle to create convenient custom styles.