使用自定义字体时,UITextView使文本无法正常工作 [英] UITextView attributed text not working when using custom font

查看:145
本文介绍了使用自定义字体时,UITextView使文本无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 UITextView ,我正在按代码设置字体和属性文本。

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

案例1: 自定义字体 - 是归属文字 - 否

textView文本以自定义字体显示。

The textView text is displayed in custom font.

I想要使用自定义字体的属性文本,即消息:,在这种情况下,请加粗。

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

案例2: 自定义字体 - 是归属文本 - 是

只有特定文本(粗体文本)以自定义字体显示。

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

我的代码是:

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

当我评论行 setAttributedText:mutAttrTextViewString ,evrything以自定义字体显示。当我取消注释它时,只有属于自定义字体的属性显示,而其余的以默认字体显示。

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.

为什么会发生这种情况?如果这是不可能的,我想将html内容作为属性文本包含在内,但我想在最坏的情况下将其视为一个选项。

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.

推荐答案

此处存在相关问题。< br>
主要问题是 UITextView的 font UIFont )属性将应用于其文本 NSString )属性(也称为纯文本) )而不是 attributedText NSAttributedString )。

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).

所以你必须自己将普通字体效果应用到 NSAttributedString

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

所以只需替换:

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

with:

NSMutableAttributedString *mutAttrTextViewString = [[NSMutableAttributedString alloc] initWithString:_textView.text attributes:@{NSFontAttributeName:[UIFont fontWithName:@"BurbankSmall-Medium" size:16]}];

这篇关于使用自定义字体时,UITextView使文本无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆