Skip to content

Instantly share code, notes, and snippets.

@hsylife
Last active December 16, 2018 12:57
Show Gist options
  • Save hsylife/86cb8e066da6466d415f6a70ad56ed26 to your computer and use it in GitHub Desktop.
Save hsylife/86cb8e066da6466d415f6a70ad56ed26 to your computer and use it in GitHub Desktop.
文字につける影の比較 ref: https://qiita.com/hsylife/items/8c04bfcec6a640b38db7
import UIKit
@IBDesignable class ShadowAttributeSettableLabel : UILabel {
@IBInspectable var shadowColorForCustom: UIColor = .clear
@IBInspectable var shadowOffsetForCustom: CGSize = .zero
@IBInspectable var shadowRadiusForCustom: CGFloat = 0.0
override func layoutSubviews() {
super.layoutSubviews()
guard let text = text, let attributed = attributedText else { return }
let mutable = NSMutableAttributedString(attributedString: attributed)
let shadow = createShadow()
let range = NSRange(location: 0, length: text.count)
mutable.addAttribute(.shadow, value: shadow, range: range)
attributedText = mutable
}
private func createShadow() -> NSShadow {
let shadow = NSShadow()
shadow.shadowColor = shadowColorForCustom
shadow.shadowOffset = shadowOffsetForCustom
shadow.shadowBlurRadius = shadowRadiusForCustom
return shadow
}
}
import UIKit
@IBDesignable class ShadowUIColorSettableLabel : UILabel {
@IBInspectable var shadowUIColor: UIColor = .clear {
didSet {
layer.shadowColor = shadowUIColor.cgColor
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment