UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 300)]; lable.text = @"其中俩字是红色,大小为17"; NSRange rangeRmb=[lable.text rangeOfString:[NSString stringWithFormat:@"红色"]]; NSMutableAttributedString *rmbStr=[[NSMutableAttributedString alloc] initWithString:lable.text attributes:nil]; if ([[[UIDevice currentDevice] systemVersion] floatValue]>=6.0) { NSDictionary *fontDic=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],NSForegroundColorAttributeName,[UIFont systemFontOfSize:17],NSFontAttributeName, nil]; [rmbStr addAttributes:fontDic range:rangeRmb]; lable.attributedText = rmbStr; }else{ lable.text=rmbStr.string; }
时间: 2024-10-12 12:26:30