5

I'm using UITextView and I'm setting its font and attributed text by code.

Case 1: Custom-font - YES , attribtued text- NO

enter image description here

The textView text is displayed in custom font.

I want the attribtued text with custom font i.e Message : in this case shold be bolded.

Case 2: Custom-font - YES , attribtued text- YES

enter image description here

Only the attribtued text(Bold text) is displayed in custom font.

The code I have is:

- (void)loadTextView
{
    _textView.text=NSLocalizedString(@"more_info_text",nil);
    _textView.font=[UIFont fontWithName:@"BurbankSmall-Medium" size:16];

    NSRange rangeBold  = [_textView.text rangeOfString:@"Examples of invalid characters:"];
    NSRange rangeBold2 = [_textView.text rangeOfString:@"Restricted text:"];
    NSRange rangeBold3 = [_textView.text rangeOfString:@"Message :"];

    UIFont *fontText = [self boldFontWithFont:_textView.font];
    NSDictionary *dictBoldText = [NSDictionary dictionaryWithObjectsAndKeys:fontText, NSFontAttributeName, nil];

    NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:_textView.text];
    [mutAttrTextViewString setAttributes:dictBoldText range:rangeBold];
    [mutAttrTextViewString setAttributes:dictBoldText range:rangeBold2];
    [mutAttrTextViewString setAttributes:dictBoldText range:rangeBold3];

    [_textView setAttributedText:mutAttrTextViewString];

    _textView.editable=NO;
    _textView.selectable=NO;
}

- (UIFont *)boldFontWithFont:(UIFont *)font
{
    UIFontDescriptor * fontD = [font.fontDescriptor
                                fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold];
    return [UIFont fontWithDescriptor:fontD size:16];
}

When I comment the line setAttributedText:mutAttrTextViewString , evrything is displayed in custom font. When I uncomment it, only the attribtued text is showed in custom font, and rest is showed in default font.

Why is this happening ? If this is not possible I'm thinking to include html content as the attributed text, but I want to consider that as an option in worst case.

5
  • 1
    NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:_textView.text attributes:@{NSFontAttributeName:[UIFont fontWithName:@"BurbankSmall-Medium" size:16]}];? If you log mutAttrTextViewString with your current code, you should see that it doesn't have the font for the rest of the text (where you didn't put the bold font) and doesn't have to get by default the font set in _textView for PLAIN text. Commented May 9, 2016 at 16:17
  • I did set the font for the text view , and why the mutAttrTextViewString is not taking the textView font ? @Larme Commented May 9, 2016 at 16:23
  • I didn't say that it should replace your WHOLE use of mutAttrTextViewString. It's just that line NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:_textView.text];. For the rest, you keep calling [mutAttrTextViewString setAttributes:dictBoldText range:rangeBold]; and the two other lines. Commented May 9, 2016 at 16:23
  • 1
    For the same reasons given there: stackoverflow.com/questions/36064762/… (see @matt's comment). font property of UITextView is for it's text property (as commonly said plain text), and not for its attributedText property. Commented May 9, 2016 at 16:26
  • Do you want to put that comment as an answer ? @Larme It worked. It is quite strange that we need to specify the attributes again for the mutable string. Commented May 9, 2016 at 16:26

2 Answers 2

3

There is a related question here.
The main issue is that font (UIFont) property of UITextView is to be applied for its text (NSString) property (also named "plain text") and not for the attributedText (NSAttributedString).

So you have to applied the "normal" font effect to your NSAttributedString yourself.

So just replace:

NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:_textView.text];

with:

NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:_textView.text attributes:@{NSFontAttributeName:[UIFont fontWithName:@"BurbankSmall-Medium" size:16]}];
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of setting the font outside of the attributed text, try setting all the font changes at once inside of the attributed string:

NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:_textView.text];
[mutAttrTextViewString setAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"BurbankSmall-Medium" size:16] range:NSMakeRange(0, _textView.text.length)];
[mutAttrTextViewString setAttributes:dictBoldText range:rangeBold];
[mutAttrTextViewString setAttributes:dictBoldText range:rangeBold2];
[mutAttrTextViewString setAttributes:dictBoldText range:rangeBold3];

[_textView setAttributedText:mutAttrTextViewString];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.