簡體   English   中英

在 UITextView 的邊框周圍放置陰影以在 iOS/swift 中賦予它 3d 效果

[英]drop shadow around border of UITextView to give it 3d effect in iOS/swift

有沒有辦法在 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)

這導致:

在此處輸入圖片說明

原因: Clips to bounds = true和同一視圖上的shadow不能同時工作。 所以要解決這個問題,你必須遵循。

  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提供陰影。

現在一切正常,僅此而已。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM