文字阴影 - iOS、swift 3.0
Text Shadow - iOS, swift 3.0
我想把阴影尺寸调大一点,但我做不到。
到目前为止:
findAPlace.titleLabel?.layer.shadowOffset = CGSize(width: -1, height: 1)
findAPlace.titleLabel?.layer.shouldRasterize = true
findAPlace.titleLabel?.layer.shadowRadius = 1
findAPlace.titleLabel?.layer.shadowOpacity = 1
findAPlace.titleLabel?.layer.shadowColor = UIColor(red:0.07, green:0.07, blue:0.07, alpha:1.0).cgColor
如何将阴影缩放到比文本本身大?
像这样。
也许加个边框就可以了,我的文字是一个UIButton
的标题!!!我希望它是uiButton
周围的文字
你可以这样做
实际上你需要使用 setTitleShadowColor
而不是 titleLabel?.layer.shadowColor
这是完整的工作代码
let btnTemp = UIButton(type: .custom)
btnTemp.frame = CGRect(x: 50, y: 200, width: 150, height: 40)
btnTemp.setTitle("Hello", for: .normal)
btnTemp.titleLabel?.layer.shouldRasterize = true
btnTemp.titleLabel?.layer.shadowRadius = 1.0
btnTemp.titleLabel?.layer.shadowOpacity = 1.0
btnTemp.setTitleColor(UIColor.blue, for: .normal)
btnTemp.backgroundColor = UIColor.gray
btnTemp.titleLabel?.shadowOffset = CGSize(width: -1, height: 1)
btnTemp.setTitleShadowColor(UIColor(red:0.07, green:0.07, blue:0.07, alpha:1.0), for: .normal)
self.view.addSubview(btnTemp)
希望对您有所帮助
输出:
您可以按照这种方式实现轮廓文字。
您必须使用属性字符串并使用 setAttributedTitle
属性 按钮才能获得所需的结果。
代码如下:
Swift 4
let strokeTextAttributes: [NSAttributedStringKey : Any] = [
NSAttributedStringKey.strokeColor : UIColor.red,
NSAttributedStringKey.foregroundColor : UIColor.gray,
NSAttributedStringKey.strokeWidth : -2.0,
]
let attributedString = NSAttributedString(string: "text", attributes: strokeTextAttributes)
self.btnTemp.setAttributedTitle(attributedString, for: .normal)
Swift 3
let strokeTextAttributes = [
NSStrokeColorAttributeName : UIColor.red,
NSForegroundColorAttributeName : UIColor.gray,
NSStrokeWidthAttributeName : -2.0,
] as [String : Any]
let attributedString = NSAttributedString(string: "text", attributes: strokeTextAttributes)
self.btnTemp.setAttributedTitle(attributedString, for: .normal)
输出:
我想把阴影尺寸调大一点,但我做不到。
到目前为止:
findAPlace.titleLabel?.layer.shadowOffset = CGSize(width: -1, height: 1)
findAPlace.titleLabel?.layer.shouldRasterize = true
findAPlace.titleLabel?.layer.shadowRadius = 1
findAPlace.titleLabel?.layer.shadowOpacity = 1
findAPlace.titleLabel?.layer.shadowColor = UIColor(red:0.07, green:0.07, blue:0.07, alpha:1.0).cgColor
如何将阴影缩放到比文本本身大?
像这样。
也许加个边框就可以了,我的文字是一个UIButton
的标题!!!我希望它是uiButton
你可以这样做
实际上你需要使用 setTitleShadowColor
而不是 titleLabel?.layer.shadowColor
这是完整的工作代码
let btnTemp = UIButton(type: .custom)
btnTemp.frame = CGRect(x: 50, y: 200, width: 150, height: 40)
btnTemp.setTitle("Hello", for: .normal)
btnTemp.titleLabel?.layer.shouldRasterize = true
btnTemp.titleLabel?.layer.shadowRadius = 1.0
btnTemp.titleLabel?.layer.shadowOpacity = 1.0
btnTemp.setTitleColor(UIColor.blue, for: .normal)
btnTemp.backgroundColor = UIColor.gray
btnTemp.titleLabel?.shadowOffset = CGSize(width: -1, height: 1)
btnTemp.setTitleShadowColor(UIColor(red:0.07, green:0.07, blue:0.07, alpha:1.0), for: .normal)
self.view.addSubview(btnTemp)
希望对您有所帮助
输出:
您可以按照这种方式实现轮廓文字。
您必须使用属性字符串并使用 setAttributedTitle
属性 按钮才能获得所需的结果。
代码如下:
Swift 4
let strokeTextAttributes: [NSAttributedStringKey : Any] = [
NSAttributedStringKey.strokeColor : UIColor.red,
NSAttributedStringKey.foregroundColor : UIColor.gray,
NSAttributedStringKey.strokeWidth : -2.0,
]
let attributedString = NSAttributedString(string: "text", attributes: strokeTextAttributes)
self.btnTemp.setAttributedTitle(attributedString, for: .normal)
Swift 3
let strokeTextAttributes = [
NSStrokeColorAttributeName : UIColor.red,
NSForegroundColorAttributeName : UIColor.gray,
NSStrokeWidthAttributeName : -2.0,
] as [String : Any]
let attributedString = NSAttributedString(string: "text", attributes: strokeTextAttributes)
self.btnTemp.setAttributedTitle(attributedString, for: .normal)
输出: