SwiftUIGrid 库

GitHub

一个开源库,它为 SwiftUI 库提供扩展,以启用间距/字体网格和其他高级实用工具。

开发为可重用组件,用于 XII 在 iOS、macOS 和 watchOS 应用程序上的各种项目。

安装

Swift Package Manager

  1. 在 Xcode 中,选择“File” > “Swift Packages” > “Add Package Dependency”。
  2. 按照提示操作,使用此仓库的 URL
  3. 选择 SwiftUIGrid 库以添加到你的项目中

依赖项

许可证

请参阅 LICENSE 文件。

内边距扩展 (Source)

extension View {
  func padding(
    top: CGFloat = 0.0,
    trailing: CGFloat = 0.0,
    bottom: CGFloat = 0.0,
    leading: CGFloat = 0.0
  ) -> some View
}

将每个方向的内边距设置为提供的值。如果未提供特定方向的值,则假定为 0.0


extension View {
  func padding(
    vertical: CGFloat = 0.0,
    horizontal: CGFloat = 0.0
  ) -> some View
}

padding(top:trailing:bottom:leading) 的变体,用于设置垂直/水平内边距。


extension View {
  func systemPadding() -> some View
}

View 的内边距重置为系统计算的默认值。

结构化字体定义 (Source)

struct FontFace : Hashable {
  let name: String?
  
  let weight: Font.Weight
  
  let design: Font.Design
  
  init(
    name: String? = nil,
    weight: Font.Weight = .regular,
    design: Font.Design = .default
  )
  
  func toFont(size: CGFloat) -> Font
  
  func toFont<Size : RawRepresentable>(size: Size) -> Font
    where Size.RawValue == CGFloat
}

定义一个可选命名的字体外观,具有特定粗细,允许将其转换为 FontNSAttributedString

应用程序可以将其自定义字体定义为此结构的扩展,例如

extension FontFace {
  static let MyCustomFont = FontFace(name: "MyCustomFont", weight: .light)
  static let MySystemFont = FontFace(weight: .light)
}

FontFace 属性在 iOS 上的支持

extension FontFace {
  func toAttributes(size: CGFloat) -> [NSAttributedString.Key : UIFont]
  
  func toAttributes<Size : RawRepresentable>(
    size: Size
  ) -> [NSAttributedString.Key : UIFont] where Size.RawValue == CGFloat
}

FontFace 属性在 macOS 上的支持

extension FontFace {
  func toAttributes(size: CGFloat) -> [NSAttributedString.Key : NSFont]
  
  func toAttributes<Size : RawRepresentable>(
    size: Size
  ) -> [NSAttributedString.Key : NSFont] where Size.RawValue == CGFloat
}

动画化 FontFace 大小 (Source)

extension View {
  func withAnimatedFont(
    face: FontFace,
    size: CGFloat
  ) -> some View
}

将给定的字体外观/大小样式应用于 View,但允许字体大小可动画化。


extension View {
  func withAnimatedFont<Size : RawRepresentable>(
    face: FontFace,
    size: Size
  ) -> some View where Size.RawValue == CGFloat
}

将给定的字体外观/大小样式应用于 View,但允许字体大小(取自枚举原始值)可动画化。