如何将文本阴影应用于UITextView?

don*_*ile 44 iphone uitextview ios

其实我爱UILabel.他们很甜蜜.现在我不得不去,UITextView因为UILabel没有垂直对齐文本到顶部.该死的.我真正需要的是文字阴影.UILabel有它.UITextView好像没有它.但我猜那家伙只是使用相同的底层UIKit NSString加法?也许有人已经有解决这个问题的方法?我会覆盖什么?

adj*_*lli 154

text.layer.shadowColor = [[UIColor whiteColor] CGColor];
text.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
text.layer.shadowOpacity = 1.0f;
text.layer.shadowRadius = 1.0f;
Run Code Online (Sandbox Code Playgroud)

并且不要忘记加上顶部:

#import <QuartzCore/QuartzCore.h>
Run Code Online (Sandbox Code Playgroud)

  • 我希望我能够进一步标记这一点 - 这是向UITextView添加阴影的合适方法.另一种方法非常繁重,除了非常简单的UITextView使用之外,其他任何方式都没有. (2认同)
  • 这完美地工作,请将此标记为替代答案:) (2认同)

Nik*_*ita 15

答案是

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;
Run Code Online (Sandbox Code Playgroud)


Leo*_*bus 12

斯威夫特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
Run Code Online (Sandbox Code Playgroud)

斯威夫特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
Run Code Online (Sandbox Code Playgroud)


Sur*_*gch 9

在此输入图像描述

Swift示例使用向文本添加阴影的属性字符串方法.有关Swift中属性字符串的更多信息,请参阅此答案.此方法(与使用图层方法相对)使您可以根据需要灵活地在一系列文本上设置阴影.

// 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
Run Code Online (Sandbox Code Playgroud)

针对Swift 4进行了更新


Rob*_*ert 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];
Run Code Online (Sandbox Code Playgroud)

你好的例子