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

时间:2010-05-17 17:26:14

标签: ios iphone uitextview

其实我喜欢UILabel。他们很甜蜜。现在我必须转到UITextView,因为UILabel没有将文字垂直对齐到顶部。该死的。我真正需要的是文字阴影。 UILabel拥有它。 UITextView似乎没有它。但我猜那家伙只是使用相同的基础UIKit NSString添加?也许有人已经有解决这个问题的方法?我会覆盖什么?

7 个答案:

答案 0 :(得分:153)

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 :(得分: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;

答案 2 :(得分:11)

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

答案 3 :(得分:9)

enter image description here

Swift示例使用向文本添加阴影的属性字符串方法。有关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

针对Swift 4进行了更新

答案 4 :(得分: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

答案 5 :(得分:0)

这取决于您使用的iOS版本。从iOS 6开始,支持单个阴影,您可以将其设置为NSAttributedString上的属性,然后在标签上设置该属性。

答案 6 :(得分:0)

迅捷4,迅捷4.2,迅捷5及以上 简单优雅的解决方案,可以轻松地从界面生成器中使用

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