Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to achieve the Keyboard selection through @IBInspectable

I wanted to create a keyboard selection from @IBInspectabale

keyboard selection

How to achieve this

I am creating a view, In which I insert an ImageView and a TextField, Now I am creating this custom view class as @IBDesignable and created the @IBInspectable elements.

I successfully create side image and placeholder elements but now I am trying to create the keyboard type but facing issues.

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 the above working fine, but following is not working for me:

@IBInspectable var keyboard: UIKeyboardType? {
    get{
        return UIKeyboardType(rawValue: textField.keyboardType.rawValue)
    }
    set(keyboard){
        textField.keyboardType = keyboard!
    }
}

}

I tried it by creating enum but it does not give any result for me.

like image 966
nabu Avatar asked May 03 '17 10:05

nabu


2 Answers

First of all thanks to everyone. My problem is solved without doing any extra effort of creating enum and all. I used the apple predefined UIKeyboardType enum. 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 Interface builder, and you can set 0,1,2... value for your keyboard type. where the 0,1,2 represent 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's 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 @ #)
like image 64
nabu Avatar answered Sep 20 '22 17:09

nabu


It is not possible to use enum types for @IBInspectable vars. You have to set your var as a String or Int.

From Apple's docs:

You can attach the IBInspectable attribute to any property in a class declaration, class extension, or category for any type that’s supported by the Interface Builder defined runtime attributes: boolean, integer or floating point number, string, localized string, rectangle, point, size, color, range, and nil.

like image 35
Oskar Avatar answered Sep 22 '22 17:09

Oskar