3

Possible Duplicate:
UITextView formatting with fontname and bold

I am trying to Bold selected text of UITextView.I don't want to use UIWebView in my application.

How can I do that programmatically in UITextView????

Community
  • 1
  • 1
user572555
  • 51
  • 1
  • 2
  • 5
  • 2
    Your subject line should have been **How to bold selected text in UITextView?** – Mahesh Jan 14 '11 at 06:03
  • UITextView is all single font. You can't make modifications to certain parts of it. BTW, just wanted to know how will you do it with `UIWebView`. – Mahesh Jan 14 '11 at 06:15
  • 1
    You might also want to check out [How do you use NSAttributedString?](http://stackoverflow.com/questions/3482346/how-do-you-use-nsattributedstring) – ma11hew28 Nov 20 '11 at 22:38

3 Answers3

3

Try -

textViewInstance.font = [UIFont boldSystemFontOfSize:14];

Edit 1: I didn't notice your statement of selected text. This just bolds the entire text. Sorry ;)

Mahesh
  • 34,573
  • 20
  • 89
  • 115
3

I was facing a similar issue. According to the documentation and the lack of answers from google, I doubt there's a solution as UITextView has only one format setting for all its text. It's all or nothing for UILabels and UITextView. The only way I see it, is to use UIWebView which gives you more freedom of styling individual words and text in the view. Good luck. :)

HM1
  • 1,684
  • 2
  • 18
  • 25
3

In UITextView i dont have any idea but i am done this in UILabel.

First set text in UILabel and setFrame of that label according to the text Height. After that use the regular Expression for the get frame for text which you want to make bold and add new label on that frame with bold font and text.

If you want to demo for above explanation then see the below link. It's have nice implementation.

FancyLabel_1.0

Edit: Now you can make selected text bold using "NSMutableAttributedString". See below peace of code.

NSString * string = @"<Your Full String>";
NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:string];
[attStr addAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} range:NSMakeRange(0, string.length)];

NSString * subString = @"<String that you want to make bold>";
[attStr addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:13], NSForegroundColorAttributeName:[UIColor redColor]} range:[string rangeOfString:subString]];
[<Your UILabel Object> setAttributedTitle:attStr forState:UIControlStateNormal];

Thanks, MinuMaster

MinuMaster
  • 1,457
  • 1
  • 13
  • 29