0

我需要为不同的文本显示不同的字体UILabel

我知道我们可以使用属性文本在单个标签中管理具有属性文本的不同字体,如下所示:

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Hello. That is a test attributed string."];
[str addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:NSMakeRange(3,5)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(10,7)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:20.0] range:NSMakeRange(20, 10)];
agendaLabel.attributedText = str;

但是在这里我有大量的数据和很多地方我有时间,我需要在字符串中的任何地方加粗时间,而不是明确设置范围。

早上 7:00 – 早上 7:15 早餐

上午 7:30 - 会议 下午 12:30 会议

在大量数据中,我有很多像上面这样的地方。

4

2 回答 2

1
  NSError *error;
NSString *display_string = @"But here I have a large amount of data and so many places I have times, I need to bold the time where ever it is in the string, without explicitly setting the range. \n7:00 a.m. – 7:15 a.m. Breakfast. \n7:30 a.m. - Meeting 12:30 p.m. Conference \nI have many places like above in a bulk amount of data.";
NSMutableAttributedString *attri_str = [[NSMutableAttributedString alloc]initWithString:display_string];

NSRegularExpression *regex = [NSRegularExpression
                              regularExpressionWithPattern:@"([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9] (a.m.|p.m.)"
                              options:NSRegularExpressionCaseInsensitive
                              error:&error];

[regex enumerateMatchesInString:display_string options:0 range:NSMakeRange(0, [display_string length])    usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
    NSLog(@"matching case");//NSMakeRange(begin, end)
    [attri_str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue" size:30] range:[match range]];

}];

label.attributedText = attri_str; 

试试这个代码片段。希望对你有帮助

于 2013-09-30T09:01:40.883 回答
0

嗨@user2732294 我已经使用了字符串长度。请检查下面的代码

NSString *needToChangeStr = [[APP_DELEGATE.MuDict_Applied_Language valueForKey:[[NSUserDefaults standardUserDefaults] valueForKey:@"Applied_Language"]] valueForKey:@" Building security system"];

    NSString *display_string=[NSString stringWithFormat:@"Test. %@",needToChangeStr];

    NSMutableAttributedString *attri_str = [[NSMutableAttributedString alloc]initWithString:display_string];



    int begin=[display_string length]-[needToChangeStr length];
    int end=[needToChangeStr length];

    [attri_str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue" size:30] range:NSMakeRange(begin, end)];
    [attri_str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30] range:NSMakeRange(0, begin)];

//    [attri_str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, end)];

    [attri_str addAttribute:(id)kCTForegroundColorAttributeName value:[UIColor colorWithRed:35/255 green:86/255 blue:109/255 alpha:1.0] range:NSMakeRange(0, begin)];
    [attri_str addAttribute:(id)kCTForegroundColorAttributeName value:[UIColor colorWithRed:44/255 green:92/255 blue:114/255 alpha:1.0] range:NSMakeRange(begin, end)];


//    [attri_str addAttribute:(id)kCTForegroundColorAttributeName
//                      value:(id)[UIColor colorWithRed:31/255 green:68/255 blue:97/255 alpha:1.0].CGColor
//                      range:NSMakeRange(0, end)];

    Label_Center.attributedText = attri_str;
于 2013-09-30T07:09:46.783 回答