ios - 在 UITextView 的边框周围放置阴影以在 iOS/swift 中赋予它 3d 效果

标签 ios swift xcode uitextview shadow

有没有办法在 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)

这导致:

enter image description here

最佳答案

原因: Clips to bounds = trueshadow在同一个 View 上不能同时工作。因此,要解决这个问题,您必须遵循。

  1. 添加您的 TextView在 UIView 中(说 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 添加阴影.

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

关于ios - 在 UITextView 的边框周围放置阴影以在 iOS/swift 中赋予它 3d 效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46860793/

相关文章:

ios - 如何使用我的 ViewModel 绑定(bind) "UITableViewVellAccessory.CheckMark"?

html - 更改Swift NSAttributedString的无序列表的字体

swift - 在 Swift 中使用元胞自动机生成程序级别

ios - UICollectionView iOS8 的问题

iphone - 从文本文件中读取 - Objective C

ios - Swift 4+实例化符合协议(protocol)的类变量

ios - 允许文本选择但不在 WkWebView 中显示上下文菜单

iphone - 删除 View 中的图像

ios - 新的 ios 应用程序在上传后不会显示在 itunes connect 上

ios - ARC 转换失败 : -ccc-arcmt-check argument "unused"