【问题标题】:Adding shadow to UITextView makes text expand out of UITextViewFrame向 UITextView 添加阴影会使文本从 UITextViewFrame 扩展
【发布时间】:2017-10-19 13:54:38
【问题描述】:

我有一个 UITextView,我想给它添加阴影。我使用的代码是:

textview.layer.shadowColor = UIColor.lightGray.cgColor
textview.layer.shadowOffset = CGSize(width:0,height: 2.0)
textview.layer.shadowRadius = 2.0
textview.layer.shadowOpacity = 1.0
textview.layer.masksToBounds = false
textview.layer.cornerRadius = 5

此代码添加了阴影并使角变圆,但它也使 UITextView 内的文本超出了 textview 框架。 我尝试添加

textview.clipToBounds = true

这会将文本设置在内部,但会移除阴影。我也试过设置:

textview.layer.maskToBounds = true
textview.clipToBounds = false

但问题重演。 我在这里读到了我应该设置的地方

textview.layer.shouldRasterize = true

它什么都不做,就像将它设置为 false 什么都不做一样。 因此,如果有人知道什么会起作用,请帮助我,因为我被卡住了。

【问题讨论】:

    标签: ios swift xcode uitextview shadow


    【解决方案1】:

    不幸的是,如果您使用textview.clipsToBounds = true,您的阴影将不可见。

    我建议您设置textview.clipsToBounds = true 并在textview 下方插入另一个UIView,它与textview 具有相同的框架,并在UIView 上绘制阴影和圆角半径。

    来自UITextView 的文本不会溢出它的边界,并且您将看到阴影和圆角半径...

    【讨论】:

    • 据我在线阅读,这可能是这种情况的最佳解决方案。
    猜你喜欢
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    相关资源
    最近更新 更多