Mcrich23 工具包

这是一个我制作的软件包,包含许多提升生活品质的功能。

要求

安装

安装 Mcrich23 工具包的首选方式是通过 Swift 包管理器

  1. 在 Xcode 中,打开您的项目并导航至 FileAdd Packages...
  2. 粘贴仓库 URL (https://github.com/Mcrich23/Mcrich23-Toolkit) 并点击 Next
  3. 对于 Rules,选择 Up To Next Minor Version (并将基础版本设置为 0.6.1)。
  4. 点击 Finish
  5. 勾选 Mcrich23-Toolkit
  6. 点击 Add To Project

目录

要求

安装

SwiftUI 函数
UIKit 函数
其他

SwiftUI 函数

ScrollCapsuleMultiFilter

类型

SwiftUI 视图

描述

一个美观的水平滚动多重过滤器 UI

图像

CapsuleMultiFilter Image

示例

 ScrollCapsuleMultiFilter(menuContent: .constant({ // Passes in the view for the plus button menu. Must use .constant() so that the view updates.
        VStack {
            ForEach(viewModel.filterOpt, id: \.self) { text in
                if !viewModel.filter.contains(text) {
                    Button {
                        viewModel.filter.append(text)
                    } label: {
                        Text(text)
                    }
                }
            }
        }}),
    opt: $viewModel.filterOpt, //Use an array that are the same options as in the menu
    selected: $viewModel.filter //An array of currently selected filters
)

AdaptiveCapsuleMultiFilter

类型

SwiftUI 视图

描述

一个美观的自动调整大小的多重过滤器 UI

图像

CapsuleMultiFilter Image

示例

AdaptiveCapsuleMultiFilter(
    "Filter: ", // The label you want next to the capsule filters
    menuContent: .constant({ // Passes in the view for the plus button menu. Must use .constant() so that the view updates.
        VStack {
            ForEach(viewModel.filterOpt, id: \.self) { text in
                if !viewModel.filter.contains(text) {
                    Button {
                        viewModel.filter.append(text)
                    } label: {
                        Text(text)
                    }
                }
            }
        }}), opt: $viewModel.filterOpt, // Use an array that are the same options as in the menu selected:$viewModel.filter) // An array of currently selected filters

OnboardingScreen

类型

SwiftUI 视图

描述

一个用于欢迎用户或展示“新功能”屏幕的界面。

图像

OnboardingScreen Image (Individual) OnboardingScreen Image (Steps)

示例

单个

OnboardingScreen(
    titleIcon: .systemImage(named: "plus"), // An icon to go next to the title
    titleIconColor: .yellow, // Color for the icon next to the title
    title: "Hello World", // Title
    subtitle: "Lorem Ipsum", // Subtitle (leave blank for it to dissapear)
    cells: .individual([
        FeatureCell(
            image: .systemImage(named: "hand"), // An icon next to the cell
            imageColor: .red, // Color for the icon next to the cell
            title: "Title", // Title
            subtitle: "Subtitle" // Subtitle/Description (leave blank for it to dissapear)
        )
    ])
)

步骤

let steps = [ Text(NSLocalizedString("welcome title 1", comment: "")).font(.body),
              Text(NSLocalizedString("welcome title 2", comment: "")).font(.body),
              Text(NSLocalizedString("welcome title 3", comment: "")).font(.body),
              Text(NSLocalizedString("welcome title 4", comment: "")).font(.body)]

let indicationTypes = [
    StepperIndicationType
        .custom(CircledIconView(image: Image(systemName: "plus"), width: 50)),
        .custom(CircledIconView(image: Image(systemName: "hand.draw"), width: 50)),
        .custom(CircledIconView(image: Image(systemName: "hand.tap"), width: 50)),
        .custom(CircledIconView(image: Image(systemName: "eye"), width: 50))
]
...

OnboardingScreen<Content>(
   titleIcon: .systemImage(named: "plus"), // An icon to go next to the title
   titleIconColor: .yellow, // Color for the icon next to the title
   title: "Hello World", // Title
   subtitle: "Lorem Ipsum", // Subtitle (leave blank for it to dissapear)
   cells:
       .steps(
           StepperViewOnboarding(
               steps: steps, // All the steps for the onboarding
               indicationTypes: indicationTypes, // What goes next to the step text
               lineOptions: .custom(1, Colors.blue(.teal).rawValue) // All the different line options
           )
       )
)

有关 Steps 函数的更多信息,请参阅 badrinathvm/StepperView

SwiftUIAlert

类型

提示框

描述

SwiftUI 的默认提示框

示例

show

SwiftUIAlert.show(title: "Hello Word!", //Alert Title
                    message: "Lorem Ipsum", //Alert Message
                    preferredStyle: .alert, //Style (alert or action sheet)
                    actions: [UIAlertAction(title: "Done", //Action Title
                                            style: .default, //Action Style (default, destructive, dismiss)
                                            handler: {_ in}) //Handler for choosing that action
                    ]
)

textfieldShow

SwiftUIAlert.textfieldShow(title: "Test", //Alert Title
                            message: "Hello World!", //Alert Message
                            preferredStyle: .alert, //Style (alert or action sheet)
                            textfield: AlertTextfield(text: $text, //Textfield Text
                                                        placeholder: "", //Textfield Placeholder
                                                        clearButtonMode: .whileEditing, //When to show clear butt(always, whileEditing, unlessEditing, or never)
                                                        enablesReturnKeyAutomatically: true, //Show return key keyboard
                                                        disableAutocorrection: false, //Disable Autocorrection?
                                                        autocapitalization: .sentences, //Autocapitalization (nonsentances, allCharacter, or words)
                                                        keyboardType: .default, //The type of keyboard
                                                        returnKeyType: .default, //Type of return key on keyboard
                                                        isSecureTextEntry: .no, //Is Secure Textfield? (.yes(UITextInputPassordRules) or .no)
                                                        dismissKeyboardOnReturn: true), //Dismiss keyboard on return?
                            actions: [UIAlertAction(title: "Done", style: .default)]
)

contextMenu

类型

SwiftUI 视图修饰符

描述

使用 UIKit 元素来扩展上下文菜单的功能。

示例

展开

PreviewContextMenu(
       navigate: .expand, // Expands view
       destination: ContentView2(), // View to preview
       menu: {
           let openView = UIAction(title: "Open", image:       UIImage(systemName: "arrow.right")) { _ in // Item for menu
               showView.toggle()
           }
           return UIMenu(
               title: "", // Menu Title
               children: [ // Menu Items
                   openView
               ]
           )
       }
   )

自定义

.contextMenu(PreviewContextMenu(
       navigate: .custom({ // Dismisses view and lets you run custom function
           showView.toggle()
       }),
       destination: ContentView2(), // View to preview
       menu: {
           let openView = UIAction(title: "Open", image:       UIImage(systemName: "arrow.right")) { _ in // Item for menu
               showView.toggle()
           }
           return UIMenu(
               title: "", // Menu Title
               children: [ // Menu Items
                   openView
               ]
           )
       }
   )
)

CardView

类型

SwiftUI 视图

描述

类似于 App Store 中的卡片。

图像

CardViewOpenCard

示例

CardView(
    showHeader: .yes( // Wheather header should be visible
        headerTitle: "Title", // Title on the header
        headerSubtitle: "Subtitle", // Subtitle on the header
        headerSubtitleLocation: .below // If Subtitle is above or below the Title
    ),
    cards: $cards, // All the cards in the view
    showCreateButton: .yes( // Wheather create button should be visible
        create: { // Action when create (Plus Button) is tapped
            showCreateTicket.toggle()
        }
    ),
    maxWidth: 428, // The maximum width for cards
    selectedCards: { // Action called on selection of a card
        print("Selected Card")
    },
    deselectedCards: { // Action called on deselection of a card
        print("Deselected Card")
    }
)

ShareSheet

类型

ShareSheet

描述

SwiftUI 的系统分享表单。

图像

ShareSheet

示例

Mcrich23_Toolkit.presentShareSheet(
    activityItems: ["Hello World"], // Items to share
    excludedActivityTypes: [] // Applications to exclude from share sheet
)

onRotate

类型

SwiftUI 视图修饰符

描述

在设备旋转时运行代码。

示例

@State var orientation = UIDevice.current.orientation
var body: some view {
    VStack {
        if orientation == .portrait {
            Text("Hello World")
        }
    }
    .onRotate { newOrientation in
        orientation = newOrientation // Updates the orientation variable
    }
}

getTopVC

类型

函数

描述

获取顶层视图控制器,以便在当前视图而不是根视图上执行 uikit 函数

示例

Mcrich23-Toolkit.getTopVC { vc in
    vc.present(view, animated: true)
}

topVC

类型

UIViewController

描述

获取顶层视图控制器,以便在当前视图而不是根视图上执行 uikit 函数

示例

Mcrich23_Toolkit.topVC.present {
    EmptyView()
}

onWillDissapear

类型

SwiftUI 视图修饰符

描述

在视图将要消失但实际消失之前运行代码。

示例

var body: some View {
    Text("This view has dissapeared\(viewModel.dissapearCount)times!")
    .onWillDissapear {
        viewModel.dissapearCount += 1
    }
}

ConvertedGlyphImage

类型

SwiftUI 图像

描述

将 GlyphImage 转换为 SwiftUI 图像

示例

ConvertedGlyphImage(GlyphImage: $GlyphImage, defaultIcon: Image(systemName: "apps.iphone") { image in
    image
        .resizable()
        .aspectRatio(contentMode: .fit)
        .foregroundColor(.primary)
}

openUrl

类型

函数

描述

打开一个 URL

示例

Mcrich23_Toolkit.openUrl(url: url)

UIKit 函数

InteracteractiveLinkLabel

类型

UILabel

描述

使用 Mcrich23 工具包的这个组件轻松地为您的 UILabel 添加超链接

示例

let label: InteractiveLinkLabel = {
    let label = InteractiveLinkLabel()
    
    let firstChunk = NSMutableAttributedString(string: "Hello, my name is Morris, you cancheck out my website", attributes: nil) // Just text
    let website = NSMutableAttributedString(string: "here",attributes:[NSAttributedString.Key.link: URL(string: "https://mcrich23.com")!]) //Hyperlinked word
    
    //Put it together
    let fullAttributtedText = NSMutableAttributedString()
    fullAttributtedText.append(firstChunk)
    fullAttributtedText.append(tos)
    
    label.attributedText = fullAttributtedText
    label.numberOfLines = 0
    label.sizeToFit()
    label.translatesAutoresizingMaskIntoConstraints = false
    label.isUserInteractionEnabled = true
    label.customUrlHandler = { url in // Open url in a custom way, Note: you may need todeclare in viewDidLoad
        let safari = SFSafariViewController(url: url)
        self.present(safari, animated: true)
    }
    
    return label
}()

InteracteractiveLinkTextView

类型

UITextView

描述

使用 Mcrich23 工具包的这个组件轻松地为您的 UITextView 添加超链接

示例

let textView: InteractiveLinkLabel = {
    let textView = InteractiveLinkLabel()
    
    let firstChunk = NSMutableAttributedString(string: "Hello, my name is Morris, you cancheck out my website", attributes: nil) // Just text
    let website = NSMutableAttributedString(string: "here",attributes:[NSAttributedString.Key.link: URL(string: "https://mcrich23.com")!]) //Hyperlinked word
    
    //Put it together
    let fullAttributtedText = NSMutableAttributedString()
    fullAttributtedText.append(firstChunk)
    fullAttributtedText.append(tos)
    
    // Modifiers
    textView.attributedText = fullAttributtedText
    textView.numberOfLines = 0
    textView.sizeToFit()
    textView.translatesAutoresizingMaskIntoConstraints = false
    textView.isUserInteractionEnabled = true
    textView.customUrlHandler = { url in // Open url in a custom way, Note: you may need todeclare in viewDidLoad
        let safari = SFSafariViewController(url: url)
        self.present(safari, animated: true)
    }
    
    return label
}()

其他

GlyphImage

类型

枚举

描述

不同类型的字形,无论是图标还是图像。一个变量用于所有类型。

使用 ConvertedGlyphImage 将其转换为图像

用例

GlyphImage.systemImage(named: "x.circle") // Uses SF Symbols
GlyphImage.systemImage(named: "person1") // Uses Assets.xcassets
GlyphImage.remoteImage(url: self.url) // Fetches from url and displays image
GlyphImage.defaultIcon // The default icon that you specify

NetworkMonitor

类型

描述

监视互联网连接并使用信息来修改应用程序行为。

注意:您必须调用 startMonitoring 才能使 NetworkMonitor 开始工作。

用法

开始监视:NetworkMonitor.shared.startMonitoring() (在 AppDelagate 中调用)

停止监视:NetworkMonitor.shared.stopMonitoring()

获取连接类型:NetworkMonitor.shared.connectionType (Unknown 可能意味着已断开连接)