How to apply text shadow to UITextView?

前端 未结 7 1056
醉梦人生
醉梦人生 2021-02-06 21:12

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. O

相关标签:
7条回答
  • 2021-02-06 21:31

    In iOS 6+ use attributed text

    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 讨论(0)
  • 2021-02-06 21:35
    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>
    
    0 讨论(0)
  • 2021-02-06 21:42

    swift 4, swift 4.2, swift 5 and above Simple and elegant solution , can use from interface builder easily

    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
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-06 21:46

    This Swift example uses the attributed string method of adding shadow to text. See this answer for 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.

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

    0 讨论(0)
  • 2021-02-06 21:48

    The answer with

    text.layer.shadowColor

    adds shadow to whole textView, perhaps it works, but it adds shadow not only to text.

    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;
    
    0 讨论(0)
  • 2021-02-06 21:52

    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.

    0 讨论(0)
提交回复
热议问题