Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何使用没有索引的文本属性_Objective C_Uilabel_Nsattributedstring - Fatal编程技术网

Objective c 如何使用没有索引的文本属性

Objective c 如何使用没有索引的文本属性,objective-c,uilabel,nsattributedstring,Objective C,Uilabel,Nsattributedstring,我需要在UILabel中为不同的文本显示不同的字体 我知道我们可以使用属性文本在单个标签中管理属性文本的不同字体,如下所示: NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Hello. That is a test attributed string."]; [str addAttribute:NSBackgroundColorAttributeName

我需要在
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会议室


在大量数据中,我有许多类似上面的地方。

Hi@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;
请尝试此代码段。 我希望它能帮助你

  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;