DJExtension 1.4.1

DJExtension 1.4.1

jiadawei 维护。



  • 作者:
  • jiadawei

DJExtension

Swift 的扩展,全局函数。一行代码创建组件。

提高代码效率10倍。

主要特性!

  • 一行代码创建组件,如标签、按钮、textField...
  • 使用全局函数代替一些 Apple API。
  • 为 UILabel/UIButton/UIView/UIViewController/UIImage... 创建许多扩展和便捷的初始化方法。

如何安装?

手动方式

只需将 DJExtension 文件夹拖到您的项目中。

这些都已完成,您就可以使用扩展功能。所有函数或常量都以 dj 开头。

Cocoapods

pod 'DJExtension'

在使用它之前,您还应该导入此框架,如下所示:import DJExtension

使用 DJExtension 的理由

使用 DJExtension,您不需要在项目中编写重复和冗余的代码。下面是一些示例。

示例 01 - 创建标签

  • 之前
let label = UILabel()
label.text          = "Label"
label.font          = UIFont.systemFont(ofSize: 11)
label.textColor     = UIColor(red: 123.0/255, green: 123.0/255, blue: 123.0/255, alpha: 1)
label.textAlignment = .left
label.numberOfLines = 1
    
view.addSubview(label)
    
label.snp.makeConstraints { (make) in
    make.center.equalTo(view.snp.center)
    make.width.height.equalTo(100)
}
  • 现在
UILabel(text: "Label", font: dj_systemfont(11), color: dj_rgbGrayColor(123), alignment: .left, lineCount: 1, superView: view) { (make) in
    make.center.equalTo(view.snp.center)
    make.width.height.equalTo(100)
}

参数 alignmentlineCount 有默认值,因此您可以这样编写。

UILabel(text: "Label", font: dj_systemfont(11), color: dj_rgbGrayColor(123), superView: view) { (make) in
    make.center.equalTo(view.snp.center)
    make.width.height.equalTo(100)
}

示例 02 - 创建按钮

  • 之前
let button = UIButton()
button.setTitle("On", for: .normal)
button.setTitle("Off", for: .selected)
button.titleLabel?.font = UIFont(name: "PingFangSC-Regular", size: 11)
button.setTitleColor(UIColor.green, for: .normal)
button.setTitleColor(UIColor.red, for: .selected)
button.backgroundColor = UIColor.white
    
button.addTarget(self, action: #selector(clickSwitch(button:)), for: .touchUpInside)
        
view.addSubview(button)
        
button.snp.makeConstraints { (make) in
    make.center.equalTo(view.snp.center)
    make.width.height.equalTo(100)
}
  • 现在
UIButton(title: "On", selTitle: "Off", titleFont: dj_pingRegularFont(11), titleColor: djGreen, selTitleColor: djRed, bgColor: djWhite, target: self, action: #selector(clickSwitch(button:)), superView: view) { (make) in
    make.center.equalTo(view.snp.center)
    make.width.height.equalTo(100)
}

示例 03 - 创建文本字段

  • 之前
let textField = UITextField()
textField.placeholder        = "Please input your name"
textField.textColor          = UIColor.blue
textField.font               = UIFont.systemFont(ofSize: 11)
textField.textAlignment      = .left
textField.keyboardType       = .default
textField.clearButtonMode    = .always
        
view.addSubview(textField)
        
textField.snp.makeConstraints { (make) in
    make.center.equalTo(view.snp.center)
    make.width.equalTo(200)
    make.height.equalTo(50)
}
  • 现在
UITextField(placeholder: "Please input your name", placeholderColor: djLightGray, placeholderFont: dj_systemfont(11), textColor: djBlue, superView: view) { (make) in
    make.center.equalTo(view.snp.center)
    make.width.equalTo(200)
    make.height.equalTo(50)
}
注意

您可以使用 DJExtension 为您的文本字段设置占位符颜色和字体。

示例 04 - 推送一个新的视图控制器

  • 之前
navigationController?.pushViewController(TestViewController(), animated: true)
  • 现在
dj_push(TestViewController())

示例 05 - 显示操作表

  • 之前
let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
        
let action1 = UIAlertAction(title: "Take a photo", style: .default) { (action) in
    print("Take a photo")
}
let action2 = UIAlertAction(title: "Open photo library", style: .default) { (action) in
    print("Open photo library")
}
let action3 = UIAlertAction(title: "Cancel", style: .cancel)
        
alertController.addAction(action1)
alertController.addAction(action2)
alertController.addAction(action3)
        
present(alertController, animated: true, completion: nil)
  • 现在
dj_showActionSheet(firstTitle: "Take a photo", firstAction: { (_) in
    print("Take a photo")
}, secondTitle: "Open photo library") { (_) in
    print("Open photo library")
}
注:

您还可以使用 DJExtension 为操作标题设置颜色。

更多您可能使用的功能

功能 注释
dj_hexColor("00ff00") 获取一个十六进制值对应的颜色。
dj_pingSemiboldFont(15) 从字体家族 "PingFangSC-Semibold" 获取字体。
dj_isCameraAllowed() 相机授权是否允许。
dj_navigationBarHeight() 获取导航栏的高度(适用于 iPhone X 或更高版本)。
dj_isEmpty(obj) 一个对象是否为空。
dj_isIPhoneX() 手机是否为 iPhone X、Xs、Xs Max。
dj_toJson(obj) 将对象转换为 JSON 字符串。
dj_callPhone("13288889990") 拨打一个电话号码。
dj_saveFile(url, fileName) 将文件保存到沙盒。
dj_deleteFile(fileName) 从沙盒删除文件。
dj_postNotification("LoginSuccessNotification") 发布一个通知。
更多...

您可以在文件 DJGlobalFunctions.swift 中找到更多全局函数。

许可证

MIT

联系我

悟饭哪

感谢