2010-05-17 37 views
41

其實我愛UILabel。他們很甜蜜。現在我不得不去UITextView,因爲UILabel沒有將文本垂直對齊。該死的。我真正需要的是一個文字陰影。 UILabel有它。 UITextView似乎沒有它。但我想那傢伙只是使用相同的底層UIKitNSString補充?也許有人已經有這個問題的解決方案?我會覆寫什麼?如何將文字陰影應用於UITextView?

+2

我認爲你應該接受adjwilli的答案,因爲它很容易螞蟻我認爲最會同意,這是應該做的正確方法。 – Lukas 2012-11-14 07:29:41

回答

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; 

而且不要忘了頂部加起來:

#import <QuartzCore/QuartzCore.h> 
+1

標記爲正確的答案將起作用,但它的混亂和通常很差的形式。 – averydev 2011-05-10 02:19:35

+2

我希望我可以進一步標記 - 這是向UITextView添加陰影的適當方式。另一種方法非常嚴格,並且除了UITextView的非常小的用途之外,其他方法都不應該這樣做。 – 2011-05-26 03:40:23

+0

奇怪的行爲:這種方法給文本本身,而不是uitextview框添加陰影。我想給文本添加陰影而不是文本。 – 2011-06-08 08:35:40

14

答案text.layer.shadowColor

增加陰影整體TextView的,也許它有效,但它不僅增加了文字的陰影。

正確的答案是:

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

這是可編輯textview的正確方式 – zszen 2017-10-30 18:38:45

0

這取決於iOS上的版本所使用。從iOS 6開始,支持一個陰影,將其設置爲NSAttributedString上的一個屬性,然後將其設置在標籤上。

8

在iOS中使用6+屬性文本

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

可悲的是它不能編輯 – zszen 2017-10-30 18:39:00

9

斯威夫特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

夫特示例使用添加陰影爲文本的屬性串方法。有關Swift中的歸因字符串的更多信息,請參見this answer。此方法(與使用layer method相反)使您可以靈活地在一定範圍的文本上設置陰影(如果需要)。

// 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 

更新了雨燕4

+0

這對我來說很好。謝謝。 – adev 2017-08-20 02:46:42