我想给标签加上圆角,也想给标签加上阴影,但是我的阴影方法和圆角半径方法不能在标签上一起使用。
这是我用来处理标签的代码
extension UILabel
{
func ShadowLabel() {
self.layer.shadowColor = UIColor.lightGray.cgColor
self.layer.shadowRadius = 2
self.layer.shadowOpacity = 1
self.layer.shadowOffset = CGSize(width: 5, height: 5)
}
}
extension UILabel
{
func RoundCornerLabel() {
self.layer.cornerRadius = frame.size.height/2
self.layer.masksToBounds = true
}
}
override func viewDidLoad() {
super.viewDidLoad()
label.shadowLabel()
label.RoundCornerLabel()
}
答案 0 :(得分:1)
您要尝试执行的操作是不可能的。阴影被应用在UIView
的边界之外,但是cornerRadius
在不遮盖bounds
的{{1}}的情况下是不可见的。因此,您将必须使用容器UIView
。
如果要以通用方式编写此代码,请编写一个UIView
扩展名,该扩展名返回一个UIView
,其中包含要为其应用阴影的视图
答案 1 :(得分:1)
确实,您 不能 确实将背景/阴影添加到fabo_autor()
如果出于任何原因您真的想将基类保留为UILabel ,那么一个干净的解决方案是:
实际上是在顶部添加另一个UILabel ,并将文本放在其中:
(在示例中,这是一个简单的固定宽度布局。)
stripe-node
然后您可以对UILabel做任何您想做的事情。
当然,您会遇到一个问题,
向UILabel添加背景和阴影并不是那么容易:/
一种解决方案:
import Stripe from 'stripe';
const stripe = new Stripe(
'sk_test_...',
{
apiVersion: '2019-12-03',
typescript: true,
}
);
因此,如果您真的要这样做,那是一种方法。
初始化的帮助程序类只是
UILabel
(请注意import UIKit
class BettaLabel: UIILabel {
override var text: String? {
set { _overLabel.text = newValue }
get { return _overLabel.text }
}
override func common() {
super.common()
addSubview(_overLabel)
_overLabel.font = font
_overLabel.textColor = textColor
_overLabel.textAlignment = textAlignment
text = ""
}
override func layoutSubviews() {
super.layoutSubviews()
_overLabel.frame = bounds
}
lazy var _overLabel: UILabel = {
let o = UILabel()
o.backgroundColor = .clear
return o
}()
}
中的多余“ I” .. I用于初始化。)
答案 2 :(得分:0)
尝试使用此代码,有时您需要在设置运行时属性之前取消刷新UI
override func viewDidLoad() {
super.viewDidLoad()
self.view.layoutIfNeeded()
label.shadowBtn()
label.RoundCornerBtn()
}
答案 3 :(得分:0)
您面临的问题很明显。如果将 masksToBounds
设置为 true
,则将所有内容裁剪到该图层之外。
阴影将绘制在图层的外部,因此也会被修剪。因此,我们不能对这两种效果都使用同一层,我的意思是阴影和角都圆了,所以最好的解决方法是@Sparr comment。