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.
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'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
}
I have an attributed string which I am making out of HTML using DTCoreText, and I want to set it as the value of my UILabel. However, I want to check at a later point if the value of the string has changed, in comparison to the UILabel. Is it possible to set both the UILabel's text and attributedText properties? Will the attributedText simply overshadow the text property, so the text property can be kept as an internal value?
According to the documentation, assigning to text replaces attributedText with the same contents (albeit as an unstyled attributed string), and assigning to attributedText replaces text with the same contents (without any formatting information).
If you want to attach arbitrary information to an obj-c object, you should use associated objects.
How do I determine the current state of the UIResponderStandardEditActions for text formatting? For example, if I do the following:
[textView toggleBoldface:nil];
How do I query the textView to find out if the state of bold is now on or off? This is for just a cursor with no selection (i.e. range length is 0). As such, enumerateAttribute doesn't seem to work.
Thank you.
It appears the typingAttributes property (available in iOS 6) will log the attributes that will be applied to new text typed by the user, even with a selection length of 0. Thus revealing what the state of formatting options such as bold will be.
NSLog(#"textViewFormatting options: %#", [[self noteTextView] typingAttributes]);
I'm not finding anything useful in the docs, but I suppose it would be simple enough to just subclass UITextView, add a property on it like BOOL boldText and then wherever you call [textView toggleBoldFace:nil]; just toggle that property as well. And then when you need to check the state of the textView, just check the boldText property instead.