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.
Related
I have a "$" symbol in my app. And I want this char to apply a custom font in every string that contains this symbol. I know that font is related to UILabel but can I relate it to string? Is it possible to make it once and automate it?
The short answer is No. There is no way to automate that.
Essentially what you would have to do is set the attributedText property of the UILabel with an NSAttributedString that applied the correct style settings to symbol when it was found.
So you might create a function that takes a string and creates an NSAttributed string for it with your desired font settings wherever the symbol is found. Then you could add a method to UILabel to accept a string, pass it through your function, and set the attributedText of the label
But it's going to take some work on your part.
I'm trying to replace some text that was selected inside a UITextView with some NSAttributedString but only the following method is available:
textView.replace(UITextRange, withText: String)
As you can see, replacing text only accepts a String and I cannot find way to replace it with an NSAttributedString.
One thing I could do is to store the whole UITextView attributedText and then perform the desired changes on a NSMutableAttributedString and then I can replace the UITextView.attributedText to be the one of the NSMutableAttributedString, but this comes with some issues for me.
If the text is already long with some NSStorage and NSAttachments this will be way more expensive.
Is there any workaround?
Because you can't intermix String and NSAttributedString, there's unfortunately no workaround that will let both co-exist in a text field.
But you should be able to use replaceCharacters(in:with:):
existingAttributedString.replaceCharacters(in: range, with: replacementAttributedString)
I need to get the attributed string from UITextView. For example if user has formatted the text to bold or italic i need to save in variable or display in label as it is. Please guide me how to do this.
Thanks in advance!
I'm not sure what you're asking here, but UITextView has an attributedText property, so you can just get this by doing:
yourTextView.attributedText
Remember that an attributed string doesn't have only one attribute for the whole string (like an NSString), but depending on the index, it can have different attributes (so the first word can be in bold, and the second one can be italic).
To retrieve an attribute on a given index you can use the following property:
attributedString.attribute("your attribute", atIndex: yourIndex, effectiveRange: yourRange)
You can see more ways of accessing the attributes here.
You could do so:
UITextView *txtView;
UILabel *label;
self.label.attributedText = self.txtView.attributedText;
I have a UILabel and whenever I set its outlet the padding I created in Storyboards for it under attributed text disappears like it was never there. The text the stays stuck on the left side. How can I fix this issue?
#IBOutlet weak var mycoollabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
mycoollabel.text = "Wow"
}
Note: OP clarified in a comment that they are setting their padding using attributed text in Interface Builder.
The problem here is a confusion between text and attributed text, or, because these are code, it's text and attributedText.
On iOS, lots of things have a text property, and it's just a simple string of text that gets displayed somewhere without any sort of formatting. Usually the formatting is attached to the object that is showing the text, e.g. a label has a text colour and alignment properties that make its text look however you want.
On iOS, most (all?) of those things also have an attributedText property, which is a very different beast. Attributed text contains a string but also specific formatting instructions required to display that string – text attributes, hence the name.
So, OP is creating an attributed text configuration in their storyboard, then, in code, modifying the text property. This will overwrite their formatted string with a plain string, losing its layout configuration.
If you're looking to create an attributed string with a first line indent, you should try this:
let ps = NSMutableParagraphStyle();
ps.firstLineHeadIndent = 50
let attrs = [NSParagraphStyleAttributeName: ps]
let attributedString = NSAttributedString(string: "Hello, world!", attributes: attrs)
label.attributedText = attributedString
Note that there are a few different options available to you here, so you might need to find the right type of indent for your precise needs. If you right-click on NSMutableParagraphStyle and choose Jump To Definition you'll see all the options.
That's because a UILabel can have either plain text or attributed text. You set attributed text in the storyboard, and add some formatting (padding, etc.). Then, in code, you override that text (and all its formatting) with plain text.
If you want to keep the formatting, you need to set the label's attributedText property. And add all the necessary formatting to the NSAttributedString object you create for that.
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.