簡體   English   中英

使用NSAttributedString為UILabel設置突出顯示的文本顏色。

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

我在tableview單元格上使用標簽。 已為單元格分配了一個屬性字符串,但問題是在選擇時,單元格文本對於屬性字符串不會變為白色。

有沒有辦法解決它..?

任何幫助表示贊賞。

這是我的代碼。

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

我有解決方案。

在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子類解決方案

@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

你可以使用RTLabel類非常有用。 從這里下載RTLabel

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM