0

私は iOS 開発の初心者であり、TTTAttributedLabel を使用して NSString 内の繰り返しテキストを強調表示しようとしていますが、常に最初に出現したものを太字で取得します。これは私が使用している小さなサンプルですが、何が間違っていますか?. ライブラリがこの種の機能に対応しているかどうかはわかりません。

UIView *viewOfSection1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1700)];
viewOfSection1.backgroundColor = [UIColor whiteColor];
KMSection *section1 = [[KMSection alloc] init];
section1.view = viewOfSection1;
section1.title = @"Some Title";
section1.colorForBackground = [UIColor whiteColor];

TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 300, 1700)];
label.font = [UIFont systemFontOfSize:14];
label.textColor = [UIColor darkGrayColor];
label.lineBreakMode = LINE_BREAK_WORD_WRAP;
label.numberOfLines = 0;
label.textAlignment = NSTextAlignmentJustified;
[label setTextInsets:UIEdgeInsetsMake(0, 40, 0, 7)];

NSString *text = @"This is text sample , text sample"

[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
    NSRange boldRange = [[mutableAttributedString string] rangeOfString:@"text," options:NSCaseInsensitiveSearch];
    NSRange boldRange1 = [[mutableAttributedString string] rangeOfString:@"sample" options:NSCaseInsensitiveSearch];

    UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:14];
    CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
    if (font) {
        [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange];
        [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange1];
        CFRelease(font);
    }

    return mutableAttributedString;
}];

[viewOfSection1 addSubview:label];

return @[section1];

私が探していることを実行する唯一の方法は、テキストを 2 つの部分に分けて、テキストを別々に強調表示することでした。

ありがとうございます。

4

1 に答える 1