ios uitextview nsattributedstring textkit

¿Cómo puedo establecer el color y la alineación del texto atribuido en una UITextView en iOS 7?



nsattributedstring textkit (1)

El formato de mis textViews funcionó bien en iOS 6, pero ya no en iOS 7. Entiendo que con Text Kit muchas cosas han cambiado. Se ha vuelto realmente bastante confuso, y espero que alguien pueda ayudar a enderezarlo un poco ayudándome con algo tan simple como esto.

A mi UITExtView estático originalmente se le asignó un valor para textAlignment propiedades textColor y textAlignment . Luego hice un NSMutableAttributedString , le NSMutableAttributedString un atributo y luego lo asigné a la propiedad de texto de attributedText de textView. La alineación y el color ya no tienen efecto en iOS 7.

¿Cómo puedo arreglar esto? Si estas propiedades no tienen efecto, ¿por qué ya no existen? Aquí está la creación de textView:

UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)]; titleView.textAlignment = NSTextAlignmentCenter; titleView.textColor = [UIColor whiteColor]; NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"]; UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60]; [title addAttribute:NSParagraphStyleAttributeName value:font range:NSMakeRange(0, title.length)]; titleView.attributedText = title; [self.view addSubview:titleView];


Curioso, las propiedades se tienen en cuenta para UILabel pero no para UITextView

¿Por qué no agrega atributos de color y alineación a la cadena atribuida de forma similar a la forma en que lo hace con la fuente?

Algo como:

NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"]; UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60]; [title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)]; //add color [title addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, title.length)]; //add alignment NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setAlignment:NSTextAlignmentCenter]; [title addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, title.length)]; titleView.attributedText = title;

Editar: asigna el texto primero, luego cambia las propiedades y así funciona.

UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)]; //create attributed string and change font NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"]; UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60]; [title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)]; //assign text first, then customize properties titleView.attributedText = title; titleView.textAlignment = NSTextAlignmentCenter; titleView.textColor = [UIColor whiteColor];