2013-09-30 100 views
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會議

我有很多地方像大量的數據上面。

回答

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; 

代碼試試這個代碼段。 我希望它可以幫助你

+0

哇,它的工作原理,謝謝 – user2732294

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;