【问题标题】:drop shadow around border of UITextView to give it 3d effect in iOS/swift在 UITextView 的边框周围放置阴影以在 iOS/swift 中为其提供 3d 效果
【发布时间】:2017-10-21 06:49:53
【问题描述】:

有没有办法在 UITextView 的边框周围放置阴影以赋予它 3D 效果? textView 有圆角。

我不想为 textView 的内容(即文本)投下阴影。仅在 textView 边框周围。

我需要在 textView 上为圆角添加 clipToBounds。

到目前为止,这是我尝试过的:

let shadowLayer = CAShapeLayer()
shadowLayer.path = UIBezierPath(roundedRect: textView.bounds, cornerRadius: 15).cgPath
shadowLayer.fillColor = UIColor.clear.cgColor
shadowLayer.shadowColor = UIColor.black.cgColor
shadowLayer.shadowPath = shadowLayer.path
shadowLayer.shadowOffset = CGSize(width: 2, height: 2)
shadowLayer.shadowOpacity = 1
shadowLayer.shadowRadius = 2
textView.layer.addSublayer(shadowLayer)

这会导致:

【问题讨论】:

  • 添加您尝试过的代码。
  • 编辑以反映我的尝试
  • 所以代码不起作用?我的意思是影子没有出现?
  • 阴影出现但覆盖整个文本视图
  • 评论 fillColor 和 textview.backGroudColor = white。并检查fillColor= white

标签: ios swift xcode uitextview shadow


【解决方案1】:

原因: Clips to bounds = trueshadow 在同一视图上不能同时工作。所以要解决这个问题,你必须遵循。

  1. 在 UIView 中添加您的 TextView(如 viewTextBG)。
  2. TextView 约束:顶部底部,前导,尾随 = 0 wrt。 viewTextBG
  3. textviewviewTextBG 指定圆角半径

    textview.cornerRadius = 10
    viewTextBG.cornerRadius = 10
    
  4. 将 clipsToBounds 给 textviewviewTextBG

    textview.clipsToBounds = True
    viewTextBG.clipsToBounds = false
    
  5. 现在给viewTextBG添加阴影。

现在一切正常,仅此而已。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 2011-07-19
    • 2021-07-15
    • 1970-01-01
    相关资源
    最近更新 更多