2012-05-26 17 views
7

Tengo un UILabel en un Controlador de vista de detalle, por lo que su contenido cambia según la fila de la tabla seleccionada. Tengo un problema, establecería un ancho fijo para mi UILabel y una altura dinámica dependiendo del texto. ¿Cómo puedo hacer esto? (Lo siento por mis errores, pero no soy inglés)UILabel con ancho fijo y altura flexible

Respuesta

-2

Puede hacerlo ... hay código.

UILabel *yourlabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, 100, sizeToFit)]; 
yourlabel.numberOfLines = 0; 

Para cualquier consulta, comenta.

+0

No puedo usar sizeToFit como un valor para la altura de CGRect, Xcode no me permite hacer esto .. Dice que es un 'identificador no declarado' –

+0

Gracias Albert;) –

+0

simplemente verifique esto ... enlace Creo que resolverá tu problema. http://basheerad.blogspot.com/2011/12/dynamic-width-and-height-for-uilabel-in.html – jamil

8

Me gusta la subclase UILabel para hacer esto por mí.

AutosizingLabel.h

#import <UIKit/UIKit.h> 


@interface AutosizingLabel : UILabel { 
    double minHeight; 
} 

@property (nonatomic) double minHeight; 

- (void)calculateSize; 

@end  

AutosizingLabel.m

#define MIN_HEIGHT 10.0f 

#import "AutosizingLabel.h" 

@implementation AutosizingLabel 

@synthesize minHeight; 

- (id)init { 
    if ([super init]) { 
     self.minHeight = MIN_HEIGHT; 
    } 

    return self; 
} 

- (void)calculateSize { 
    CGSize constraint = CGSizeMake(self.frame.size.width, 20000.0f); 
    CGSize size = [self.text sizeWithFont:self.font constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];  

    [self setLineBreakMode:UILineBreakModeWordWrap]; 
    [self setAdjustsFontSizeToFitWidth:NO]; 
    [self setNumberOfLines:0]; 
    [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, MAX(size.height, MIN_HEIGHT))]; 

} 

- (void)setText:(NSString *)text { 
    [super setText:text]; 

    [self calculateSize]; 
} 

- (void)setFont:(UIFont *)font { 
    [super setFont:font]; 

    [self calculateSize]; 
} 

@end 

Para utilizar esta, importación/crear el .hy archivos .m en su proyecto. Entonces, si va a crear su UILabel en el código, se vería algo como esto:

#import "AutosizingLabel.h" 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    AutosizingLabel *label = [[AutosizingLabel alloc] init]; 
    label.text = @"Some text here"; 
    [self.view addSubview:label]; 
} 

Si estás utilizando un XI ter, se puede seleccionar cualquier UILabel y hacer clic en el Inspector de identidad en la barra lateral derecha para establecer su clase a AutosizingLabel. En cualquier caso, establecer la propiedad .text actualizará automáticamente el tamaño de la etiqueta.

+0

Gracias. Y entonces, ¿cómo puedo usar esta subclase en el Controlador de Vista de Detalle para tener una altura dinámica para mi UILabel? –

+0

Actualicé mi respuesta con más información sobre cómo usarla. –

+0

Muchas gracias, voy a intentarlo ahora. –

Cuestiones relacionadas