2011-05-03 13 views
6

Tengo un botón de IU que me gustaría poner dos etiquetas en él, similar a cómo una celda tiene un texto de título y texto de detalle.iOS UIButton con etiquetas múltiples

Me gustaría que el botón tenga una fuente más grande para el texto principal, y tenga un texto de detalles más pequeño debajo de eso.

¿Esto es posible? He tratado de poner varias líneas en un botón, pero necesito tener diferentes tamaños de texto para cada línea, por lo que configurar lineBreakMode y numberOfLines del titleLabel realmente no funciona.

Respuesta

6

Aquí está el código que finalmente utilizamos. Asistencia de John Wang.

Gracias a todos por las sugerencias!

// Formats a label to add to a button. Supports multiline buttons 
// Parameters: 
// button - the button to add the label to 
// height - height of the label. usual value is 44 
// offset - the offset from the top of the button 
// labelText - the text for the label 
// color - color of the text 
// formatAsBold - YES = bold NO = normal weight 
// tagNumber - tag for the label 

- (void) formatLabelForButton: (UIButton *) button withHeight: (double) height andVerticalOffset: (double) offset andText: (NSString *) labelText withFontSize: (double) fontSize withFontColor: (UIColor *) color andBoldFont:(BOOL) formatAsBold withTag: (NSInteger) tagNumber { 

    // Get width of button 
    double buttonWidth= button.frame.size.width; 

    // Initialize buttonLabel 
    UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, offset, buttonWidth, height)]; 

    // Set font size and weight of label 
    if (formatAsBold) { 
     buttonLabel.font = [UIFont boldSystemFontOfSize:fontSize]; 
    } 
    else { 
     buttonLabel.font = [UIFont systemFontOfSize:fontSize]; 
    } 

    // set font color of label 
    buttonLabel.textColor = color; 

    // Set background color, text, tag, and font 
    buttonLabel.backgroundColor = [UIColor clearColor]; 
    buttonLabel.text = labelText; 
    buttonLabel.tag = tagNumber; 

    // Center label 
    buttonLabel.textAlignment = UITextAlignmentCenter; 

    // Add label to button 
    [button addSubview:buttonLabel]; 

    [buttonLabel autorelease]; 
} // End formatLabelForButton 
4

Un truco que recomendaría es poner un UIButton con un interior transparente encima de UILabels. He usado este truco antes y, aunque puede presentar algunos problemas en términos de mantenimiento y i18n, funciona como un encanto.

Aquí hay una muestra de 5 minutos usando la sugerencia anterior. Label behind button

Dado más tiempo, puede hacer una mejor etiqueta con esquinas redondeadas.

+0

Gracias por la sugerencia, pero esto me obligaría a crear una vista con un fondo para el botón, y me gustaría tener dos estilos de fuente diferentes en el botón idealmente. – orangemako

+0

@Orangemako cuando usa la sugerencia de @Gordon, le da a la etiqueta de fondo un color diferente. –

1

debería poder agregarle las subvistas. Como todo es una vista, todo puede tener subvistas.

Lo clasificaría en subclases y pondría las etiquetas en él dentro de la subclase, luego puede ampliar las propiedades de texto y subtexto para cambiar sus valores.

No dice que puede funcionar al 100%. Pero fuera de mi cabeza. UIView puede tener SubViews

Cuestiones relacionadas