如何在NSTextView中获取某些字体文本字符串?

时间:2015-07-19 07:08:45

标签: objective-c macos swift nstextview

我在NSTextView中使用不同的字体,我想要做的是在tableView中显示某种字体的文本字符串。

如何枚举textView.attributedString()并获取带有NSFont名称作为参数的某些文字字符串?

1 个答案:

答案 0 :(得分:0)

我目前无法尝试运行此代码,但我相信以下内容可以为您提供帮助:

NSMutableArray *matchingStrings = [NSMutableArray new];
[attributedText enumerateAttribute:NSFontAttributeName
                                    inRange:NSMakeRange(0,attributedText.length)
                                    options:0
                                 usingBlock:(void (^ nonnull)(UIFont *font,
                                 NSRange range,
                                 BOOL * nonnull stop)){
    if ([font.fontName isEqualToString: @"AvenirNext-Bold"])
    {
         NSAttributedString *attributedSubstring = [attributedText attributedSubstringFromRange:range];
        [matchingStrings addObject:attributedSubstring.string];
    }
}];