Changing font formattings of selected word/line in a UITextView - iphone

Has anyone tried to change the font formatting(bold/italic/color) of a selected word or sentence in a UITextView, like a text editor application? I have been trying to do this but it seems formatting is applied only to the whole text, not on individual words or sentences.

you can change any specific word font, style and size also,
Make UITextView text to "Attributed", by default it was "Plain"
and then you can perform edition on selected text.

UITextView cannot style text fragments. Styles, colors and fonts are applied to the entire text. You may wish to use a UIWebView or Core Text to render your text instead.

Related

how to change the color of the first text in UITextView

is there a way where we can change the color of the first text in UITextView and the rest of the text will in default color which is black
If you are asking if you can change the color of a word or sentence of a UITextView but not the entire content, the answer is no unfortunately.
Check this answer here, Change font of individual words in UITextView in Interface Builder
It is possible to use a UIWebView to render the text in the manner you are asking. You could also draw it yourself using CoreText, but that would be quite a pain.
Hope this helps,
Matt

Adding rich text in localizable.strings

Is it possible to add some sort of styling to my localizable.strings file.
For example
"HELLO_WORLD" = "Hello \bold WORLD \bold";
No, it is not possible. Localized strings can be used in many places in your code and UI, only some of which may support display of rich text. For example, a UILabel has a text property that you can set from a localized string but it has no capability of showing rich text.
You will need to choose some way of displaying rich text in your UI then decide how you are going to encode that text in a localized string. You may end up having to parse the localized string and do custom text drawing depending on where and how you want to display the text.

iOS - Override UITextView Methods?

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.

Xcode UITextView Change textcolor of specified word

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.

Underline / Bold in UITextView

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!