Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/custombutton
Customizable button for your macOS app
https://github.com/sindresorhus/custombutton
button cocoa macos nsbutton swift swift-package
Last synced: about 1 month ago
JSON representation
Customizable button for your macOS app
- Host: GitHub
- URL: https://github.com/sindresorhus/custombutton
- Owner: sindresorhus
- License: mit
- Created: 2019-09-22T10:20:21.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-02-20T07:13:01.000Z (9 months ago)
- Last Synced: 2024-04-14T04:18:48.773Z (7 months ago)
- Topics: button, cocoa, macos, nsbutton, swift, swift-package
- Language: Swift
- Homepage:
- Size: 499 KB
- Stars: 167
- Watchers: 4
- Forks: 16
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license
Awesome Lists containing this project
README
# CustomButton
> Customizable button for your macOS app
It's a layer-based NSButton subclass that animates the styles and colors between normal and active (pressed) state.
This package is used in production by [Gifski](https://github.com/sindresorhus/Gifski).
## Requirements
- macOS 10.13+
## Install
Add `https://github.com/sindresorhus/CustomButton` in the [“Swift Package Manager” tab in Xcode](https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app).
## Usage
See the [source](Sources/CustomButton/CustomButton.swift) for what properties you can change and play with it in the [example app](Example). By default, it has no style.
```swift
import Cocoa
import CustomButton@main
final class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!func applicationDidFinishLaunching(_ notification: Notification) {
let button = CustomButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.title = "CustomButton"
button.activeBackgroundColor = .systemPink
button.borderWidth = 1
button.borderColor = .systemPink
button.cornerRadius = 5
button.textColor = .systemPink
button.activeTextColor = .whitelet contentView = window.contentView!
contentView.addSubview(button)NSLayoutConstraint.activate([
button.centerXAnchor.constraint(equalTo: contentView.centerXAnchor),
button.centerYAnchor.constraint(equalTo: contentView.centerYAnchor),
button.widthAnchor.constraint(equalToConstant: 100),
button.heightAnchor.constraint(equalToConstant: 30)
])
}
}
```The button can also be edited in Interface Builder, but Xcode is very buggy with `@IBDesignable`, so I would recommend using it only programmatically for now.
## FAQ
#### Can you support Carthage and CocoaPods?
No, but you can still use Swift Package Manager for this package even though you mainly use Carthage or CocoaPods.
#### Won't SwiftUI make this moot?
SwiftUI does indeed make it much easier to create custom-looking buttons, but SwiftUI is still immature and most companies will not be able to require macOS 10.15 for a long time. So this package will still be useful for multiple years to come.
## Related
- [Defaults](https://github.com/sindresorhus/Defaults) - Swifty and modern UserDefaults
- [KeyboardShortcuts](https://github.com/sindresorhus/KeyboardShortcuts) - Add user-customizable global keyboard shortcuts to your macOS app
- [LaunchAtLogin](https://github.com/sindresorhus/LaunchAtLogin) - Add "Launch at Login" functionality to your macOS app
- [DockProgress](https://github.com/sindresorhus/DockProgress) - Show progress in your app's Dock icon
- [More…](https://github.com/search?q=user%3Asindresorhus+language%3Aswift+archived%3Afalse&type=repositories)You might also like my [apps](https://sindresorhus.com/apps).