2010-05-17 27 views
41

En realidad me encanta UILabel. Ellos son dulces Ahora tenía que ir al UITextView porque UILabel no alineaba el texto verticalmente en la parte superior. Maldita sea. Una cosa que realmente necesito es una sombra de texto. UILabel lo tiene. UITextView parece no tenerlo. Pero supongo que ese tipo solo usa las mismas adiciones subyacentes UIKitNSString ?? Tal vez alguien ya tiene una solución para ese problema? ¿Qué iba a sobrescribir?Cómo aplicar sombra de texto a UITextView?

+2

Creo que debería aceptar la respuesta adjwilli, porque es fácil de hormigas creo que la mayoría estará de acuerdo en que es la forma correcta de hacerlo. – Lukas

Respuesta

152
text.layer.shadowColor = [[UIColor whiteColor] CGColor]; 
text.layer.shadowOffset = CGSizeMake(1.0f, 1.0f); 
text.layer.shadowOpacity = 1.0f; 
text.layer.shadowRadius = 1.0f; 

Y no se olvide de añadir hasta la parte superior:

#import <QuartzCore/QuartzCore.h> 
+1

La respuesta marcada como correcta funcionará, supongo, pero su forma desordenada y generalmente pobre. – averydev

+2

Ojalá pudiera marcar esto más: esta es la forma adecuada de agregar una sombra a una UITextView. El otro método es muy pesado y no del todo como debería hacerse para otra cosa que no sean usos muy triviales de UITextView. –

+0

comportamiento extraño: este enfoque agrega sombra al texto mismo, no a la casilla uitextview. Me gustaría agregar sombra al cuadro, no al texto. –

14

La respuesta con

text.layer.shadowColor

agrega sombra a Textview conjunto, quizás funcione, pero agrega sombra no solo al texto.

La respuesta correcta es:

CALayer *textLayer = ((CALayer *)[textView.layer.sublayers objectAtIndex:0]); 
textLayer.shadowColor = [UIColor whiteColor].CGColor; 
textLayer.shadowOffset = CGSizeMake(0.0f, 1.0f); 
textLayer.shadowOpacity = 1.0f; 
textLayer.shadowRadius = 1.0f; 
+0

Es la forma correcta con textview editable – zszen

0

Depende de la versión de iOS que está utilizando. A partir de iOS 6 hay una sola sombra compatible, la configura como un atributo en un NSAttributedString que luego configura en la etiqueta.

8

En iOS 6+ uso atribuido texto

NSShadow * shadow = [[NSShadow alloc] init]; 
shadow.shadowColor = [UIColor blackColor]; 
shadow.shadowOffset = CGSizeMake(2, 2); 

NSDictionary * textAttributes = 
@{ NSForegroundColorAttributeName : [UIColor blueColor], 
    NSShadowAttributeName   : shadow, 
    NSFontAttributeName   : [UIFont boldSystemFontOfSize:20] }; 

textView.attributedText = [[NSAttributedString alloc] initWithString:@"Hello" 
                  attributes:textAttributes]; 

Hello example

+0

Lamentablemente no se puede editar – zszen

9

Swift 3


let textView = UITextView(frame: view.frame) 
textView.font = UIFont(name: "Helvetica", size: 64.0) 
textView.textColor = .red 
textView.text = "Hello World" 

textView.layer.shadowColor = UIColor.black.cgColor 
textView.layer.shadowOffset = CGSize(width: 2.0, height: 2.0) 
textView.layer.shadowOpacity = 1.0 
textView.layer.shadowRadius = 2.0 
textView.layer.backgroundColor = UIColor.clear.cgColor 

Swift 2,3


let textView = UITextView(frame: view.frame) 
textView.font = UIFont(name: "Helvetica", size: 64.0) 
textView.textColor = UIColor.redColor() 
textView.text = "Hello World"  

textView.layer.shadowColor = UIColor.blackColor().CGColor 
textView.layer.shadowOffset = CGSize(width: 2.0, height: 2.0) 
textView.layer.shadowOpacity = 1.0 
textView.layer.shadowRadius = 2.0 
textView.layer.backgroundColor = UIColor.clearColor().CGColor 
9

enter image description here

Este ejemplo Swift utiliza el método de cadena atribuido de añadir sombra al texto. Consulte this answer para obtener más información sobre las cadenas atribuidas en Swift. Este método (en lugar de usar layer method) le brinda la flexibilidad de establecer la sombra en un rango de texto si lo desea.

// Create a string 
let myString = "Shadow" 

// Create a shadow 
let myShadow = NSShadow() 
myShadow.shadowBlurRadius = 3 
myShadow.shadowOffset = CGSize(width: 3, height: 3) 
myShadow.shadowColor = UIColor.gray 

// Create an attribute from the shadow 
let myAttribute = [ NSAttributedStringKey.shadow: myShadow ] 

// Add the attribute to the string 
let myAttrString = NSAttributedString(string: myString, attributes: myAttribute) 

// set the attributed text on a label 
myLabel.attributedText = myAttrString // can also use with UITextView 

Actualizado para la Swift 4

+0

Esto funcionó muy bien para mí. Gracias. – adev

Cuestiones relacionadas