SwiftToastAlert

GitHub License SPM compatible

目录

  1. 简介
  2. 如何使用
  3. 作者

简介

这个软件包专为 iOS 16+ 和 SwiftUI 设计,为您的用户提供简洁的 Toast 警报。凭借用户友好的 API 和完整的设计自定义,您可以完全控制警报的外观和感觉。

这是第一个 API interactiveToastAlert 的预览

Screen.Recording.2024-08-02.at.5.15.27.PM.mov

第二个 API toastAlert 的预览

Screen.Recording.2024-08-02.at.5.15.57.PM.mov

我乐于接受建议,所以如果您有任何新功能想法或错误,请创建一个新 issue,让我们一起改进它 💪🏻。

如何使用

为了尽可能简化 API,我使用 viewModifiers 创建了它。我创建了两种类型的 API,第一种是 toastAlert,它代表您选择的带有圆角矩形背景的图标图像,并在 3 秒后消失,就像这个一样

Screen.Recording.2024-08-02.at.5.15.57.PM.mov

第二种 API 是 interactiveToastAlert,它也做与 toastAlert 相同的事情,但如果用户点击它,它将展开以显示有关错误或您想告诉用户的任何消息的更多信息,并且在再次点击后,它会收缩并消失,就像这个一样

Screen.Recording.2024-08-02.at.5.15.27.PM.mov

toastAlert

使用 toastAlert 非常简单,它采用 isPresented 作为标志,指示何时显示和消失 toast,positionAlertPosition 类型,表示警报 toast 的位置,background 表示 toast 的背景视图,采用 foregroundStyle 接受的任何内容,foregroundStyleShapeStyle 类型,alertIconAppearance 是返回 AlertIconApperance 类型的闭包,表示将在警报中显示的图标及其外观。例如

struct SwiftUIView: View {
  @State private var isPresented: Bool = false
  
  var body: some View {
    ZStack {
      Color.white.ignoresSafeArea()
      Button("Hello, World 🌍") {
        isPresented.toggle()
      }.foregroundStyle(.black)
    }
    .toastAlert(
      isPresented: $isPresented,
      position: .top,
      background: .black) {
        AlertIconAppearance(image: Image(systemName: "checkmark"), color: .green, font: .system(size: 16, weight: .bold))
      }
 }
}

输出

Screen.Recording.2024-08-02.at.5.15.57.PM.mov

警告

不要在将 isPresented 参数设置为 true 后将其更改为 false,API 会自行更改该值,并且自行进行此更改可能会导致意外行为。

interactiveToastAlert

此 API 采用与 toastAlert 相同的参数,但多了一个额外的参数 alertTextAppearancealertTextAppearance 是一个闭包,它返回包含显示的文本及其外观的 AlertTextAppearance 实例。例如

struct SwiftUIView: View {
  @State private var isPresented: Bool = false
  
  var body: some View {
    ZStack {
      Color.white.ignoresSafeArea()
      Button("Hello, World 🌍") {
        isPresented.toggle()
      }.foregroundStyle(.black)
    }
    .interactiveToastAlert(isPresented: $isPresented, position: .topTrailing, background: .black) {
      AlertTextAppearance(text: "Replace `var` with `let`", color: .white, font: .system(size: 16, weight: .bold))
    } alertIconAppearance: {
      AlertIconAppearance(image: Image(systemName: "checkmark"), color: .green, font: .system(size: 16, weight: .bold))
    }
 }
}

输出

Screen.Recording.2024-08-02.at.5.15.27.PM.mov

注意

不要增加警报中显示的文本超过 2 行,否则它将显示为 ...

AlertIconApperance

此类型表示将在警报中显示的图标及其外观。它包含三个属性 image: Image 表示将在警报中显示的图标,它可以是 SF 符号或来自资源目录的图像,color: Color? 表示警报中图标的颜色,它用于自定义 SF 符号外观,font: Font? 用于自定义图标的大小和粗细(如果它是 SF 符号)。

/// This type represent the icon that will be displayed in the alert and it's appearance.
public struct AlertIconAppearance {
  /// This represent the icon that will be displayed in the alert, it can be SF symbol or image from the asset catalog.
  public let image: Image
  /// This is the color of the icon in the alert, it is used for SF symbol only.
  public let color: Color?
  /// The font properties is used to customize the size and weight of the icon if it was SF symbol
  public let font: Font?
  
  /// if you are not using SF symbol, don't init the color or the font property.
  public init(image: Image, color: Color? = nil, font: Font? = nil) {
    self.image = image
    self.color = color
    self.font = font
  }
}

AlertTextApperance

此类型表示将在警报中显示的文本及其外观。它包含三个属性 text: String 表示点击时在警报中显示的文本,color: Color 表示显示的文本的颜色,font: Font 表示显示的文本的属性,其中包括 text 的大小、粗细和字体类型。

/// This represent the text that will be displayed in the alert toast with it's properties.
public struct AlertTextAppearance {
  /// The displayed text in the toast alert.
  public let text: String
  /// The color of the displayed ``text``.
  public let color: Color
  /// The font properties of the displayed text, which contains size, weight and font of the ``text``.
  public let font: Font
  
  public init(text: String, color: Color, font: Font = .system(size: 16, weight: .bold)) {
    self.text = text
    self.color = color
    self.font = font
  }
}

AlertPosition

AlertPosition 是一个 enum,包含 toast 警报可用的可能位置。

/// This enum represent the possible position options for the toast alert.
public enum AlertPosition {
  case topLeading
  case top
  case topTrailing
  
  internal var alignment : Alignment {
    switch self {
    case .topLeading:
        .topLeading
    case .top:
        .top
    case .topTrailing:
        .topTrailing
    }
  }
}

这就是全部内容!🚀 祝您使用这款 Swifty 软件包愉快。

作者

这个软件包由 Eng.Omar Elsayed 创建,旨在帮助 iOS 社区,让他们的生活更轻松。要联系我,请发送电子邮件至 eng.omar.elsayed@hotmail.com