UITextView attributed text not working when using custom font

↘锁芯ラ 提交于 2019-12-01 07:28:07

问题


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

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

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

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.


回答1:


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]}];



回答2:


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];


来源:https://stackoverflow.com/questions/37120535/uitextview-attributed-text-not-working-when-using-custom-font

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!