user2732294
user2732294

Reputation: 641

How to use text attributes without indexes

I need to display different fonts for different texts in a UILabel.

I know that we can manage different fonts with attribute text in a single label using attribute text, as follows:

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;

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.

7:00 a.m. – 7:15 a.m. Breakfast

7:30 a.m. - Meeting 12:30 p.m. Conference

I have many places like above in a bulk amount of data.

Upvotes: 0

Views: 104

Answers (2)

KAREEM MAHAMMED
KAREEM MAHAMMED

Reputation: 1695

  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; 

Try this code snippet. I hope it helps you

Upvotes: 1

mshau
mshau

Reputation: 503

Hi @user2732294 I have done using string length. Please check code below

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;

Upvotes: 0

Related Questions