简体   繁体   English

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

[英]Set highlighted text color for UILabel using NSAttributedString.?

I am using a label on tableview cell. 我在tableview单元格上使用标签。 The cell has been assigned an attributed string, but the problem is that on selection the cell text does not turn white for attributed string. 已为单元格分配了一个属性字符串,但问题是在选择时,单元格文本对于属性字符串不会变为白色。

Is there any way to fix it..? 有没有办法解决它..?

Any help is appreciated. 任何帮助表示赞赏。

Here is a look at my code. 这是我的代码。

- (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;
}

Try to use this one 尝试使用这个

    -(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
        }

I have got the solution. 我有解决方案。

In Cell class, we can input attributed strings as below 在Cell类中,我们可以输入如下的属性字符串

- (void) formatText:(BOOL)isSelected{
    UIFont *font = [UIFont systemFontOfSize:14.0];
    UIFont *secondFont = [UIFont systemFontOfSize:10.0];

    NSMutableDictionary *firstAttributes;
    NSMutableDictionary *secondAttributes;

    NSDictionary *firstAttributeFont = @{NSFontAttributeName:font};
    NSDictionary *secondAttributeFont = @{NSFontAttributeName:secondFont};

    [firstAttributes addEntriesFromDictionary:firstAttributeFont];
    [secondAttributes addEntriesFromDictionary:secondAttributeFont];

    if (!isSelected) {
        [firstAttributes addEntriesFromDictionary:@{NSForegroundColorAttributeName:[UIColor darkGrayColor]}];
        [secondAttributes addEntriesFromDictionary:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]}];

    }
    else{
        [firstAttributes addEntriesFromDictionary:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
        [secondAttributes addEntriesFromDictionary:@{NSForegroundColorAttributeName:[UIColor colorWithWhite:1.0 alpha:4.0]}];
    }


    NSString* completeString = [NSString stringWithFormat:@"%@ %@",self.firstAttributeText,self.secondAttributeText];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]     initWithString:completeString];
    [attributedString setAttributes:firstAttributes range:[completeString rangeOfString:self.firstAttributeText]];
    [attributedString setAttributes:secondAttributes range:[completeString rangeOfString:self.secondAttributeText]];
    self.headerLabel.attributedText = attributedString;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{

    [super setSelected:selected animated:animated];
    [self formatText:selected];
    // Configure the view for the selected state

}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{

    [super setHighlighted:highlighted animated:animated];
    [self formatText:highlighted];

}

UILabel subclass solution UILabel子类解决方案

@implementation CustomLabelHighlighted {
    NSAttributedString *savedAttributedString;
}

-(void)setHighlighted:(BOOL)highlighted {
    if(!SYSTEM_VERSION_LESS_THAN(@"6.0")){
        if(highlighted){
            if(!self.attributedText){
                return;
            }

            NSMutableAttributedString *highAttributedString = [self.attributedText mutableCopy];
[highAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:(NSRange){0, [highAttributedString.string length]}];

            // important!
            [super setAttributedText:highAttributedString];
        }
        else{
            if(savedAttributedString){
                self.attributedText = savedAttributedString;
            }
        }
    } else {
        [super setHighlighted:highlighted];
    }
}

- (void)setAttributedText:(NSAttributedString *)attributedText {
    [super setAttributedText:attributedText];
    savedAttributedString = attributedText;
}

@end

You can use RTLabel class it will very usefull. 你可以使用RTLabel类非常有用。 Download it from here RTLabel 从这里下载RTLabel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我在nib中设置文本颜色并在资源中定义该颜色,则UILabel文本颜色不会以编程方式更改 - UILabel text color is not changing programmatically, if I set the text color in nib and define that color in assets initWithCoder未设置UILabel属性(UIFont和文本颜色) - initWithCoder doesn't set UILabel property (UIFont and text color) 如何使用NSAttributedString设置图像 - how to set Image using NSAttributedString 具有NSAttributedString的可单击UILabel - Clickable UILabel with NSAttributedString NSAttributedString与UILabel(ios 6):使用行高属性和IB中的属性 - NSAttributedString with UILabel (ios 6): Using line height attribute and attributes from IB 如何从超级视图中获取所有元素(如:UiLabel,UITextfield)并设置标签文本颜色和文本字段占位符颜色 - How to get all element(Like: UiLabel,UITextfield) from superview and set the label text color and textfield placeholder color 是否可以在UILabel中设置多种颜色? - is it possible to set multiple color in UILabel? 在 Swift 中动画 UILabel 文本颜色 - Animate UILabel text color in Swift 在UITableViewCell中更改UILabel的文本颜色 - Changing the text color of UILabel in UITableViewCell 突出显示iOS 7时,为UIButton设置标题颜色 - set title color for UIButton when highlighted iOS 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM