-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ffb449
commit 377b83b
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// | ||
// form.swift | ||
// Nativebrik | ||
// | ||
// Created by Ryosuke Suzuki on 2023/12/14. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
import YogaKit | ||
|
||
class TextInputView: UIView, UITextFieldDelegate { | ||
var textInput: UITextField? = nil | ||
|
||
required init?(coder: NSCoder) { | ||
super.init(coder: coder) | ||
} | ||
|
||
init(block: UITextInputBlock) { | ||
super.init(frame: .zero) | ||
|
||
// wrap layout | ||
self.configureLayout { layout in | ||
layout.isEnabled = true | ||
layout.width = YGValueUndefined | ||
layout.width = .init(value: 100.0, unit: .percent) | ||
layout.flexShrink = 1 | ||
} | ||
configureBorder(view: self, frame: block.data?.frame) | ||
|
||
// toolbar for input | ||
let toolbar = UIToolbar() | ||
let space = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) | ||
let doneButton = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(doneButtonTapped)) | ||
toolbar.setItems([space, doneButton], animated: true) | ||
toolbar.sizeToFit() | ||
|
||
// input layout | ||
let textInput = UITextField() | ||
self.textInput = textInput | ||
textInput.textAlignment = parseTextAlign(block.data?.textAlign) | ||
|
||
textInput.inputAccessoryView = toolbar | ||
textInput.delegate = self | ||
textInput.configureLayout { layout in | ||
layout.isEnabled = true | ||
layout.width = .init(value: 100.0, unit: .percent) | ||
configurePadding(layout: layout, frame: block.data?.frame) | ||
} | ||
if let paddingLeft = block.data?.frame?.paddingLeft { | ||
textInput.leftView = UIView(frame: CGRect(x: 0, y: 0, width: paddingLeft, height: 0)) | ||
textInput.leftViewMode = .always | ||
} | ||
if let paddingRight = block.data?.frame?.paddingRight { | ||
textInput.rightView = UIView(frame: CGRect(x: 0, y: 0, width: paddingRight, height: 0)) | ||
textInput.rightViewMode = .always | ||
} | ||
if let color = block.data?.color { | ||
textInput.textColor = parseColor(color) | ||
} else { | ||
textInput.textColor = .label | ||
} | ||
textInput.font = parseTextBlockDataToUIFont(block.data?.size, block.data?.weight, block.data?.design) | ||
if let placeholder = block.data?.placeholder { | ||
textInput.placeholder = placeholder | ||
} | ||
if let autocorrect = block.data?.autocorrect { | ||
if autocorrect { | ||
textInput.autocorrectionType = .yes | ||
} else { | ||
textInput.autocorrectionType = .no | ||
} | ||
} | ||
if let secure = block.data?.secure { | ||
textInput.isSecureTextEntry = secure | ||
} | ||
textInput.keyboardType = parserKeyboardType(block.data?.keyboardType) | ||
|
||
self.addSubview(textInput) | ||
} | ||
|
||
override func layoutSubviews() { | ||
super.layoutSubviews() | ||
} | ||
|
||
@objc func doneButtonTapped() { | ||
self.textInput?.resignFirstResponder() | ||
} | ||
|
||
// UITextFieldDelegate | ||
func textFieldShouldReturn(_ textField: UITextField) -> Bool { | ||
textField.resignFirstResponder() | ||
} | ||
} |