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
Related
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.
I am trying to create a custom keyboard using the app keyboard extension. I am happy with the layout but the output is depended on the UITextField's font.
Is there a way to force a different font (use special characters?) while using the keyboard ?
Thank you
It depends.
Text field (or any other view that draws text) uses 2 informations on how to show some text. One is the sequence of characters called String and the other one is how the string should be represented. The second one is then split it things like fonts, colors, line height, line breaking and wrapping...
So the keyboard alone is not enough to for instance present a certain part of word using different fonts. You need at least a bit of access to the item that represents the text. So if you have no access to your text field then the answer is; No, you can not fore a different font when using different keyboard.
If you do have the access then the answer should lie in NSAttributedString. It is a string you can assign to most items under attributedText. This class wraps your raw string and can add many properties to parts of text you want to change. That includes using a different font.
Another approach would be using HTML tags. Again you will need to process this using for instance NSAttributedString or display it with another element like web view.
I would try it with using NSAttributedString. Hook up to delegate and implement textField(: shouldChangeCharactersIn: replacementString:. The implementation itself may still not be easy though.
I am currently writing my first app on ios using swift 3. I have a plist that has a list of different pieces of information and each is a long paragraph with sections throughout it. for each section, I want to bold the text for the title or at least have the option to format it a certain way rather than just display all the text. I currently have a simple table view that displays text in a text view once tapped. I cannot figure out how to read the paragraph into a string, and compare parts of the string and bold that specific text.
For example, If I had a string that was read from the plist and said:
"My name is #Bob and I like to #dance."
How could I change "#Bob" to "Bob" and "#dance" to "dance" without hard coding it?
#IBOutlet var paragraphTextView: UITextView!
.
.
.
if let text = paragraph["Text"] {
paragraphTextView.text = text
}
The simplest solution is to use some HTML markup in the text in your plist file.
Instead of the plain text:
My name is #Bob and I like to #dance.
Use:
My name is <b>Bob</b> and I like to <b>dance</b>.
This gives you far more flexibility. You can add bold, underline, and italic simply by using the appropriate <b></b>, <u></u>, and <i></i> tags as needed. It also allows you to markup more general ranges than single words.
Once the text has the proper markup, you create an NSAttributedString from the marked up string and then set that to the text viewsattributedTextproperty instead of using thetext` property.
For a good example of how to create an NSAttributedString from HTML text, see HTML Format in UITextView
I want to insert a URL hyperlink into a UITextField that has different display text.
This is super easy in html:
Go To Google
How can I do this in iOS?
You can change the text style inside your UITextField via an attributed string sent to the text field's "attributedText" property.
However, what I suspect you really want to do is have a link that's clickable. I would recommend using a UIButton with a custom type (i.e. no border) and you can set the color and the underline style.
But like Evan said, if you have multiple lines of text, it may be smarter to use a UITextField where you set "editable" to NO and turn on the LINK traits (both of these you can do from the object inspector in Xcode).
Alright, so here's what I did to get what I wanted. I used UIWebView, and simply linked it to an html page in the project that has the text, and hyperlink at the bottom with different text displayed.
Answer is here :
If you want it as clickable Hyperlink,
NSString *string = #"Go To Google";
You need to add "BACKWARD SLASH" before ". That's it.
If I want formatted text (variations on size, position of certain text elements, etc) but don't want to use UIWebView, is Core Text my only remaining option?
I'm shying away from UIWebView primarily because of problems getting the layout right when switching between the iPad and the iPhone. Rather than doing conditional code and dynamically adjusting the HTML depending on device/orientation, I thought I would go ahead and look at using Core Text.
Suggestions?
Perhaps NSAttributedString could help your cause?
An NSAttributedString object manages character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string. The cluster’s two public classes, NSAttributedString and NSMutableAttributedString, declare the programmatic interface for read-only attributed strings and modifiable attributed strings, respectively.
Using only Apple's SDK, yes Core Text is your only other option. I am sure there are third-party implementations, and you could ask that question if you are interested. Apple has a decent example which shows both how to draw text using Core Text and how to handle keyboard input.