2010-03-22 5 views
18

¿Cómo puedo utilizar fuente de acceso directo en el objetivo C? Más específicamente en UITableViewCellFuente de acceso directo en el objetivo C

cell.textLabel.text = name; 
cell.detailTextLabel.text = quantity ; 
cell.XXX = ?? 
+1

puede usted seleccionar la respuesta correcta a fin de no confundir a los usuarios que llegan a encontrar una solución para golpear texto a través –

Respuesta

9

EDIT: Esta respuesta está fuera de fecha a partir de iOS 6. Por favor ver las respuestas más recientes debajo

No hay soporte nativo para las fuentes tachado o subrayado. Debe dibujar las líneas usted mismo sobre las vistas de las etiquetas.

Esto es una tontería ya que el inspector de fuentes para IB tiene opciones para establecer el texto y subrayar, pero estas se ignoran rápidamente si intentas configurarlas.

+0

¿Hay alguna parte cualquier documentación que apoyen esta tesis? (No es que lo dude, simplemente me lo estoy preguntando.) – Tim

+0

@Tim, aparte de eso NSAttributedString y la cadena de programación de cadenas atribuidas no existen en las referencias de iPhone Dev, no. La falta de documentación es lo mejor que puedo hacer. –

+1

Alguna vez traté de cambiar a "tachado" en IB, pero se ignoró como usted dijo. – wal

4
CGRect frame = sender.titleLabel.frame; 
UILabel *strikethrough = [[UILabel alloc] initWithFrame:frame]; 
strikethrough.opaque = YES; 
strikethrough.backgroundColor = [UIColor clearColor]; 
strikethrough.text = @"------------------------------------------------"; 
strikethrough.lineBreakMode = UILineBreakModeClip; 
[sender addSubview:strikethrough]; 
1

¿Ha pensado en encontrar su propia fuente de tachado y cargarla usted mismo? No es tan difícil, simplemente agregue el UIFont a su archivo Info.plist y coloque el nombre de la fuente allí. Luego puede establecer manualmente el texto en la nueva fuente de tachado.

Consulte esta publicación al cargar una fuente personalizada.

Can I embed a custom font...

+1

¿Alguna de las recomendaciones de fuentes que vienen con el tachado? – hatunike

5

1-Obtener el tamaño del texto que debe tachado

CGSize expectedLabelSize = [string sizeWithFont:cell.titleLabel.font constrainedToSize:cell.titleLabel.frame.size lineBreakMode:UILineBreakModeClip]; 

2-Crear una línea y añadirlo al texto

UIView *viewUnderline = [[UIView alloc] init]; 
viewUnderline.frame = CGRectMake(20, 12, expectedLabelSize.width, 1); 
viewUnderline.backgroundColor = [UIColor grayColor]; 
[cell addSubview:viewUnderline]; 
[viewUnderline release]; 
48

esto es Marin, el autor del capítulo de cadenas atribuidas en "iOS6 por Tutoriales".

Desde iOS6, en realidad hay un soporte nativo para un conjunto de atributos de texto diferentes, incluido el campo a través del mensaje.

Aquí está un ejemplo corto, que se puede utilizar para su etiqueta de texto celular:

NSDictionary* attributes = @{ 
    NSStrikethroughStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle] 
}; 

NSAttributedString* attrText = [[NSAttributedString alloc] initWithString:@"My Text" attributes:attributes]; 
cell.textLabel.attributedText = attrText; 

eso es todo. ¡Buena suerte!

4

aquí está cómo tachar su etiqueta. Pero recuerde, sólo funciona después de iOS 6.0

NSNumber *strikeSize = [NSNumber numberWithInt:2]; 

NSDictionary *strikeThroughAttribute = [NSDictionary dictionaryWithObject:strikeSize 
forKey:NSStrikethroughStyleAttributeName]; 

NSAttributedString* strikeThroughText = [[NSAttributedString alloc] initWithString:@"Striking through it" attributes:strikeThroughAttribute]; 

strikeThroughLabel.attributedText = strikeThroughText; 
0
UIView *strikeView = [[UIView alloc] initWithFrame:ccr(0, 0, myLabel.bounds.size.width, 1)]; 
strikeView.backgroundColor = [UIColor redColor]; 
strikeView.center = ccp(myLabel.bounds.size.width/2, myLabel.bounds.size.height/2); 
[myLabel addSubview:strikeView]; 
1

que atraviesa es posible mediante el uso NSStrikeThroughAttributesattributedText y de UILabel. Aquí está la solución en Swift

let strikeThroughAttributes = [NSStrikethroughStyleAttributeName : 1] 
    let strikeThroughString = NSAttributedString(string: "Text of Label", attributes: strikeThroughAttributes) 
    strikeThroughLabel.attributedText = strikeThroughString 
-2

Esto pegará en toda la celda. En caso de que necesite que se vea como tarea completada:

CGSize size = cell.contentView.frame.size; // you'll draw the line in whole cell. 

UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15,size.height/2,size.width - 30, 1)]; 
line.backgroundColor = [UIColor grayColor]; // set your preferred color 
[cell addSubview:line]; 

Usted puede indicar algún otro valor en CGRectMake en lugar de 15 - se compensa con X. En este caso es mejor que había entonces disminuir la anchura de valor duplicado (en mi caso es 15 * 2 = 30) para que se vea bien.

2
NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Your String here"]; 
[attributeString addAttribute:NSStrikethroughStyleAttributeName 
        value:@2 
        range:NSMakeRange(0, [attributeString length])]; 
yourLabel.attributedText = attributeString; 
+0

¡Magia pura! ¡Gracias! –

Cuestiones relacionadas