Numeric keyboard with decimal - ios

I´m building an app with equations and I have used a numeric keyboard but I want to have a decimal slot on it. The default is only numbers. How do you add a decimal input on that keyboard?
This is the code I use to get the numeric keyboard
.keyboardType = UIKeyboardType.NumberPad

Simply change the keyboard type:
self.someTextField.keyboardType = UIKeyboardType.decimalPad
See the documentation for all the keyboard types.

Select Decimal Pad in Keyboard Type for Text Field in Attribute Inspector.
Refer the image below for more detail

In storyboard go to Attribute Inspector. There you can change keyboard type.You can check here

set keyboard type to textField in swift 3
#IBOutlet weak var txtNumber: UITextField!
override func viewDidLoad(){
self.txtNumber.keyboardType = .decimalPad
}

Related

Getting the value in the textfield in IOS Simulator

I am new to Swift and x-code. I'm currently developing an app where there is only one textbox and one submit button.
I am writing a test case which is like this- "Whenever i click the submit button, the text which is given by me in the text field should be validated."
How can i get the text from the textbox when i click the submit button? while running the test case
To find out when the button is clicked, you will have to define an Action for your button in some Target... usually your view controller. Search Apple’s documentation for the “Target Action” design pattern and you should find tutorial information.
To get the value of a text field, you can simply get it’s text property. However, to get a reference to the text field, your view controller will need an Outlet that references the text view. In the documentation you can search for “Outkests” or IBOutlet to find tutorials.
Create an IBOutlet for that textField and connect it in the storyboard (See attached file)
#IBOutlet weak private var textField: UITextField!
And use textField.text to get text file value in your IBAction that handle tap event
first create #IBOutlet of your text field and create #IBAction of submit button. and validate it in #IBAction like this :-
#IBOutlet weak var textfieldOutlet: UITextField!
#IBAction func submit(_ sender: UIButton)
{
if textfieldOutlet.text?.isEmpty == False
{
print(textfieldOutlet.text);
}
}
*set breakpoint on print value and you getvalue
In UI tests, you can access the text from an XCUIElement representing a UITextField using the value property.
let app = XCUIApplication()
let textField = app.textFields.element
textField.typeText("test text")
let text = textField.value as? String // should be "test text"

UITextField change in iOS for conversions

I am creating a conversion application. I have multiple text fields. What I intend to do is when I start entering Kg value its equivalent gram and pound value should be displayed in the gram and pound text fields. How can it be done using editingChanged function in iOS?
This question is very broad but I will try to give a short answer of how to track changed of text fields:
class YourViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
#IBAction func textFieldDidChange(_ sender: UITextField) {
self.textField.text = doSomeCalculation(sender.text)
}
}
First, you need to set up a view controller (or a view or whatever suits your interface design) with a UITextField. Then you connect the UITextField with the variable textField and the desired action of the text field with the function textFieldDidChange. You can use the action Editing Changed but I prefer Value Changed (depends on what you want to achieve).
Any time the text field is changed the function textFieldDidChange is called and you can do your calculations and alter the text of textField.

Xcode How read numbers on a Text View (or label?)

this is my first post.
I'm new in the Xcode Word.
At the moment I'm working to a simple application and I need to read a number from a box and after a lot of calculations write the result in an other text view or label.
I need how to read a numbers or a string from a box after writing (text view, label or other).
Thank you very much for your answers
if you are sure that your textfield will accept only numbers, one of the possible ways could be:
yourTextField.keyboardType = UIKeyboardType.decimalPad
in this way, when the user will tap on the textField, you may use the numeric keypad.
At this point your viewController must implement UItextFieldDelegate
yourTextField.delegate = self
and then use the method, called when textfield loses focus:
func textFieldDidEndEditing(_ textField: UITextField) {
var valueInserted : Float = NSString(string: textField.text!).floatValue
}
Drag a UITextView inside the scene (in the storyboard)
Create an outlet between the UITextView and ViewController.swift
in ViewController.swift you will have something like this:
#IBOutlet weak var myTextView:UITextView!
You must also get a reference for your label
#IBOutlet weak var myLabel:UILabel!
you can now access to the content(input) of your textView by checking the text property:
var input = myTextView.text
This will be a string, that you can try to convert to an Int. Once you have a result to display, set the label text:
myLabel.text = result

how to detect Number and Decimal Keyboard iOS

I'm developing a custom keyboard and based on Apple App Store Review guideline I have to provide a keypad for decimal and number pad :
Keyboard extensions must provide Number and Decimal keyboard types as described in the App Extension Programming Guide or they will be rejected
So I have to provide a keyboard for Decimal and Number Textfield, so how can I detect textfield type.
There is nothing about Number or Decimal Keyboard in App Extension Programming Guide so How detect which textfield I should load Decimal Keyboard or Number Keypad ?
You can get the current keyboard type before your view appears like so (in Swift).
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
// Setup keyboard manager to correct type
if let inputTraits = self.textDocumentProxy as? UITextInputTraits {
let keyboardType = inputTraits.keyboardType
let returnKeyType = inputTraits.returnKeyType
}
}
This from my project but reformatted for clarity. I also didn't compile it since it's such a simple example.
I've found it's good not to trust any extension API values until the view is visible or about to appear, but you could also try this in viewDidLoad()

How can I add a Number and Decimal keyboard into my Custom Keyboard on iOS8?

I created a custom keyboard in Swift but it was rejected from the App Store because: "keyboard extension does not include Number and Decimal types".
How can I add these two keyboards easily?
I tried to rebuild the original view but it doesn't work properly. I'm sure there is a solution to create 2 or 3 different views and switch between them.
How can I switch between keyboard types when the keyboard type changes?
You can detect changes to the keyboard type in textDidChange. You need to get the UITextDocumentProxy then detect the proxy's keyboardType, and then you can do whatever layout changes needed.
override func textDidChange(_ textInput: UITextInput?) {
// Called when the document context is changed - theme or keyboard type changes
let proxy = self.textDocumentProxy as UITextDocumentProxy
if proxy.keyboardType == UIKeyboardType.numberPad
|| proxy.keyboardType == UIKeyboardType.decimalPad {
//add code here to display number/decimal input keyboard
}
}

Resources