UITextView contentSize changes and NSLayoutManager en iOS7
El problema:UITextView
cambia silenciosamente escontentSize
en algunas situaciones
El caso más simple textView con texto grande y teclado. Solo agregue la salida UITextView y establezca- viewDidLoad
como:
- (void)viewDidLoad {
[super viewDidLoad];
// expand default "Lorem..."
_textView.text = [NSString stringWithFormat:@"1%@\n\n2%@\n\n3%@\n\n4%@\n\n5", _textView.text, _textView.text, _textView.text, _textView.text];
_textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
_textView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0);
}
Ahora, mostrar y ocultar el teclado provocará saltos de texto en algunos casos.
He encontrado la razón de saltar por subclase.UITextView
. El único método en mi subclase es:
- (void)setContentSize:(CGSize)contentSize {
NSLog(@"CS: %@", NSStringFromCGSize(contentSize));
[super setContentSize:contentSize];
}
Y se muestracontentSize
se encoge y se expande en el teclado ocultar. Algo como esto:
013-09-16 14:40:27.305 textView-bug2[11087:a0b] CS: {320, 651}
2013-09-16 14:40:27.313 textView-bug2[11087:a0b] CS: {320, 885}
2013-09-16 14:40:27.318 textView-bug2[11087:a0b] CS: {320, 902}
Parece que el comportamiento deUITextView
Se cambió mucho en iOS7. Y algunas cosas están rotas ahora.
Descubriendo más lejos he encontrado que nuevolayoutManager
propiedad de mi textView cambia también. Hay alguna información interesante en el registro ahora:
2013-09-16 14:41:59.352 textView-bug2[11115:a0b] CS: {320, 668}
<NSLayoutManager: 0x899e800>
1 containers, text backing has 2129 characters
Currently holding 2129 glyphs.
Glyph tree contents: 2129 characters, 2129 glyphs, 3 nodes, 96 node bytes, 5440 storage bytes, 5536 total bytes, 2.60 bytes per character, 2.60 bytes per glyph
Layout tree contents: 2129 characters, 2129 glyphs, 532 laid glyphs, 13 laid line fragments, 4 nodes, 128 node bytes, 1048 storage bytes, 1176 total bytes, 0.55 bytes per character, 0.55 bytes per glyph, 40.92 laid glyphs per laid line fragment, 90.46 bytes per laid line fragment
Y la siguiente línea con contentSize ={320, 885}
contieneLayout tree contents: ..., 2127 laid glyphs, 51 laid line fragments
. Por lo tanto, parece que algún tipo de reproducción automática intenta rediseñar textView en el teclado y cambia el tamaño del contenido, incluso si el diseño aún no está terminado. Y se ejecuta incluso si mi textView no es cambios entre el teclado mostrar / ocultar.
La pregunta es: ¿cómo prevenir cambios de contentSize?