0

I am using a little bit of code to change the text in an NSTextView

NSString *string = @"This is a title\n\nThis is some info";
[content setString:string];

I would like to make the text "This is a title" the only text that is bold when displaying in the NSTextView and the rest normal text. Is there any way to do this? Maybe like how the StackOverflow editor works? __bold text here__

Thanks in advance!

SteppingHat
  • 1,199
  • 4
  • 19
  • 50

1 Answers1

0

I'm not sure if you can just apply boldness as that's usually part of another set of glyphs. But how's this work...

NSString* textUnattributed = @"This is a title\n\nThis is some info";
NSMutableAttributedString* textAttributed = [[NSMutableAttributedString alloc] initWithString:textUnattributed];
[textAttributed addAttribute:NSFontAttributeName
                       value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0]
                       range:[textUnattributed rangeOfString:@"This is a title"]];
content.attributedText = textAttributed;
rob5408
  • 2,972
  • 2
  • 40
  • 53
  • I get an error with prompt `Use of undeclared identifier 'prompt'` – SteppingHat May 23 '14 at 06:01
  • @SteppingHat, whoops, I grabbed some code from one of my projects, let me fix. – rob5408 May 23 '14 at 12:30
  • all good. One other problem as well. `content.attributedText` isn't a thing I can apply to an NSTextView but instead I can use `content.attributedString` however I get an error `No setter method 'setAttributedString:' for assignment to property` – SteppingHat May 23 '14 at 13:25
  • Also... for `range:[textUnattributed rangeOfString:...]` I get an error also saying `No visible @interface for 'NSMutableArray' declares the selector 'rangeOfString'`. In my case, I'm replacing `textUnattributed` with an array called `result`. Essentially taking the array, bit by bit and turning it into a massive string within a for loop. Its a multidimensional array and any part of the array where the second array is indexed 0 I want to bold. So in the for loop it would look like `result[i][0]`. Sorry for the complexity haha. But essentially how could I make this work with an array? – SteppingHat May 23 '14 at 13:28
  • It might be time to fire up another SO question. I'll be happy to help you out over on that one too. – rob5408 May 23 '14 at 15:32
  • Done! You can find the new question at [http://stackoverflow.com/questions/23840682/nstextview-making-bold-text-using-an-array](http://stackoverflow.com/questions/23840682/nstextview-making-bold-text-using-an-array). Thanks for all your help! :) – SteppingHat May 24 '14 at 02:14