gpt4 book ai didi

iphone - 使用 NSAttributedString 为 UILabel 设置突出显示的文本颜色。?

转载 作者:可可西里 更新时间:2023-11-01 04:26:18 24 4
gpt4 key购买 nike

我在表格 View 单元格上使用标签。已为单元格分配了一个属性字符串,但问题是在选择时单元格文本不会为属性字符串变成白色。

有什么办法可以解决吗?

感谢任何帮助。

这是我的代码。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

LHSearchViewCell *cell = [LHSearchViewCell cellForTableView:tableView fromNib:_cellNib];

cell.selectionStyle = UITableViewCellSelectionStyleGray;

cell.headerLabel.textColor = [UIColor darkGrayColor];
cell.headerLabel.highlightedTextColor = [UIColor whiteColor];
cell.headerLabel.font = [UIFont systemFontOfSize:14];

cell.subLabel.font = [UIFont systemFontOfSize:12];
cell.subLabel.textColor = [UIColor darkGrayColor];
cell.subLabel.numberOfLines = 2;
switch (indexPath.section) {
case 0:
{
UIFont *font = [UIFont systemFontOfSize:14.0];
NSDictionary *firstAttributes = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,[UIColor darkGrayColor],NSForegroundColorAttributeName,nil];
UIFont *secondFont = [UIFont systemFontOfSize:10.0];
NSDictionary *secondAttributes = [NSDictionary dictionaryWithObjectsAndKeys:secondFont,NSFontAttributeName,[UIColor lightGrayColor],NSForegroundColorAttributeName,nil];
NSString* completeString = [NSString stringWithFormat:@"%@ | %@",[_libraryPdfArray objectAtIndex:indexPath.row],[_libraryPdfDateArray objectAtIndex:indexPath.row]];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:completeString];
[attributedString setAttributes:firstAttributes range:[completeString rangeOfString:[_libraryPdfArray objectAtIndex:indexPath.row]]];
[attributedString setAttributes:secondAttributes range:[completeString rangeOfString:[_libraryPdfDateArray objectAtIndex:indexPath.row]]];
cell.headerLabel.attributedText = attributedString;

cell.subLabel.text = [_libraryPdfSubtitleArray objectAtIndex:indexPath.row];

}
break;

case 1:
{
UIFont *font = [UIFont systemFontOfSize:14.0];
NSDictionary *firstAttributes = [NSDictionary dictionaryWithObjectsAndKeys:font,NSFontAttributeName,[UIColor darkGrayColor],NSForegroundColorAttributeName,nil];
NSString* completeString = [NSString stringWithFormat:@"%@ | %@",[_ebriefingPdfArray objectAtIndex:indexPath.row],[_ebriefingSecondLabelTextArray objectAtIndex:indexPath.row]];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:completeString];
[attributedString setAttributes:firstAttributes range:[completeString rangeOfString:[_ebriefingPdfArray objectAtIndex:indexPath.row]]];
[attributedString setAttributes:firstAttributes range:[completeString rangeOfString:[_ebriefingSubtitleArray objectAtIndex:indexPath.row]]];
cell.headerLabel.attributedText = attributedString;
cell.subLabel.text = [_ebriefingSubtitleArray objectAtIndex:indexPath.row];
}
break;

default:
break;
}

return cell;
}

最佳答案

尝试使用这个

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

UILabel *label = (UILabel *)[cell viewWithTag:yourTag];

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:label.text];

[str addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(10,10)];

label.attributedText = str
}

关于iphone - 使用 NSAttributedString 为 UILabel 设置突出显示的文本颜色。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17361786/

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