2013-09-30 4 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시에서 오전 7시 15분까지 조식

오전 7시 반 - 회의 오후 12시 반 컨퍼런스

저는 대량 데이터에 위와 같이 많은 장소가 있습니다.

답변

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; 
관련 문제