2011-09-22 5 views
5

Tengo un UITableViewCell personalizado que usa un NSAttributedString. Quiero que cambie de color cuando se selecciona la celda. ¿Cómo puedo hacer que NSAttributedString tenga el mismo comportamiento que un UILabel con un conjunto de colores resaltados?Cómo configurarHighlightedTextColor @ NSAttributedString

he tratado de cambiar el color de las funciones setSelected y setHighlighted de la célula, pero parece que están llamados a finales (en vez de touchUpInside Touchdown)

Gracias de antemano!

+0

intenté utilizar smth como: cadena NSAttributedString; [string addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange (0, [string length])]; cuando se selecciona su celda? – Maggie

+0

Puedo cambiar el color del texto, pero no me doy cuenta de que la celda ha sido seleccionada hasta que touchUp – Daniel

+0

didSelectRowAtIndexPath, ¿no tuvo suerte? – Maggie

Respuesta

1

solución subclase UILabel

@implementation CustomLabelHighlighted 
{ 
    NSAttributedString *savedAttributedString; 
} 

-(void)setHighlighted:(BOOL)highlighted 
{ 
    [super setHighlighted:highlighted]; 

    if (!highlighted) 
    { 
     [super setAttributedText:savedAttributedString]; 
     return; 
    } 

    NSMutableAttributedString *highAttributedString = [savedAttributedString mutableCopy]; 
    NSRange range = NSMakeRange(0, highAttributedString.string.length); 
    [highAttributedString addAttribute:NSForegroundColorAttributeName value:self.highlightedTextColor range:range]; 
    [super setAttributedText:highAttributedString]; 
} 

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

@end 
+0

no es una solución a prueba de balas, pero es conveniente cuando necesitas manejar casos de uso muy simples – marcio

0

lo general es bastante simple para detectar la selección/resaltar y cambiar los colores en función de ella. Los métodos importantes son:

-(void)setHighlighted:animated: 
-(void)setSelected:animated: 

nota que cuando anulando usted tiene que utilizar los métodos con animated:, de lo contrario no funcionará.

Cuando desee cambiar solo el color, la solución más simple es dejar que el color se establezca en la etiqueta y no en la cadena. Tenga en cuenta que la cadena atribuida sigue heredando todas las propiedades del UILabel.

Cuestiones relacionadas