gpt4 book ai didi

ios - 新手需要帮助 : Bold Font & Text Colour Based on Value

转载 作者:行者123 更新时间:2023-11-28 20:08:42 25 4
gpt4 key购买 nike

我想在下方将 sl.asl 设为粗体,并根据下方的数值为其着色。有什么想法吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateStyle:NSDateFormatterShortStyle];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}


// Configure the cell...
ServiceLevel *sl = (ServiceLevel *)[self.importedRows objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", sl.name , sl.asl];

return cell;
}

最佳答案

NSAttributedString 应该可以解决这个问题。

喜欢:

    NSString *name = @"SampleName";
NSUInteger asl = 2;
NSString *t_string = [NSString stringWithFormat:@"%@ - %d", name , asl];

NSUInteger t_nameLength = name.length;
NSUInteger t_aslLength = 1;

UIColor *t_color = nil;
switch (asl) {
case 1:
t_color = [UIColor redColor];
break;
case 2:
t_color = [UIColor greenColor];
break;
default:
t_color = [UIColor blackColor];
break;
}

NSMutableAttributedString *t_attributedString = [[NSMutableAttributedString alloc] initWithString:t_string];
[t_attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:14.0] range:NSMakeRange(0, t_nameLength)];
[t_attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, t_nameLength)];
[t_attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldMT" size:14.0] range:NSMakeRange(t_nameLength + 3, t_aslLength)];
[t_attributedString addAttribute:NSForegroundColorAttributeName value:t_color range:NSMakeRange(t_nameLength + 3, t_aslLength)];

cell.textLabel.attributedText = t_attributedString;

关于ios - 新手需要帮助 : Bold Font & Text Colour Based on Value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21123097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com