Apply font for certain string - ios

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.

Related

Detect link attribute and callout as link for NSAttributedString in iOS

I have a link within an attributed string that is being then set for a textview. For Accessibility purpose, I want that link to be called out as a link trait, is it currently possible for an attributed string to call out different traits within the string? If not what could be an ideal solution to do that then?
You can look for all the "style runs" of attributes within the text view's attributed text by calling attributes(at:longestEffectiveRange:in:) repeatedly until you come to the style run you want. Thus you can locate the link and obtain its value.

Replace UITextView selected text with NSAttributedString

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)

get the attributed string from uitextview

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;

How to show two separate links on different Strings in UILabel?

Please look at the below image, Red Color Texts are two different strings and Ash color String are another string. I concatenate those strings into one String and i assigned that string into UILabel.
i used NSMutableAttributedString for applying colors and Font styles to NSString
Now i need to show two separate Links on the Red Color Strings " SIVASAGAR" and "THE ORDER:1886", when user clicks on that links, it redirects to different views.
Is it possible to achieve this type of scenario using UIButton or STTweetlabel or something else?
NOTE: All the names ,i mean Red Color Strings Positions(X and Y values) will change Dynamically according to requirement.
I think you should use UITextView and add:
[attributedText addAttribute:NSLinkAttributeName value:#"yourCustomSchemeUrl://" range:linkRange];
and implement Custom URL Scheme

What happens if I set both UILabel.text and UILabel.attributedText?

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.

Resources