Esto funciona para mí. Cambiar el contenido de TextView Inserte y encuadre el recuadro/posición para obtener el acolchado y el ajuste de palabras correctos. Luego, cambie los límites de textView para evitar el desplazamiento horizontal. También necesita restablecer el relleno cada vez que se selecciona y cambia el texto.
- (void)setPadding
{
UIEdgeInsets padding = UIEdgeInsetsMake(30, 20, 15, 50);
textView.contentInset = padding;
CGRect frame = textView.frame;
// must change frame before bounds because the text wrap is reformatted based on frame, don't include the top and bottom insets
CGRect insetFrame = UIEdgeInsetsInsetRect(frame, UIEdgeInsetsMake(0, padding.left, 0, padding.right));
// offset frame back to original x
CGFloat offsetX = frame.origin.x - (insetFrame.origin.x - (padding.left + padding.right)/2);
insetFrame = CGRectApplyAffineTransform(insetFrame, CGAffineTransformMakeTranslation(offsetX, 0));
textView.frame = insetFrame;
textView.bounds = UIEdgeInsetsInsetRect(textView.bounds, UIEdgeInsetsMake(0, -padding.left, 0, -padding.right));
[textView scrollRectToVisible:CGRectMake(0,0,1,1) animated:NO];
}
-(void)textViewDidChangeSelection:(UITextView *)textView
{
[self setPadding];
}
-(void)textViewDidChange:(UITextView *)textView
{
[self setPadding];
}
Sí, eso es lo que estoy haciendo ahora. Funciona bien para los márgenes laterales, pero el margen superior tiene problemas. Desde la perspectiva del usuario, el texto se "corta" en la parte superior mientras se desplaza porque el texto solo se representa en UITextView. – strawtarget
Y no se puede mostrar correctamente la barra de desplazamiento vertical de esta manera. – an0
Configuración 'textView.clipsToBounds = NO'" arreglará "el corte en la parte superior, más o menos. Por supuesto, esto no ayudará a mostrar correctamente la barra de desplazamiento vertical. – villapossu