【问题标题】:How to apply text shadow to UITextView?如何将文本阴影应用于 UITextView?
【发布时间】:2010-05-17 17:26:13
【问题描述】:

其实我喜欢UILabel。他们很甜。现在我不得不去UITextView,因为UILabel 没有将文本垂直对齐到顶部。该死。我真正需要的一件事是文字阴影。 UILabel 有。 UITextView 似乎没有。但我猜那家伙只是使用相同的底层UIKitNSString 添加?也许有人已经有了解决这个问题的办法?我会覆盖什么?

【问题讨论】:

  • 我认为你应该接受 adjwilli 的回答,因为我认为大多数人都会同意这是正确的做法。

标签: ios iphone uitextview


【解决方案1】:
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>

【讨论】:

  • 我想标记为正确的答案会起作用,但它的形式混乱且通常很差。
  • 我希望我可以进一步标记它——这是向 UITextView 添加阴影的适当方法。另一种方法非常笨拙,除了 UITextView 的非常琐碎的使用之外,根本不应该这样做。
  • 这很好用,请将其标记为替代答案:)
  • 刚刚在 adjwilli 的答案中使用了确切的代码,它完美地工作,不需要任何其他东西。谢谢你,伙计。
  • textView 的背景需要设置为 clearColor 才能正常工作
【解决方案2】:

答案是

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;

【讨论】:

    【解决方案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
    

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

    【讨论】:

      【解决方案4】:

      这个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 更新

      【讨论】:

        【解决方案5】:

        在 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];
        

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            【解决方案7】:

            swift 4、swift 4.2、swift 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
                    }
                }
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2012-01-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-02-15
              相关资源
              最近更新 更多