Skip to content

Instantly share code, notes, and snippets.

@gregoryvit
Created September 6, 2017 08:42
Show Gist options
  • Save gregoryvit/4dc7792a086db86888eac9c5cd601d10 to your computer and use it in GitHub Desktop.
Save gregoryvit/4dc7792a086db86888eac9c5cd601d10 to your computer and use it in GitHub Desktop.
Pixel perfect Zeplin texts
struct TextStyle {
let fontSize: CGFloat
let weight: CGFloat
let lineHeight: CGFloat
let textColor: UIColor
}
extension String {
func attributedString(with style: TextStyle) -> NSAttributedString {
let font = UIFont.systemFont(ofSize: style.fontSize, weight: style.weight)
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
paragraph.lineSpacing = 0.0
paragraph.maximumLineHeight = style.lineHeight
paragraph.minimumLineHeight = style.lineHeight
let attributes: [String : Any] = [
NSParagraphStyleAttributeName: paragraph,
NSForegroundColorAttributeName: style.textColor,
NSKernAttributeName: 0.15,
NSFontAttributeName: font
]
return NSAttributedString(string: self, attributes: attributes)
}
}
let view = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 480.0, height: 480.0))
view.backgroundColor = UIColor.white
// Body text
let bodyStyle = TextStyle(
fontSize: 16.0,
weight: UIFontWeightRegular,
lineHeight: 24.0,
textColor: UIColor(red: 0.0, green: 0.0, blue: 0.0, alpha: 0.87)
)
let text = "На электронную почту отправлено\nписьмо со ссылкой. Перейдите\nпо этой ссылке, чтобы восстановить\nпароль."
let label = UILabel(frame: CGRect(x: 20.0, y: 200.0, width: 335.0, height: 9999.0))
label.attributedText = text.attributedString(with: bodyStyle)
label.numberOfLines = 0
label.sizeToFit()
view.addSubview(label)
// Footer
let footerStyle = TextStyle(
fontSize: 14.0,
weight: UIFontWeightRegular,
lineHeight: 22.0,
textColor: UIColor(red: 138.0 / 255.0, green: 138.0 / 255.0, blue: 138.0 / 255.0, alpha: 1.0)
)
let footerText = "Если вы не видите письмо, обязательно\nпроверьте, не попало ли оно в спам."
let footerLabel = UILabel(frame: CGRect(x: 20.0, y: 300.0, width: 400.0, height: 44.0))
footerLabel.attributedText = footerText.attributedString(with: footerStyle)
footerLabel.numberOfLines = 0
footerLabel.sizeToFit()
view.addSubview(footerLabel)
PlaygroundPage.current.liveView = view
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment