iOS: URL link in UITextField with different text - ios

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.

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.

Custom iOS keyboard - Custom font output

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.

How do you add uneditable text in an iOS app

I now a label can create static text, but I have a few paragraphs that are neatly formatted that need to go to my app. How can I do this without using a label?
You can use a UITextView and set the editable property to NO

In iOS, how do I programmatically display dynamically changing values in a text field?

I have Objective C code that continually updates a set of numerical values. I need to display these values on the screen. That's it! I can convert numerical values into a text string, no problem. But how do I display this string in a UI element? Do I use a text box or a text field or a text view, or a...? I cannot find examples to show how to pass a string from code into the UI. I assume I need to set up a text thingy, and then periodically refresh the contents of that text thingy when the values change?
I assume the answer is simple but is just obscured by a smokescreen of technical jargon.
Thanks!
From the UI perspective you might want something like a
UITextView - multi-line text input
UITextField - single-line text input
UILabel - just text
For your purpose of just printing text, you should use UILabel, since you dont want / need any kind of input. You can access its text using:
// yourLabel is your current UILabel* you want to output yourValue to
yourLabel.text = yourValue;
Of course that yourValue needs to be converted to NSString before.
To actually get hold of the UILabel, you need to connect it from the Interface Builder as an IBOutlet. For tutorials on that topic, take a look at tutorials like Interface Tutorials by Ray Wenderlich or youtube or just google Interface Builder tutorial.

Add a Text Link to a TextView

Is it possible to add a text link into a TextView? I want the link to perhaps behave like a button, where I can assign an action to it.
EDIT: When I say assign an action, I mean actually giving it something in the code. I'm wondering if it's possible to dynamically add a "button" into text that I can assign a coded action to.
Live scenario
Think of something like a dictionary app. Maybe the definition of one word uses another word that you might not know the definition of, so being able to click on that word to instantly search it rather than having to type it in would be a nice user friendly feature. It seems rather unlikely, though, I guess.
I would recommend using NIAttributedLabel from Nimbus, an open source iOS library. You can specify text ranges that are links, and you get delegate messages sent when a user taps on it.
Main Nimbus site: http://nimbuskit.info/
NIAttributedLabel docs: http://docs.nimbuskit.info/interface_n_i_attributed_label.html
in the inspector, go to the Text View Attributes tab then make sure "Detect Links" is checked.
Yes you can. Add the URL into the text view, then open up the Attributes Inspector. You will see an option in there to detect links.
I know of a way, but its a LOT of work. First, you have an NSAttributedString that you have the text view display. Second, attribute the range of text you want to be the button. Third, assign a tap gesture recognizer to the text view and in the method called by the recognizer, you'll use core text to determine if the tap happened over the range of text that represents the buttons.
Heres how youll use core text: create a framesetter with the attributed string. Create a frame from the framsetter with the shape of a square that is the frame of the text view, inset by the padding of the text view. The frame will allow you to get the y origins of every line in the text view and and once you know what line the tap happened on, you can use the line to then figure out exactly what character was tapped on that line by giving it an x offset. Once you know character index on the line, you can add it to the beginning of the range of the line and get the index of the character within the whole string. Then you can check if its within the range of the text that is your button. If it is, you can then call a method to simulate a target action type behavior.
Ive explained the process of how to accomplish this and specified what kinds of core text objects youll need, ill let you look up the specific api details:
https://developer.apple.com/library/mac/documentation/Carbon/Reference/CoreText_Framework_Ref/_index.html#//apple_ref/doc/uid/TP40005304
You can also use my objc core text wrapper:
https://github.com/mysterioustrousers/MYSCoreText
What about CoreText? It Can draw many kinds of Text .

Resources