textView loses style in iOS after populated - ios

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"
}
}

Related

Array item completion in a UITextView

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

Cannot clear "attributedText" property from UITextField

The title basically spells it out...I have a text field. When it's first responder, I want to use an attributed string to hilight all the text, in order to indicate to the user that anything they type will clear it. When it resigns first responder, I want to remove the hi-light and display plain text. Here's what I have:
func toggleHilight() {
self.hilighting = !self.hilighting // This property is initialized to false
if (self.hilighting) {
self.inputURLField.attributedText = NSAttributedString(string: self.inputURLField.text, attributes: [NSBackgroundColorAttributeName : UIColor.blueColor()])
} else {
self.inputURLField.text = BrowserNavManager.sharedInstance.currentURL.absoluteString
}
}
According to Apple's docs, setting the "attributedText" property will clear the regular "text" property, and vice-versa:
text
...In iOS 6 and later, assigning a new value to this property also replaces the value of the attributedText property with the same text, albeit without any inherent style attributes...
attributedText
...Assigning a new value to this property also replaces the value of the text property with the same string data, albeit without any formatting information...
But for me, this is simply not the case. In fact, even explicitly setting "attributedText" to nil does...nothing. It still logs the same (i.e. NOT nil), and still shows the hi-lighted (attributed) text. I've also tried calling setNeedsDisplay(), which had no effect.
What the heck am I missing here? Any help appreciated!
Solved...I found a way to trigger the system hi-lighting (which is what I was trying to ape anyway) by setting the "selectedTextRange" property on the text field.

Xcode: Text Field to String

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
}

Dynamically change keyboard's return button but preserve type

I have an input field the user needs to fill with an alphanumeric code. The keyboard the user uses to type the code has a dynamic return button that changes to "send" as he writes some text on the field. When the field is empty the return button has the default value.
To dynamically change the return button type I use the following code:
if([textField.text isEqualToString:#""])
{
textField.returnKeyType = UIReturnKeyDefault;
}
else
{
textField.returnKeyType = UIReturnKeySend;
}
[textField reloadInputViews];
However this has the following drawback: since the code is alphanumeric, the user may be typing numbers, and yet the keyboard will always switch back to the letter keyboard, so to type more than one number in a row he will need to be continuously switching to number keyboard.
Is there any way to dynamically change the return key of a keyboard as the user types but to preserve the keyboard state to letters or numbers keyboard?
I think this is not a bug on Apple's side, more a missing implementation of API for the keyboard. With the new iOS8 API you might want to create your own keyboard returning the UIKeyboardType.
For iOS7 I worked around by inspecting the views of the keyboard. Use the US2KeyboardType CocoaPod or the source:
https://github.com/ustwo/US2KeyboardType
As Martin noted above, it's not a bug on Apple's side, but on my side. However I'll be posting the solution I've found since it is the one that solves that particular problem:
Instead of manually changing the return key type when there is text on the text field, Apple provides us with a property called enablesReturnKeyAutomatically that when set to YES it automatically manages the issue, enabling and disabling the return key depending on whether there is text or not in the text field.
Therefore you don't need to modify the returnKeyType property, and thus, no calling to reloadInputViews is required, so the keyboard doesn't change back to its original state.

Getting values out of text fields with UIAutomation

I'm trying to understand how UIAutomation can be used to perform automatic application tests, so I've created a temperature converter app that can be driven from the JavaScript interface. Here's the UI:
The top text field has an Accessibility label of "Celsius", similarly the bottom field has "Fahrenheit" as its Accessibility label. I'm trying to drive it through this script:
UIALogger.logStart("Test -40ºC == -40ºF");
var window = UIATarget.localTarget().frontMostApp().mainWindow();
var celsiusField = window.textFields()["Celsius"];
var fahrenheitField = window.textFields()["Fahrenheit"];
var convertButton = window.buttons()["Convert"];
celsiusField.setValue("-40");
convertButton.tap();
var fahrenheitValue = fahrenheitField.value();
if (fahrenheitValue == "-40.0") {
UIALogger.logPass("-40C == -40F");
} else {
UIALogger.logFail("-40C == " + fahrenheitValue + "F");
}
This correctly sets the text in the Celsius field to "-40", and when the button is tapped, the app updates the text in the Fahrenheit field to "-40.0". However, fahrenheitValue has the value "Fahrenheit" (the name/label of the text field), so the test fails. I wondered whether this was maybe a timing issue, so put in a one-second delay after the tap, which didn't change the behaviour.
Is it possible to get the text out of the text field and compare it with the expected value?
Is it getting "Fahrenheit" because that's the accessibility label or because it's the placeholder text?
It looks like value() might return the placeholder text. One workaround could be to modify the placeholder value once the user starts typing to set it to be the text the user entered and then reverting it to Fahrenheit when they delete? I assume that even when you call setValue it acts as it would when the UITextField delegate methods get called?
You could add a label to your view, update it and read it out.
Try hooking up the Accessibility Inspector to show what UIAutomation "sees" in your app. It might be that it's reading some stale values because of the way you set the values in your app.
One way of refreshing the values is to send an update event:
UIAccessibilityPostNotification(UIAccessibilityLayoutChangedNotification, nil);

Resources