lunes, 20 de octubre de 2014

Swift y TextView

        
Asignar atributos en un TextView

En el caso de querer modificar una porción  de texto dentro de un UITextView . 

UIFontDescriptor proporciona un mecanismo para describir un tipo de letra con  un diccionario de atributos.

let bodyFontDescriptor = UIFontDescriptor.preferredFontDescriptorWithTextStyle(UIFontTextStyleBody)
textView.font = UIFont(descriptor: bodyFontDescriptor, size: 0)


 let attributedText = NSMutableAttributedString(attributedString: textView.attributedText!)

 let text = textView.text! as NSString

        

 // El texto "Contenido" forma parte del texto del textView.text = "Contenido del componente" let textoVerde = text.rangeOfString(NSLocalizedString("Contenido", comment: ""))         // Aplicamos un estilo declarado con un fondo verde y se aplica al rango textoVerde attributedText.addAttribute(NSBackgroundColorAttributeName, value: UIColor.applicationGreenColor(), range: textoVerde)        

 // Actualizamos el attributed Text textView.attributedText = attributedText

No hay comentarios:

Publicar un comentario