Last active
February 14, 2019 08:10
-
-
Save macneko-ayu/1d175deb3fa961bb6d4bbabafe1e3566 to your computer and use it in GitHub Desktop.
UILabelの文字列に線をつけて袋文字にするextension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Please, paste the code in playground and execute it. | |
import UIKit | |
extension UILabel{ | |
/// makeOutLine | |
/// | |
/// - Parameters: | |
/// - strokeWidth: 線の太さ。負数 | |
/// - oulineColor: 線の色 | |
/// - foregroundColor: 縁取りの中の色 | |
func makeOutLine(strokeWidth: CGFloat, oulineColor: UIColor, foregroundColor: UIColor) { | |
let strokeTextAttributes = [ | |
.strokeColor : oulineColor, | |
.foregroundColor : foregroundColor, | |
.strokeWidth : strokeWidth, | |
.font : self.font | |
] as [NSAttributedString.Key : Any] | |
self.attributedText = NSMutableAttributedString(string: self.text ?? "", attributes: strokeTextAttributes) | |
} | |
} | |
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 500, height: 60)) | |
label.font = UIFont.boldSystemFont(ofSize: 50) | |
label.text = "我が家の猫は7.4kg" | |
label.makeOutLine(strokeWidth: -2.0, oulineColor: .white, foregroundColor: .blue) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UILabel{ | |
/// makeOutLine | |
/// | |
/// - Parameters: | |
/// - strokeWidth: 線の太さ。負数 | |
/// - oulineColor: 線の色 | |
/// - foregroundColor: 縁取りの中の色 | |
func makeOutLine(strokeWidth: CGFloat, oulineColor: UIColor, foregroundColor: UIColor) { | |
let strokeTextAttributes = [ | |
.strokeColor : oulineColor, | |
.foregroundColor : foregroundColor, | |
.strokeWidth : strokeWidth, | |
.font : self.font | |
] as [NSAttributedString.Key : Any] | |
self.attributedText = NSMutableAttributedString(string: self.text ?? "", attributes: strokeTextAttributes) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment