12

I have a UITextView and there are certain words I'm casting with NSString stringWithFormat that I'd like to be bolded.

I have looked around Stack Overflow and tried to follow the the postings but I guess I'm not understanding it.

Here's what I've been playing around with:

    NSRange boldedRange = NSMakeRange(0, 4);
    NSString *boldFontName = [[UIFont fontWithName:@"Helvetica-Bold" size:100]fontName];


    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.name];

    [attrString beginEditing];
    [attrString addAttribute:NSFontAttributeName
                       value:boldFontName
                       range:boldedRange];
    [attrString endEditing];

    self.resultsTextView.attributedText = attrString;


    self.resultsTextView.text = [NSString stringWithFormat:@"One day, %@ was taking a walk and saw a %@ boy.  He was %@ a %@.", attrString, self.adjective, self.adverb, self.noun];
May Yang
  • 523
  • 1
  • 5
  • 18
  • 1
    Been a while since I've worked with attributed strings, but seems to me that your problem is that you are setting `resultsTextView` to be attributed string, but the next line you are setting it back to be of non-attributed `NSString`. `NSString` does not have attributes, so you are basically 'undoing' any attributed you 'did'. If I remember correctly, you can't have both `attributedText` and `text` properties, It is either one. – AMI289 Jan 17 '15 at 07:20
  • See Answer You can bold Particular text in textView. – Kirit Modi Jan 17 '15 at 08:16
  • @KiritModi I know you can mate. But see question, more specifically the last line of code. he eventually uses `NSString` for his `UITextView`, and not `NSAttributedString`. You can't bold text in a standard `NSString`. – AMI289 Jan 17 '15 at 08:22
  • 1
    Its not possible to set NSStting to bold text according to your last line. – Kirit Modi Jan 17 '15 at 08:24
  • You have to Use only NSAttributedString To bold text and regularText MIXUP – Kirit Modi Jan 17 '15 at 08:26

4 Answers4

21

You can also set it the following way if you want by setting a dictionary as a whole, as attribute

NSString *strTextView = @"This is some demo Text to set BOLD";

NSRange rangeBold = [strTextView rangeOfString:@"BOLD"];

UIFont *fontText = [UIFont boldSystemFontOfSize:10];
NSDictionary *dictBoldText = [NSDictionary dictionaryWithObjectsAndKeys:fontText, NSFontAttributeName, nil];

NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:strTextView];
[mutAttrTextViewString setAttributes:dictBoldText range:rangeBold];

[textViewTermsPolicy setAttributedText:mutAttrTextViewString];
channi
  • 1,028
  • 9
  • 16
  • What if I wanted to bold another word besides BOLD? For example, This is some **demo Text** to set **BOLD**? – May Yang Jan 17 '15 at 08:32
  • I hope you might have solved the issue you were facing but still if you haven't just replace "BOLD" with "demo Text" – channi Jan 19 '15 at 05:59
6

Use the below code to set Attribute string in TextView.

    NSString *infoString =@"I am Kirit Modi from Deesa.";

    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc] initWithString:infoString];

    UIFont *font_regular=[UIFont fontWithName:@"Helvetica" size:20.0f];
    UIFont *font_bold=[UIFont fontWithName:@"Helvetica-Bold" size:20.0f];

    [attString addAttribute:NSFontAttributeName value:font_regular range:NSMakeRange(0, 4)];

    [attString addAttribute:NSFontAttributeName value:font_bold range:NSMakeRange(5, 15)];

    [attString addAttribute:NSFontAttributeName value:font_regular range:NSMakeRange(16, infoString.length - 15 - 1)];

    [self.txtView setAttributedText:attString];

OutPut :

enter image description here

Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
0

Check out @CrazyYoghurt improvement on @Bbrame and @BenoitJadinon on this previous SO question 3586871 I've been using it for over a year and it works great. One limitation: I don't think you can bold multiple times the same string if it appears more than once in your original string. But you can probably expend the code to make it do so if you wish.

Community
  • 1
  • 1
Litome
  • 653
  • 8
  • 12
0

If you also need to filter some word from the UITextView and make it underline/change color of that particular text only then you can use the below code.

Here, I'm getting all the doc text in the Content string and filter some particular text that is in Hebrew language.

NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithString:content attributes:nil];
[aStr addAttribute:NSLinkAttributeName value:@"http://www.apple.com" range:[content rangeOfString:@"מדיניות פרטיות"]];
[aStr addAttribute:NSLinkAttributeName value:@"http://www.google.com" range:[content rangeOfString:@"לינק"]];
textview.linkTextAttributes = @{NSUnderlineStyleAttributeName : @(NSUnderlineStyleSingle)};
textview.delegate = (id)self;

//...You can as per your custom color

[aStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[content rangeOfString:@"מדיניות פרטיות"]];
[aStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[content rangeOfString:@"לינק"]];

//Here You can also add the tap gesture on that text. //Tap gesture

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedTextView:)];
             [textview addGestureRecognizer:tapRecognizer];
             [textview setAttributedText:aStr];
             textview.textAlignment=NSTextAlignmentRight;

//For getting the text location in the tap gesture

-(void)tappedTextView:(UITapGestureRecognizer *)tapGesture

{
    UITextView *textView = (UITextView *)tapGesture.view;
    CGPoint tapLocation = [tapGesture locationInView:textView];
    UITextPosition *textPosition = [textView closestPositionToPoint:tapLocation];
    NSDictionary *attributes = [textView textStylingAtPosition:textPosition inDirection:UITextStorageDirectionForward];
    NSString *urlStr = attributes[NSLinkAttributeName];
    if (urlStr)

    {
        //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",url]]];

        PrivacyViewController *next = [PrivacyViewController new];
        [self.navigationController pushViewController:next animated:YES];
    }
}
Yash
  • 171
  • 2
  • 8