如何在搜索时突出显示 UILabel 格式的 phone 数字
How to highlight UILabel formatted phone number while searching
如何在搜索时用格式化的 phone 数字突出显示 UILabel
?
示例:
一个单元格正在显示联系人列表,其号码如下:+381 60 0000000
如果我键入 3,我希望突出显示 + 和 3(颜色不同)
如果我输入 38160,我希望突出显示 +381 60(颜色不同)。
等等...
本题的重点是如何通过跳过空格和+号来为匹配的数字着色?
这是我迄今为止尝试过的方法,但只有当我从标签的文本值中删除空格和 + 号时它才有效:
// Highlighted search for numbers
NSString *phoneString = @"+381 60 0000000";
NSString *searchString = @"38160";
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:phoneString];
NSRange selectedRange = [[string string] rangeOfString:searchString options:NSRegularExpressionSearch];
if (selectedRange.location != NSNotFound) {
[string beginEditing];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:selectedRange];
[string endEditing];
}
cell.phoneNumberLabel.attributedText = string;
感谢@Larme,我已经回答了我自己的问题:)
// Highlighted search for numbers
NSString *phoneString = @"+381 60 000343";
NSString *firstComponent = @"\+?"; // Skip a leading + char
NSMutableArray *digits = [[NSMutableArray alloc] init];
[self.searchString enumerateSubstringsInRange:NSMakeRange(0, [self.searchString length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
[digits addObject:substring];
}];
NSString *pattern = [[NSString alloc] initWithFormat:@"%@%@", firstComponent, [digits componentsJoinedByString:@"\s?"]];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:phoneString];
NSRange selectedRange = [[string string] rangeOfString:pattern options:NSRegularExpressionSearch];
if (selectedRange.location != NSNotFound) {
[string beginEditing];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:selectedRange];
[string endEditing];
}
cell.phoneNumberLabel.attributedText = string;
如何在搜索时用格式化的 phone 数字突出显示 UILabel
?
示例:
一个单元格正在显示联系人列表,其号码如下:+381 60 0000000
如果我键入 3,我希望突出显示 + 和 3(颜色不同)
如果我输入 38160,我希望突出显示 +381 60(颜色不同)。
等等...
本题的重点是如何通过跳过空格和+号来为匹配的数字着色?
这是我迄今为止尝试过的方法,但只有当我从标签的文本值中删除空格和 + 号时它才有效:
// Highlighted search for numbers
NSString *phoneString = @"+381 60 0000000";
NSString *searchString = @"38160";
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:phoneString];
NSRange selectedRange = [[string string] rangeOfString:searchString options:NSRegularExpressionSearch];
if (selectedRange.location != NSNotFound) {
[string beginEditing];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:selectedRange];
[string endEditing];
}
cell.phoneNumberLabel.attributedText = string;
感谢@Larme,我已经回答了我自己的问题:)
// Highlighted search for numbers
NSString *phoneString = @"+381 60 000343";
NSString *firstComponent = @"\+?"; // Skip a leading + char
NSMutableArray *digits = [[NSMutableArray alloc] init];
[self.searchString enumerateSubstringsInRange:NSMakeRange(0, [self.searchString length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
[digits addObject:substring];
}];
NSString *pattern = [[NSString alloc] initWithFormat:@"%@%@", firstComponent, [digits componentsJoinedByString:@"\s?"]];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:phoneString];
NSRange selectedRange = [[string string] rangeOfString:pattern options:NSRegularExpressionSearch];
if (selectedRange.location != NSNotFound) {
[string beginEditing];
[string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:selectedRange];
[string endEditing];
}
cell.phoneNumberLabel.attributedText = string;