NSString sizeWithFont: constrainedToSize: regresar altura incorrecta en las pantallas de retina

StackOverflow https://stackoverflow.com/questions/4662441

  •  10-10-2019
  •  | 
  •  

Pregunta

creo que he encontrado un caso extremo de sizeWithFont: constrainedToSize: donde, en una pantalla de la retina, que a veces (que parece basado en el ajuste de texto) devuelve un alto línea de altura 1 que realmente se necesita, y lo más importante de lo que se lo que realmente atrae.

NOTA: El código real que estoy usando está enterrado dentro de la vista tabla de códigos de células de altura variable dibujado performant parte céntrica, por lo que he destilado el tema hasta tan simple un poco de código de ejemplo como sea posible. (Por favor, tome nota de esto cuando se trata de algo que no sea la respuesta a mi pregunta: -)

Esta UIView muestra llena de contenido, las medidas de ajuste del texto (envuelto), que llena rect, luego roba el texto.

En un dispositivo de retina (o simulador) la altura se devuelve 1 línea demasiado alto, pero en un dispositivo de pre-retina (o simulador) devuelve la altura correcta.

Le agradecería cualquier idea de cualquier persona puede tener, ya que es un error que me gustaría arreglar!

Muchas gracias!

-Eric

- (void)drawRect:(CGRect)rect {
 NSString * theString = @"Lorem ipsum dolor sit ameyyet, consectetur adipiscing elit. Etiam vel justo leo. Curabitur porta, elit vel.";
 UIFont * theFont = [UIFont systemFontOfSize:12];
 CGSize theConstraint = CGSizeMake(rect.size.width - 20, rect.size.height - 20);
 CGSize theResultSize = [theString sizeWithFont:theFont constrainedToSize:theConstraint];

 // dump the measurements
 NSLog(@"returned a size h = %f, w = %f", theResultSize.height, theResultSize.width);

 // fill the whole rect
 CGContextRef context = UIGraphicsGetCurrentContext();
 [[UIColor yellowColor] set];
 CGContextFillRect(context, rect);

 // fill the measured rect
 CGRect theRect = CGRectMake(10, 10, theResultSize.width, theResultSize.height);
 context = UIGraphicsGetCurrentContext();
 [[UIColor cyanColor] set];
 CGContextFillRect(context, theRect);

 // draw the text
 [[UIColor blackColor] set];
 [theString drawInRect:theRect withFont:theFont];
}

El proyecto simple conjunto está disponible aquí .

Simulador de archivo:
http: //files.droplr. com / files / 9979822 / aLDJ.Screen% 20shot% 202011-01-11% 20at% 2,012% 3A34% 3A34.png http: //files.droplr. com / files / 9979822 / YpCM.Screen% 20shot% 202011-01-11% 20at% 2,012% 3A36% 3A47.png

¿Fue útil?

Solución

Parece ser un problema con el simulador. Esto es lo que me dieron cuando me encontré con un simulador de retina en OS 4.3.2

introducir descripción de la imagen aquí

Otros consejos

A continuación se presenta el método que utilizo para encontrar la altura de la etiqueta de contenido de texto dinámico. Esto funciona bien en mi aplicación


- (float)getHeightFortheDynamicLabel:(NSString *)stringForTheLabel
{
    UITextView *aSampleTextView;
    // 30 is the minimum height
    aSampleTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, mywidth, 30)];
    aSampleTextView.text = stringForTheLabel;
    aSampleTextView.font = [UIFont systemFontOfSize:kMyFontSize];
    aSampleTextView.alpha = 0;
    [self.view addSubview:aSampleTextView];
    float textViewHeight = aSampleTextView.contentSize.height;
    [aSampleTextView removeFromSuperview];
    [aSampleTextView release];
    return  textViewHeight;
}

        NSString *strSubstring = @"asdfghjklasdfghjkl adsds";

        CGFloat maxWidth = 205.0;
        CGFloat maxHeight = 9999;
        CGSize maximumLabelSize = CGSizeMake(maxWidth,maxHeight);
        CGSize expectedLabelSize = [strSubstring sizeWithFont:[UIFont fontWithName:@"StagSans-Light" size:12] constrainedToSize:maximumLabelSize lineBreakMode:UILineBreakModeWordWrap];

        NSLog(@"returned a size h = %f, w = %f", expectedLabelSize.height, expectedLabelSize.width);

        return expectedLabelSize;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top