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!
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 have a few UITextFields and I would like the user to be able to change the font's appearance within those fields.
Right now I have a UIToolbar with 5 buttons used for font controls. One for font name, one for size, one for color and one for alignment.
To change font, I display a picker with the fonts family names, letting the user pick default font for that family.
For the font color, I display another toolbar with a font size slider
For font color, I display an open source color picker
For font alignment, I display a button that cycles through the left/center/right alignment.
Finally there's a close button that hides the toolbar.
Currently all of my code is in one controller, but I would like to have it refactored into a separate, reusable class.
Since I'll be rewriting code, are there any open source font controls like I'm describing above? Maybe I can just plug in something that's already made into my project?
Thank you!
It wouldn't be very hard at all. Instead of pickers use a scroll view with buttons and labels above them using the font. As for size you could do the same!
This would be easy in Xcode 4, not so sure in 3.X.X
I found that the attributed string example by apple does have a table view-like font controller that includes a preview of what fonts look like. I think extending that controller may be the easiest thing to do:
http://developer.apple.com/library/ios/#samplecode/CoreTextPageViewer/Introduction/Intro.html
I'd like to offer a user of my app the ability to highlight or change color of selected words within a UITextView. I know how to change the entire text color:
textView.textColor = [UIColor redColor];
This is not what I'm looking for. I need to know how to change color of arbitrary sequences of characters within a UITextView. I have not seen such feature in the text views up to date, so I think it's not supported out of the box. Are there any open source projects or examples on how to change color or highlight blocks of text within a UITextView?
Thank you!
You can create CATextLayer and assign NSAttributed string to it. Have a look at here. You can make use of DTCoreText
In my app, I have modified the UILabel and made it underline the text if the phone number or email appears.
but I want to highlight the text when the phone number or email is touched as shown below (I want something like what has been shown below by the dark gray highlight in background of the text "http://www.foodreporter.net".
I want to do this without use of CoreText as my app targets from iOS 3.1.2 onwards. So I don't want to use the CoreText and Attributed strings.
I want to subclass the UILabel and do it somehow. How can it be done?
I don't have the option to use OHAttributedLabel and TTTAttributedLabel.
I suggest for a workaround. What you can do is, use custom UIButton instead of label. Set the default and highlighted image of the button according to your need.(Simple text for default, and highlighted text for highlighted).
Happy Programming
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.