Want to achieve keyboard selection with @IBInspectable
I wanted to create a keyboard selection from @IBInspectabale
How to achieve this
I am creating a view where I insert ImageView and TextField, Now I am creating this custom view class as @IBDesignable and created @IBInspectable items.
I am successfully creating 3rd party image elements and placeholders, but now I am trying to create a keyboard type but am running into problems.
code snipped: `import UIKit
@IBDesignable class CustomTextField: UIView, UITextFieldDelegate {
//custom view from the XIB file
var view: UIView!
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var imageView: UIImageView!
override init(frame: CGRect) {
super.init(frame: frame)
loadViewFromNib ()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
loadViewFromNib ()
}
func loadViewFromNib() {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: "CustomTextField", bundle: bundle)
let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
view.frame = bounds
view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(view);
}
@IBInspectable var sideImage: UIImage? {
get {
return imageView.image
}
set(sideImage) {
imageView.image = sideImage
}
}
@IBInspectable var placeHolderText: String? {
get {
return textField.placeholder
}
set(placeHolderText) {
textField.placeholder = placeHolderText
}
}'
all of the above works fine, but the following doesn't work for me:
@IBInspectable var keyboard: UIKeyboardType? {
get{
return UIKeyboardType(rawValue: textField.keyboardType.rawValue)
}
set(keyboard){
textField.keyboardType = keyboard!
}
}
}
I tried this by creating an enum, but it doesn't give me any result.
source to share
Cannot use enum types for @IBInspectable
vars. You have to set your var as String
or Int
.
From Apple docs:
You can attach the IBInspectable attribute to any property in a class declaration, class extension, or category for any type supported by the Interface Builder Builder attribute: boolean, integer, or float, string, localized string, rectangle, point, size, color, range and zero.
source to share
First of all, thanks to everyone. My problem was solved without any extra effort to create an enum and that's it. I used the predefined enum UIKeyboardType. Just write the following code:
@IBInspectable var keyboard:Int{
get{
return self.textField.keyboardType.rawValue
}
set(keyboardIndex){
self.textField.keyboardType = UIKeyboardType.init(rawValue: keyboardIndex)!
}
}
And it will show the keyboard in the interface builder and you can set the value 0,1,2 ... for your keyboard type. where 0,1,2 are presented as follows:
0: default // Default type for the current input method.
1: asciiCapable // Displays a keyboard which can enter ASCII characters
2: numbersAndPunctuation // Numbers and assorted punctuation.
3: URL // A type optimized for URL entry (shows . / .com prominently).
4: numberPad // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). Suitable for PIN entry.
5: phonePad // A phone pad (1-9, *, 0, #, with letters under the numbers).
6: namePhonePad // A type optimized for entering a person name or phone number.
7: emailAddress // A type optimized for multiple email address entry (shows space @ . prominently).
8: decimalPad // A number pad with a decimal point.
9: twitter // A type optimized for twitter text entry (easy access to @ #)
source to share