【问题标题】:Can't highlight the same text within NSString using TTTAttributedLabel无法使用 TTTAttributedLabel 突出显示 NSString 中的相同文本
【发布时间】:2014-10-19 01:49:55
【问题描述】:

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

执行我正在寻找的唯一方法是将文本分成两部分并分别突出显示文本。

提前致谢。

【问题讨论】:

    标签: ios ios7 nsstring ios8 tttattributedlabel


    【解决方案1】:

    这是因为字符串范围只返回搜索字符串的一次出现,对于多次出现,你可以试试这个。

    - (NSArray *)rangesOfString:(NSString *)searchString inString:(NSString *)str withOption:(NSStringCompareOptions)option{
        NSMutableArray *results = [NSMutableArray array];
        NSRange searchRange = NSMakeRange(0, [str length]);
        NSRange range = [str rangeOfString:searchString options:option range:searchRange];
        while (range.location != NSNotFound) {
            [results addObject:[NSValue valueWithRange:range]];
            searchRange = NSMakeRange(NSMaxRange(range), [str length] - NSMaxRange(range));
            range = [str rangeOfString:searchString options:option range:searchRange];
        }
        return results;
    }
    

    然后在字符串中找到多次出现,并将文本加粗。

    NSArray *rangeArr = [self rangesOfString:@"text" inString:mutableAttributedString.string withOption:NSCaseInsensitiveSearch];
    NSArray *rangeArr1 = [self rangesOfString:@"sample" inString:mutableAttributedString.string withOption:NSCaseInsensitiveSearch];
    
    NSMutableArray *boldArray = [NSMutableArray arrayWithArray:rangeArr];
    [boldArray addObjectsFromArray:rangeArr1];
    
    UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:14];
    CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
    if (font) {
        for (NSValue *rangeValue in boldArray) {
            NSRange boldRange = rangeValue.rangeValue;
            [mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)font range:boldRange];
        }
        CFRelease(font);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 2013-06-26
      • 2013-07-15
      相关资源
      最近更新 更多