Skip to content

下载的CoreTextDemo中定制文字颜色无效问题 #24

@kepuna

Description

@kepuna

大神您好,最近在拜读您的 《基于 CoreText 的排版引擎:基础》,然后https://raw.githubusercontent.com/tangqiaoboy/iOS-Pro/master/DemoProjects/CoreText.zip 下载的CoreTextDemo里面,我测试的时候发现,通过range修改富文本的颜色无效,但是设置富文本的字体大小有效果!麻烦您能测试看一下吗?

测试步骤如下:

  1. 下载了CoreText的zip包
  2. 将ViewController里viewDidload的代码改成如下代码即可,我测试的时候,文字颜色改变不了,但是我将设置颜色改成设置字体大小可以生效
  • (void)viewDidLoad
    {
    [super viewDidLoad];

    CTFrameParserConfig *config = [[CTFrameParserConfig alloc] init];
    config.width = self.ctView.width;
    config.textColor = [UIColor blackColor];

    NSString *content =
    @" 对于上面的例子,我们给 CTFrameParser 增加了一个将 NSString 转 "
    " 换为 CoreTextData 的方法。"
    " 但这样的实现方式有很多局限性,因为整个内容虽然可以定制字体 "
    " 大小,颜色,行高等信息,但是却不能支持定制内容中的某一部分。"
    " 例如,如果我们只想让内容的前三个字显示成红色,而其它文字显 "
    " 示成黑色,那么就办不到了。"
    "\n\n"
    " 解决的办法很简单,我们让CTFrameParser支持接受 "
    "NSAttributeString 作为参数,然后在 NSAttributeString 中设置好 "
    " 我们想要的信息。";
    NSDictionary *attr = [CTFrameParser attributesWithConfig:config];
    NSMutableAttributedString *attributedString =
    [[NSMutableAttributedString alloc] initWithString:content
    attributes:attr];
    [attributedString addAttribute:NSForegroundColorAttributeName
    value:[UIColor redColor]
    range:NSMakeRange(0, 7)];

    CoreTextData *data = [CTFrameParser parseAttributedContent:attributedString
    config:config];
    self.ctView.data = data;
    self.ctView.height = data.height;
    self.ctView.backgroundColor = [UIColor yellowColor];
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions