I want to override the method that draws the text into a UITextView. What is the correct method to override?
Update 2016-02-09: This answer is now wrong (and has been since iOS 6). You can now use UITextView by setting an NSAttributedString to the attributedText property instead of setting a normal NSString to the text property.
Relevant newer question: https://stackoverflow.com/a/14231900/458205
Original Answer
There's no way to set different colours for different words in a UITextView.
There are multiple replacements for a UITextView which support rich text editing (listed bellow).
However if you only want to give different text different color's in a UITextView (like syntax highlighting) the easiest thing to do is just use a UITextView itself for the editing with a CoreText overlay displaying the coloured text.
https://github.com/SquaredTiki/EditableCoreTextOverlay
EGOTextView : http://www.cocoacontrols.com/platforms/ios/controls/egotextview
BCTextView : http://www.cocoacontrols.com/platforms/ios/controls/bctextview
JTextView : http://codaset.com/jer/jtextview
http://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=rich+text&commit=Search
Multiple colors in a UITextView is not possible at this point in time. You can set one text color only.
If you need multiple text colors, the only way is loading a RTF file in a UIWebView, but that doesn't allow editing.
Maybe in iOS 5 there'll be RTF support for UITextView.
Related
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.
I would like to change the font color of a certain word inside a UITextView.
if([text1.text isEqualToString:#"value"]) {
}
That's what I have right now, and I want the word value to change font color, any ideas? Sorry if it's simple but I'm still learning ;)
It's not possible, sorry.
from UITextView Class Reference
This class does not support multiple
styles for text. The font, color, and
text alignment attributes you specify
always apply to the entire contents of
the text view. To display more complex
styling in your application, you need
to use a UIWebView object and render
your content using HTML.
Is it possible to underline or embolden certain bits of text in a UITextView?
For example my Text View has headings in it, and would like those underlined...
Random Mode
In random mode, you can generate numbers...
Sweepstake Mode
In sweepstake mode...
If not, what is the best way to achieve this?
Thanks
Use should use NSAttributedString, and use controllers for drawing NSAttributesString.
Controller for NSAttributedString
Note: you can't use UITextView to display a NSAttributedString
Update
From iOS6, UILabel now support NSAttributedString, you should use UILabel directly instead of OHAttributedLabel as it is now natively supported by the OS.
I believe NSAttributedString is what you're after, it's available in iOS 3.2 or later: look at this question
I know this is an old thread, but this is something I just discovered myself. At least in Xcode version 4.6.3 this is possible by using an attributed textView. What's even better is that it's possible to all be done in Interface Builder!
Here are the steps:
Place your textView at the desired location
Select the textView and open up the Attributes tab under the Utilities panel
Change the textView text to "attributed"
Enter your desired text
Now, highlight whatever text you want bolded, underlined, etc.
Click on the "T" button next to the fontName
In the popup, select your desired typeface (ex: Bold)
You should see the desired typeface displayed in the Utilities panel
Enjoy!
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.
I want to display texts in UITextView with different colors and underline some of them (to show they are erroneous - commonly done in spellchecker) but that's not possible with the current version of UIKit. What other options should I pursue?
I am thinking of subclassing UIScrollView (as I am dealing with a lot of texts) and blit the text into a UIView (as the content) using CoreGraphics, and as new text is entered, I will blit new text onto it. Is that a good approach?
The recommended approach is to generate HTML and use a UIWebView. That is a scroll view so you should have all the functionality you need.
If you want more control over the text then your approach is good although you can probably use the UIKit NSString extensions to draw your text. Specifically, you want drawAtPoint:withFont: