NSMutableattributedstring不适用于子字符串

时间:2014-12-02 05:59:20

标签: ios nsmutableattributedstring

-(NSMutableAttributedString *)getAttributedStringforString:(NSString *)totalString filtertext:(NSString *)filtertext font:(UIFont *)titlefont backgroundColor:(UIColor *)bcolor foregroundColor:(UIColor *)fcolor
{
    NSMutableAttributedString *attributedString = nil;
    NSDictionary *filterTextAttribute = @{NSBackgroundColorAttributeName:bcolor, NSForegroundColorAttributeName:fcolor, NSFontAttributeName: titlefont};
    attributedString = [[NSMutableAttributedString alloc] initWithString:totalString];
    [attributedString beginEditing];
    [attributedString setAttributes:filterTextAttribute range:[totalString rangeOfString:filtertext options:NSCaseInsensitiveSearch]];
    [attributedString endEditing];

    return attributedString;
}

如果filtertext是totalstring,那么我在totalstring中对filtertext应用NSMutableAttributedString,那么上面的setattributes正在工作,否则它不适用于子字符串

0 个答案:

没有答案