Coder Social home page Coder Social logo

djextension's Introduction

DJExtension

Extensions, global functions for swift. Create a component with one line of codes.

10 times to improve your code efficiency.

Main Features!

  • Create a component with one line of codes, like a label, button, textField...
  • Use global functions instead of some Apple APIs.
  • Make many extensions and convenience init methods for UILabel/UIButton/UIView/UIViewController/UIImage...

What's New

The latest version is 1.5.0. User can create a button with touch event in one line.

  • 1.5.0 or later
UIButton(title: "login", titleFont: dj_regularFont(22), titleColor: .red, bgColor: .lightGray, superView: view, layoutClosure: { (make) in
    make.left.equalTo(50)
    make.top.equalTo(77)
    make.width.equalTo(88)
    make.height.equalTo(44)
}) {
    print("login successfully!")
}
  • 1.4.1 or before
let loginButton = UIButton(title: "login", titleFont: dj_regularFont(22), titleColor: .red, bgColor: .lightGray, superView: view) { (make) in
    make.left.equalTo(50)
    make.top.equalTo(77)
    make.width.equalTo(88)
    make.height.equalTo(44)
}

loginButton.dj_addTarget(self, action: #selector(clickLogin))

@objc private func clickLogin() {
        
    print("Login Successfully!")
}

How To Install?

Manual (Recommended)

Just drag the DJExtension folder to your project.

This is what all you should do, then you can use the extension functions. All the functions or constants are start with dj.

Cocoapods (Not Recommended)

pod 'DJExtension'

You should also import this framework before you use it, like this import DJExtension.

NOTE:

The latest version in the pod depository is 1.4.1. So you'd better just drag the whole package to your project. Then you can use the latest functions.

The reason to use DJExtension

With DJExtension, you won't need to write duplicate and redundant codes in your projects. Here are some examples.

Example 01 - create labels

  • before
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)
}
  • now
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)
}

The parameter alignment and lineCount have default values, so you can also write like this.

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)
}

Example 02 - create buttons

  • before
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)
}
  • now
UIButton(title: "login", titleFont: dj_regularFont(22), titleColor: .red, bgColor: .lightGray, superView: view, layoutClosure: { (make) in
    make.left.equalTo(50)
    make.top.equalTo(77)
    make.width.equalTo(88)
    make.height.equalTo(44)
}) {
    print("login successfully!")
}

Example 03 - create text fileds

  • before
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)
}
  • now
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)
}
Note

You can set the placeholder color and placeholder font for your text filed with DJExtension.

Example 04 - push a new view controller

  • before
navigationController?.pushViewController(TestViewController(), animated: true)
  • now
dj_push(TestViewController())

Example 05 - show an action sheet

  • before
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)
  • now
dj_showActionSheet(firstTitle: "Take a photo", firstAction: { (_) in
    print("Take a photo")
}, secondTitle: "Open photo library") { (_) in
    print("Open photo library")
}
Note:

You can also set color for the action titles with DJExtension.

More functions you may use

Functions Comment
dj_hexColor("00ff00") get a color with a hex value.
dj_pingSemiboldFont(15) get a font from the font family "PingFangSC-Semibold".
dj_isCameraAllowed() the camera authorization is allowed or not.
dj_navigationBarHeight() get the navigation bar height(adapted iPhone X or later).
dj_isEmpty(obj) an object is empty or not.
dj_isIPhoneX() the phone is iPhone X,Xs,Xs Max or not.
dj_toJson(obj) convert an object to json string.
dj_callPhone("13288889990") call a number
dj_saveFile(url, fileName) save a file to sandbox.
dj_deleteFile(fileName) delete the file from sandox.
dj_postNotification("LoginSuccessNotification") post a notification.
more...
Note:

You can find more global functions in file DJGlobalFunctions.swift.

License

MIT

Contact Me

悟饭哪

Thanks

djextension's People

Contributors

iwufan avatar

Stargazers

chianglim avatar  avatar owlonexx avatar Wan avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.