Skip to content

Commit

Permalink
Merge pull request #2 from cameronshemilt/text-as-button
Browse files Browse the repository at this point in the history
Add option for a text-based button
  • Loading branch information
cameronshemilt authored Mar 20, 2021
2 parents 0a8b99d + de8a667 commit 24d85e1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ Use by attaching `.keyboardToolbar(...)` to the outermost part of your View.

| **Parameter** | **Type** | **Optional** | **Description** |
| ------------- | ---------------------- | ------------ | ------------------------------------------------------------ |
| `image` | `Image` | No | The image/icon of the toolbar icon. Convenience initializers exist for *systemName* and *name* |
| `image` | `Image` | Yes | The image/icon of the toolbar icon. Convenience initializers exist for *systemName* and *name* |
| `text` | `String` | Yes | The text of the toolbar icon. Alternative to displaying an image. |
| `color` | `Color` | Yes | Color of the image/icon |
| `isFixed` | `KeyboardToolbarEdge?` | Yes | Whether the item should be sticky or not. Possible values: `nil`, `.leading` or `.trailing`. |
| `callback` | `() -> Void` | No | Action the item should perform when pressed |
Expand Down
25 changes: 19 additions & 6 deletions Sources/KeyboardToolbar/KeyboardToolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,18 @@ internal struct KeyboardToolbar: View {
func itemGroup(_ items: [KeyboardToolbarItem]) -> some View {
ForEach(items) { item in
Button(action: item.callback, label: {
item.image
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: style.itemSize, height: style.itemSize)
.foregroundColor(item.color)
if (item.image != nil || item.text == nil) {
(item.image ?? Image(systemName: "placeholdertext.fill"))
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: style.itemSize, height: style.itemSize)
.foregroundColor(item.color)
} else {
Text(item.text ?? "")
.font(.system(size: style.itemSize))
.frame(minWidth: style.itemSize)
.foregroundColor(item.color)
}
})
.frame(height: style.height)
.contentShape(Rectangle())
Expand Down Expand Up @@ -76,7 +83,13 @@ internal struct KeyboardToolbar: View {

struct KeyboardToolbar_Previews: PreviewProvider {
static var previews: some View {
KeyboardToolbar(items: [KeyboardToolbarItem("xmark.circle", callback: {}), KeyboardToolbarItem("checkmark.circle", callback: {})], style: .standard)
KeyboardToolbar(items: [
KeyboardToolbarItem("xmark.circle", callback: {}),
KeyboardToolbarItem("checkmark.circle", callback: {}),
KeyboardToolbarItem(text: #"\"#, color: .red, callback: {}),
KeyboardToolbarItem(text: "apple", color: .red, callback: {}),
KeyboardToolbarItem(image: nil, text: nil, color: .red, callback: {})
], style: .standard)
}
}
#endif
14 changes: 12 additions & 2 deletions Sources/KeyboardToolbar/KeyboardToolbarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,35 @@ import SwiftUI

public struct KeyboardToolbarItem: Identifiable {
public let id: UUID = UUID()
let image: Image
let image: Image?
let callback: () -> Void
let color: Color
let isFixed: KeyboardToolbarEdge?
let text: String?

public init(_ image: Image, color: Color = .primary, isFixed: KeyboardToolbarEdge? = nil, callback: @escaping () -> Void) {
private init(image: Image?, text: String?, color: Color = .primary, isFixed: KeyboardToolbarEdge? = nil, callback: @escaping () -> Void) {
self.image = image
self.text = text
self.callback = callback
self.color = color
self.isFixed = isFixed
}

public init(_ image: Image, color: Color = .primary, isFixed: KeyboardToolbarEdge? = nil, callback: @escaping () -> Void) {
self.init(image: image, text: nil, color: color, isFixed: isFixed, callback: callback)
}

public init(_ systemName: String, color: Color = .primary, isFixed: KeyboardToolbarEdge? = nil, callback: @escaping () -> Void) {
self.init(Image(systemName: systemName), color: color, isFixed: isFixed, callback: callback)
}

public init(name: String, color: Color = .primary, isFixed: KeyboardToolbarEdge? = nil, callback: @escaping () -> Void) {
self.init(Image(name), color: color, isFixed: isFixed, callback: callback)
}

public init(text: String, color: Color = .primary, isFixed: KeyboardToolbarEdge? = nil, callback: @escaping () -> Void) {
self.init(image: nil, text: text, color: color, isFixed: isFixed, callback: callback)
}
}

public enum KeyboardToolbarEdge {
Expand Down
4 changes: 4 additions & 0 deletions Sources/KeyboardToolbar/ViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ internal struct AppendKeyboardToolbar: ViewModifier {
}

public extension View {
/// Applies a toolbar to the keyboard of this View. Should be attached to the outermost part of the View.
/// - Parameters:
/// - items: A list of all items the toolbar shall contain.
/// - style: Styling of the keyboard toolbar.
func keyboardToolbar(_ items: [KeyboardToolbarItem], style: KeyboardToolbarStyle = .standard) -> some View {
#if targetEnvironment(macCatalyst)
return self
Expand Down

0 comments on commit 24d85e1

Please sign in to comment.