I'm new to Xcode and trying to do something easy.
But unfortunately, I couldn't find an easy way to do so.
My question might be (laughably) easy but I'm seriously stuck right now.
In Xcode, there is an object in the object library called "Text Field".
In this text field, words can be entered. I want to push a button and save the written words not a string, which I can then display as a label.
Help would be really appreciated.
Thanks in advance!
The "Text Field" UI element in the object library represents an instance of a UITextField. If you look at the documentation, a text field has a text property - a String that contains the current textual content of the text field. Similarly, UILabel has a text property. So you want to take the text field's text, and assign it to the label's text.
Assuming you have an outlet to your text field in the storyboard and an outlet/variable referencing the label you want to set the text of, in your button's action method, you might do something like this:
#IBAction func buttonPressed(button: UIButton) {
myLabel.text = myTextField.text
}
Related
I'm trying to accomplish an autocomplete of my own for matching array items while typing. So, I already have the logic done for matching what's being typed with what's in the array, but my question is related to displaying the proposed correction in the UITextView like this screenshot
I was trying by splitting the text in the textview and replacing the last word into attributed strings, but that makes the proposed correction part of the actual contents. Any ideas of how this is accomplished in this app?
What you currently do is good , when you set the text of the textView to attributed string make bool flag say it's name is textEdited = true with a string part that the user types say it's name userStr , when textView change method is triggered check that bool and according to it make the search if it's true proceed search with userStr if it's not proceed search with whole textView text , don't forget to make textEdited= false after every zero suggested result
Edit: regarding the cursor put a label under the textfield with the same font as the textView and make it's background darkGray , also make background of the textview transparent and every attributed string assign it to the label so plus part of the label will be shown and cursor will be as it is in the textView
I may have found a bug in iOS and I'm not sure how to overcome it. I am losing my textView style as soon as the database populates it with content.
That is what happens:
In my storyboard I have the following textView
As soon as I populate the textView manually or from my object as per below it loses its style.
if let object = currentObject.objectForKey("postText") as? String {
postTextView.text = "the text field has some text added"
}
On simulator, showing the style is lost.
Also, other situation:
I may lose the style as well if I uncheck the editable box as per the image bellow.
Here's an older answer which hopefully answers your question and solves your problem. Is seems indeed to be a bug. https://stackoverflow.com/a/19115950/543224
Did you set the attributes on the text view itself (i.e. the text view text is "plain", and not "attributed")? Or did you use attributed text?
If the latter, it's quite normal that if you replace the attributed text with plain text with no attributes, it reverts to the attributes of the text view itself.
Make sure the type is set to "plain", and that you set the attributes on the text view itself, not on the text inside it. Or use the attributedText and not the text property of your text view, with appropriate attributes.
I guess that you update UI on from background. If you want update UI you have to update in main queue. If you don't do it, some strange issue will come. So you can change your code to:
dispatch_async(dispatch_get_main_queue()) { () -> Void in
if let object = currentObject.objectForKey("postText") as? String {
postTextView.text = "the text field has some text added"
}
}
I have a text field. When a certain text is in it, I want to run some code. But I'm confused on how to do this. Should I create an action for the text field, when the text is in it, and then create an else if with the name of the text field, and then the code I want when certain words are entered?
Have a look at the UITextFieldDelegate.
You could set the delegate of your textfield to a class that implement your custom actions in textFieldDidEndEditing.
I have Objective C code that continually updates a set of numerical values. I need to display these values on the screen. That's it! I can convert numerical values into a text string, no problem. But how do I display this string in a UI element? Do I use a text box or a text field or a text view, or a...? I cannot find examples to show how to pass a string from code into the UI. I assume I need to set up a text thingy, and then periodically refresh the contents of that text thingy when the values change?
I assume the answer is simple but is just obscured by a smokescreen of technical jargon.
Thanks!
From the UI perspective you might want something like a
UITextView - multi-line text input
UITextField - single-line text input
UILabel - just text
For your purpose of just printing text, you should use UILabel, since you dont want / need any kind of input. You can access its text using:
// yourLabel is your current UILabel* you want to output yourValue to
yourLabel.text = yourValue;
Of course that yourValue needs to be converted to NSString before.
To actually get hold of the UILabel, you need to connect it from the Interface Builder as an IBOutlet. For tutorials on that topic, take a look at tutorials like Interface Tutorials by Ray Wenderlich or youtube or just google Interface Builder tutorial.
Is there a native UI control for code input text field, for example like Whatsapp:
No. To achieve this, they're almost certainly tapping into the textField:shouldChangeCharactersInRange:replacementString: method for their UITextField, selectively accepting and formatting user input to match the dash-if-empty approach.
Further, I'm sure they've subclassed the field; per your comments there isn't a blue cursor - which isn't standard for a UITextField.
No there isn't. Use a UITextField, fill it with dashes, keep track of how many characters the user has entered, and replace the dashes accordingly as the user types.
There's a 4-digit code input text field called CodeInputView written in Swift.
In the past I've added a UITextField to the view and set its hidden == true. Then I show/hide the keyboard by calling becomeFirstResponder()/resignFirstResponder() on it. I listen for text did change notifications and update a visible label with the value of the hidden text field.