SwiftyButton

Simple and customizable button in Swift

Github星跟蹤圖

SwiftyButton

Maintainer(s): @camanjj

Simple and customizable button in Swift.

Carthage compatible

Installation

Cocoapods

pod 'SwiftyButton'

Carthage

github 'TakeScoop/SwiftyButton'

Usage

Flat Button

let button = FlatButton()
button.color = .cyan
button.highlightedColor = .blue
button.cornerRadius  = 5

Pressable Button

let button = PressableButton()
button.colors = .init(button: .cyan, shadow: .blue)
button.shadowHeight = 5
button.cornerRadius = 5

All Properties

Here is a list of all the properties that you can modify. Those are all editable directly from Interface Builder. See FlatButton.Defaults or PressableButton.Defaults to set defaults for those properties.

FlatButton

button.color = .cyan
button.highlightedColor = .cyan
button.selectedColor = .blue
button.disabledButtonColor = .gray
button.cornerRadius = 8

PressableButton

button.colors = .init(button: .cyan, shadow: .blue)
button.disabledColors = .init(button: .gray, shadow: .darkGray)
button.shadowHeight = 10
button.cornerRadius = 8
button.depth = 0.5 // In percentage of shadowHeight

Interface Builder (Storyboard/XIB)

Add a UIButton as usual, then:

  • Modify the underlying class to FlatButton/PressableButton, and that the module is set to SwiftyButton
  • Make sure that the button type is set to Custom

Defaults

You can set defaults that will be applied for any new instance of SwiftyButton by modifying the FlatButton.Defaults or PressableButton.Defaults structure:

FlatButton.Defaults.color = .cyan
PressableButton.Defaults.cornerRadius = 8
...

Custom Content

Use CustomPressableButton to add custom content in a Swifty Button.

This is a subclass of SwiftyButton that exposes a content view that moves when the button state changes. All you have to do is add your views inside button.contentView and setup layout constraints relative to this view.

Install

pod `SwiftyButton/CustomContent`

Usage

Here is how you would create a button similar to the one above (here we used PureLayout for constraints):

let button = CustomPressableButton()

let indicator = UIActivityIndicatorView(activityIndicatorStyle: .white)
button.contentView.addSubview(indicator)
indicator.autoPinEdgesToSuperviewEdges(with: UIEdgeInsets(top: 10, left: 15, bottom: 10, right: 0), excludingEdge: .right)
indicator.startAnimating()

let label = UILabel()
button.contentView.addSubview(label)
label.autoPinEdgesToSuperviewEdges(with: UIEdgeInsets(top: 10, left: 0, bottom: 10, right: 10), excludingEdge: .left)
label.autoPinEdge(.left, to: .right, of: indicator, withOffset: 10)
label.text = "Loading..."
label.textColor = .white

Known limitations

  • PressableButton only works with plain colors (no alpha / transparency) because of the way its background image is rendered. See #2
  • PressableButton manages title and image insets automatically (titleEdgeInsets and imageEdgeInsets). See #5
    • User-specified inset values for top and bottom will be overridden.
    • You may specify inset values for left and right.

More examples

Look at the Examples folder to see more button examples.

Contribute

Publishing a new version

Here's an example of a version bump: bump to 0.8.0.

Replace NEW_VERSION with the version you're about to publish.

  • Checkout the master branch and ensure it's up-to-date.
  • Open SwiftyButton.xcworkspace and edit settings for the SwiftyButton target. Change the target version to NEW_VERSION.
  • Open CHANGELOG.md and add a new section [NEW_VERSION]. At the bottom of the document, copy a version anchor and link to the new version tag (which we'll create later). Insert a new empty [master] section on top.
  • Open SwiftyButton.podspec and edit s.version to NEW_VERSION.
  • Commit your changes as Bump to NEW_VERSION.
  • Create a new git tag named NEW_VERSION and push your commit + tag to Github.
  • Push the new pod version with $ pod trunk push ./

License

This project is copyrighted under the MIT license. Complete license can be found here: https://github.com/TakeScoop/SwiftyButton/blob/master/LICENSE

Credits

主要指標

概覽
名稱與所有者TakeScoop/SwiftyButton
主編程語言Swift
編程語言Ruby (語言數: 3)
平台
許可證MIT License
所有者活动
創建於2015-11-16 23:44:49
推送於2021-08-07 05:53:38
最后一次提交2020-05-20 12:05:02
發布數11
最新版本名稱0.8.2 (發布於 )
第一版名稱0.0.1 (發布於 2015-11-16 15:54:25)
用户参与
星數551
關注者數45
派生數43
提交數42
已啟用問題?
問題數16
打開的問題數7
拉請求數4
打開的拉請求數3
關閉的拉請求數2
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?