Swift 常用功能方法封装 2

这篇博客介绍了Swift中的一些实用功能,包括弹窗选择、弹窗输入交互、修改状态栏颜色、时间戳转换时间字符串、设置带颜色的输入框占位符、时间选择器、MD5加密、计算UILabel高度、条件组合检查、获取应用版本号以及获取凌晨时间的方法和用法。
摘要由CSDN通过智能技术生成
  1. 弹窗选择
// 弹窗选择
func alertSheet(_ title: String?, _ message: String?, list: [String], result: @escaping ((_ index: Int) -> ())) {
   
    let current = UIViewController.current()
    let alert = UIAlertController(title: title, message: message, preferredStyle: .actionSheet)
    var index = 0
    for title in list {
   
        let action = UIAlertAction(title: title, style: .default, handler: {
    (action) in
            let tag = action.info?["tag"] as! Int
            result(tag)
        })
        action.info = ["tag": index]
        alert.addAction(action)
        index += 1
    }
    alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
    current?.present(alert, animated: true, completion: nil)
}

使用方式:
在这里插入图片描述
2.弹窗输入

//弹窗输入 secret input 是密码输入类型,两种输入类型的结果会拼接在返回的contents里,按序取
func alertInput(_ title: String?, _ message: String?, inputPlaceholders: [String]?, secretInputPlaceholders: [String]?, userResponse: @escaping ((_ entry: Bool, _ inputContents: [String]) -> ())) {
   
    let current = UIViewController.current()
    let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)

    if inputPlaceholders != nil {
   
        for placeholder in inputPlaceholders! {
   
            alert.addTextField {
    (tf) in
                tf.placeholder = placeholder
            }
        }
    }
    if secretInputPlaceholders != nil {
   
        for placeholder in secretInputPlaceholders! {
   
            alert.addTextField {
    (tf) in
                tf.placeholder = placeholder
                tf.isSecureTextEntry = true
            }
        }
    }

    alert.addAction(UIAlertAction(title: "取消", style: .cancel, handler: {
    (action) in
        userResponse(false, [])
    }))
    alert.addAction(UIAlertAction(title: "确定", style: .default, handler: {
    (action) in
        var contents = [String]()
        for tf in alert.textFields ?? [] {
   
            contents.append(tf.text ?? "")
        }
        userResponse(true, contents)
    }))
    current?.present(alert, animated: true, completion: nil)
}

其他相关的简单弹窗交互

// 弹窗警告
func alert(_ title: String?, _ message: String?) {
   
    DispatchQueue.main.async {
   
        let current = UIViewController.current()
        let alert = UIAlertController(tit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值