2013-06-28 35 views
5

Tôi đang sử dụng nhãn trên ô xem bảng. Ô đã được gán một chuỗi được phân bổ, nhưng vấn đề là khi chọn văn bản ô không chuyển sang màu trắng cho chuỗi được phân bổ.Đặt màu văn bản được tô sáng cho UILabel bằng NSAttributedString.?

Có cách nào để khắc phục sự cố không ..?

Mọi trợ giúp đều được đánh giá cao.

Đây là mã của tôi.

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

Vui lòng thêm một bình luận cho lý do tại sao bạn bình chọn nó xuống. –

+0

Đoán của tôi là bạn sẽ không nhận được phiếu bầu âm nếu bạn cho chúng tôi thấy những gì bạn đã làm cho đến thời điểm này. –

+0

@AnkitSrivastava xem câu trả lời của tôi từ liên kết này http://stackoverflow.com/questions/14068962/how-to-use-multiple-font-stylings-on-a-single-string-inside-a-label/14068992#14068992 :) –

Trả lời

2

tôi đã có giải pháp.

Trong lớp di động, chúng ta có thể nhập vào chuỗi gán như sau

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

} 
5

Cố gắng sử dụng này một

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

Đây là giải pháp thay thế với các tùy chọn trên willSelect và willDeselect cell, nhưng tôi đang cố gắng tìm ra một số giải pháp tốt hơn bằng cách tránh sử dụng các thay đổi văn bản trong bộ điều khiển thay vì chỉ trong lớp ô. –

+1

Bạn nên làm "label.attributedText = str" và không đơn giản là "label.text = str" – superpuccio

+0

@superpuccio Cảm ơn đã thay đổi .... –

-1

Bạn có thể sử dụng lớp RTLabel nó sẽ rất hữu ích. Tải về nó từ đây RTLabel

+0

Có, hãy giải quyết vấn đề này với thư viện của bên thứ 3 ... – TheCodingArt

2

giải pháp UILabel lớp con

@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 
Các vấn đề liên quan