iPhone iOS NSMutableAttributedString editor example - iphone

I'm looking at an NSMutableAttributedString example by apple : http://developer.apple.com/library/ios/#samplecode/CoreTextPageViewer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010699-Intro-DontLinkElementID_2 and see that they load attributed strings as XML files.
Are there any examples of how to use a text editor create an attributed string on an iPhone/iPad? I don't want to create attributed string programmatically, but want the user to be able to create an attributed string using a text editor.
Is this kind of work possible?

Mixing three or four answers I have read on this forum, you can do this:
Create your own user interface buttons for underline, add color... (I suggest on storyboard, but this is your election)
Subclass UITextView and override the drawRect method for display, using CoreText, because you'll need an editable text view. Here you have how to do it.
Create a method for change the attributes to each letter user writes after touching, p.e., the "underline" button.
I hope it helps you!!

Related

Is it possible to assign different attributes to different characters in a string in UITextView?Swift 3

I placed a large text in a UITextView In Xcode in interface builder. Is it possible to assign different attributes (colors, headings, alignments..etc) to different parts of the string. Using MutableAttributedString would not work for me since the string is longer than 5000 words, it would take ages to format it.
There is a simple way to do this. Outside Xcode :)
Open TextEditor in MAC and paste your text into it and format it as per your requirement and again copy it.
Now Add your UITextView into your ViewController in Storyboard and make that textView Attributed.
Now Paste your formatted text there in the text field.
Check below screenshot for reference.
Hope this will help you.

How to keep 2 different fonts within the same UITextField or UITextView?

I want to set 2 different fonts within the same UITextField and UITextView . How to do it?
Its a bit of work - you'll need to use Core Text and NSAttributedString to do this.
There are plenty of tutorials and examples, although I'd suggest using someone else's already-made UILabel subclass such as:
OHAttributedLabel
or
TTAttributedLabel
As these usually have some convenience methods to make handling a lot easier.
I would do it with 2 custom textfields overlaying, both backgroundcolor:clearColor, maybe stuffed on an image that represents the background.
I don't think it is possible to handle 2 different fonts within the same UITextField or UITextView. If you want to have different font style you can either set different font style within a UIWebView or use the coreText API.
Here are some links that might help:
iPhone Development - Setting UIWebView font
the official doc on core text: https://developer.apple.com/library/mac/#documentation/StringsTextFonts/Conceptual/CoreText_Programming/Introduction/Introduction.html
I know you already picked a valid answer but... don't do it that way... it's not worth it. Use a webview instead and draw everything with html.
In interface builder change Text View's Text field to Attributed. In that small editor that appears you can change the font/format/color of the selected text, like in any advanced text editor.

Adding hyperlink to any text in UITextFiled and perform a custom action - iPhone 4

I am trying to accomplish the following:
Read and Parse the text in a UITextField. Identify all the numbers in the UITextField and convert these into hyperlinks.
When these hyperlinks are clicked perform a custom action, which is to display a UIActionSheet and based on the selection assign the number (in the hyperlink) to another UITextField instance
For example if the UITextField has the text - "This is a sample test with number 123445 and more numbers 44555, 66777".
I should be able to parse the above text, detect all three numbers and add hyperlinks to them.
For the first part (parsing) I found out that there is a NSRegularExpression class that can be used to detect patterns in a text. But I could not find a way of adding hyperlinks to the matched numbers. I tried looking at Three20 documentation and could not figure out a way. Even tried the answer in this link - Just how to you use TTStyledTextLabel? but it only auto detects URLs and adds hyperlinks to them, I want to add hyperlinks to any custom text.
Can someone please help me with this. Please do not ask me to use WebView. I would really appreciate some code snippets. Thanks in advance. I am using xCode4.
If you don't need the text to be user-editable directly, you may use my OHAttributedLabel class to achieve this. (here on github)
This allows you to display any NSAttributedString and can also autodetect links, phone numbers and everything Apple's NSDataDetector class is able to detect. You can also add custom links to your label on any part of the text.
See the sample project included in my github repository for more details.
It is very customizable, both for link colors, underline style, action to perform when a link is tapped, which link types it should autodetect, and you can add any custom links and style you need on the text.

In iOS, how do you embed images inline with text (in a UITextView)?

How can you create a UITextView that can have images inline? I looked up NSAttributedString, but it seems iOS does not support image attachments. Any ideas for an editable text view that can display images (I suppose RTF would do this)?
Thanks in advance for your help!
The only method I am aware of is to use a UIWebView. There is no way to add images to a UITextView, unfortunately, just plain text.
You could use the NSTextAttachment class. It lets you insert images inline of text.
I found an example of this in a cool project here: https://github.com/dzog/ImgGlyph. ImgGlyph provides UITextView and UILabel subclass implementations that help you replace specific strings with images and ensures they're sized correctly with respect to the text surrounding it.

How to change font color of individual characters in a UIPickerView row?

I have an app which shows text in a UIPickerView based on a search. I want to highlight specific letters in the string that are there as a result of a wildcard character.
For example, if I searched for "CA?", one of the rows will show "CAT" and I want only the letter "T" to be in the color blue.
Any ideas? The user gets immediate feedback as he types so performance is important.
To create a string that has different font properties for different characters, you would generally use NSAttributedString. However, UIPickerView doesn't seem to directly support using NSAttributedStrings as the labels for your picker components, nor does UILabel seem to support them. You might have to create a custom UIView subclass and return it from pickerView:viewForRow:forComponent:reusingView: in your UIPickerViewDelegate.
Thanks to David for the tip to get me started.
I ended up using the Three20 library and returning a TTStyledTextLabel from pickerView:viewForRow:forComponent:reusingView: with the text property set to [TTStyledText textFromXHTML:myXHTML] along with a TTDefaultStyleSheet to define the colored spans. Works great and seems to be very fast in the UIPickerView component.