Cálculo incorrecto Cuando se calcula el tamaño de NSString usando sizewithfont: restrictedTosize: linebreakmode

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

  •  25-10-2019
  •  | 
  •  

Pregunta

Tengo una visión de uable con una subvisión que consiste en un UITextView. El contenido de TextView es dinámico, por lo que quiero medir el ancho y la altura exactos para crear una celda y una vista text sin necesidad de desplazarse.

He creado el siguiente método para medir el tamaño que tomará el NSString:

- (CGSize)getSizeOfString:(NSString *)str font:(NSString *)theFont fontSize:(CGFloat)theSize {

    CGSize maxSize = CGSizeMake(280, CGFLOAT_MAX);
    UIFont *font = [UIFont fontWithName:theFont size:theSize];

    return [str sizeWithFont:font constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap];
}

Lo estoy usando de esta manera:

// CellForRowAtIndexPath
    ..... 

    CGSize size = [strTool getSizeOfString:text font:TEXT_FONT fontSize:TEXT_SIZE];
    CGFloat width = size.width;
    CGFloat height = size.height;

    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, width, height)];

    [textView setBackgroundColor:[UIColor blueColor]];
    textView.font = [UIFont fontWithName:TEXT_FONT size:TEXT_SIZE];
    textView.editable = NO;

    // Set value
    textView.text = text;

    [cell.contentView addSubview:textView];
    [textView release];

    return cell;

// heightForRowAtIndexPath
    NSString *returnStr;

    if (indexPath.section == SECTION_SHORT) 
        returnStr = self.manchet;
    else
        returnStr = self.newsText;

    return [strTool getSizeOfString:returnStr font:TEXT_FONT fontSize:TEXT_SIZE].height;

Pero no importa cuál sea la altura calculada es demasiado pequeña, lo que resulta en un desplazamiento desagradable dentro de CellView. Vale la pena mencionar que casi todos los textos contienen borros de línea ( n).

¿Qué estoy haciendo mal aquí?

Gracias

¿Fue útil?

Solución

Creo que su ** HIEMPROFROURDATINCEXTH ** debería verse algo así:

NSString *returnStr;
if (indexPath.section == SECTION_SHORT) returnStr = self.manchet;
else returnStr = self.newsText;
return [strTool getSizeOfString:returnStr font:TEXT_FONT 
                       fontSize:TEXT_SIZE].height+10.0f*2.0f;

Y también debe tener en cuenta lo que hizo compensación para el campo de texto 15px a lo largo de x (lo que significa qué ancho para el tamaño de tamaño debe ser más pequeño en 15px) y 10px desde la parte superior, sugiero que use el mismo valor para la parte inferior para que se vea centrado.

ContentView también tiene márgenes, tenga todo esto en cuenta.

Otros consejos

UITextView necesita un espacio un poco más grande que el tamaño que calculó. Como tiene un margen superior y un margen inferior, no estoy seguro de cuánto es eso. Si quieres un ajuste perfecto, intente

Nsstring dibuinrect: withfont:

O simplemente haga un poco más de espacio para UITextView, puede encontrar el espacio adicional intentando.

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