ios 如何将文本阴影应用于 UITextView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2851248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to apply text shadow to UITextView?
提问by dontWatchMyProfile
Actually I love UILabel
. They're sweet. Now I had to go to UITextView
because UILabel
is not aligning text vertically to the top. Damn. One thing I really need is a text shadow. UILabel
has it. UITextView
seems to not have it. But I guess that guy just uses the same underlying UIKit
NSString
additions?? Maybe someone already has a solution for that problem? What would I overwrite?
其实我爱UILabel
。他们是甜的。现在我不得不去,UITextView
因为UILabel
没有将文本垂直对齐到顶部。该死。我真正需要的一件事是文本阴影。UILabel
有。UITextView
好像没有。但我猜那家伙只是使用了相同的底层UIKit
NSString
添加?也许有人已经有了解决这个问题的办法?我会覆盖什么?
回答by adjwilli
text.layer.shadowColor = [[UIColor whiteColor] CGColor];
text.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
text.layer.shadowOpacity = 1.0f;
text.layer.shadowRadius = 1.0f;
And don't forget to add up top:
并且不要忘记加起来:
#import <QuartzCore/QuartzCore.h>
回答by Nikita
The answer with
答案与
text.layer.shadowColor
text.layer.shadowColor
adds shadow to whole textView, perhaps it works, but it adds shadow not only to text.
为整个 textView 添加阴影,也许它有效,但它不仅为文本添加阴影。
The correct answer is:
正确答案是:
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;
回答by Leo Dabus
Swift 3
斯威夫特 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
斯威夫特 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
回答by Suragch
This Swift exampleuses the attributed string method of adding shadow to text. See this answerfor more about attributed strings in Swift. This method (as opposed to using the layer method) gives you the flexibility to set the shadow on a range of text if you want to.
这个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
Updated for Swift 4
为 Swift 4 更新
回答by Robert
In iOS 6+ use attributed text
在 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];
回答by Cocoanetics
It depends on the version of iOS you are using. Beginning with iOS 6 there is a single shadow supported, you set that as an attribute on an NSAttributedString that you then set onthe label.
这取决于您使用的 iOS 版本。从 iOS 6 开始,支持单个阴影,您将其设置为 NSAttributedString 上的属性,然后在标签上设置。
回答by midhun p
swift 4, swift 4.2, swift 5 and above Simple and elegant solution , can use from interface builder easily
swift 4, swift 4.2, swift 5 and above 简单优雅的解决方案,可以从界面构建器轻松使用
extension UIView {
/* The color of the shadow. Defaults to opaque black. Colors created
* from patterns are currently NOT supported. Animatable. */
@IBInspectable var shadowColor: UIColor? {
set {
layer.shadowColor = newValue!.cgColor
}
get {
if let color = layer.shadowColor {
return UIColor(cgColor: color)
}
else {
return nil
}
}
}
/* The opacity of the shadow. Defaults to 0.4 Specifying a value outside the
* [0,1] range will give undefined results. Animatable. */
@IBInspectable var shadowOpacity: Float {
set {
layer.shadowOpacity = newValue
}
get {
return layer.shadowOpacity
}
}
/* The shadow offset. Defaults to (1, 2). Animatable. */
@IBInspectable var shadowOffset: CGPoint {
set {
layer.shadowOffset = CGSize(width: newValue.x, height: newValue.y)
}
get {
return CGPoint(x: layer.shadowOffset.width, y:layer.shadowOffset.height)
}
}
/* The blur radius used to create the shadow. Defaults to 3. Animatable. */
@IBInspectable var shadowRadius: CGFloat {
set {
layer.shadowRadius = newValue
}
get {
return layer.shadowRadius
}
}
}