Created
September 28, 2017 07:08
-
-
Save 3lvis/0536d95518169991d38e08a90b232313 to your computer and use it in GitHub Desktop.
1-1 Label Swift
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 | |
import PlaygroundSupport | |
class CustomLabel: UILabel { | |
var topInset: CGFloat = 2.25 | |
var bottomInset: CGFloat = 2.25 | |
override func drawText(in rect: CGRect) { | |
let insets = UIEdgeInsets(top: topInset, left: 0, bottom: bottomInset, right: 0) | |
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets)) | |
} | |
override var intrinsicContentSize: CGSize { | |
get { | |
var contentSize = super.intrinsicContentSize | |
contentSize.height += topInset + bottomInset | |
return contentSize | |
} | |
} | |
} | |
class ViewController: UIViewController { | |
let topMargin = CGFloat(100) | |
let lineHeightMultiple = CGFloat(1) | |
let lineSpacing = CGFloat(4.5) | |
func attributedString(for text: String) -> NSMutableAttributedString { | |
let style = NSMutableParagraphStyle() | |
style.lineHeightMultiple = lineHeightMultiple | |
style.lineSpacing = lineSpacing | |
let string = NSMutableAttributedString(string: text) | |
string.addAttribute(.paragraphStyle, value:style, range: NSMakeRange(0, string.length)) | |
return string | |
} | |
lazy var label1: CustomLabel = { | |
let view = CustomLabel() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.backgroundColor = .red | |
view.font = UIFont.systemFont(ofSize: 18) | |
view.attributedText = attributedString(for: "Type") | |
return view | |
}() | |
lazy var label2: CustomLabel = { | |
let view = CustomLabel() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.backgroundColor = .green | |
view.font = UIFont.systemFont(ofSize: 18) | |
view.attributedText = attributedString(for: "Type") | |
return view | |
}() | |
lazy var label3: CustomLabel = { | |
let view = CustomLabel() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.backgroundColor = .blue | |
view.font = UIFont.systemFont(ofSize: 18) | |
view.attributedText = attributedString(for: "Type") | |
return view | |
}() | |
lazy var multilineLabel1: CustomLabel = { | |
let view = CustomLabel() | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.backgroundColor = .brown | |
view.numberOfLines = 0 | |
view.font = UIFont.systemFont(ofSize: 18) | |
view.attributedText = attributedString(for: "Type Type Type") | |
return view | |
}() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
view.addSubview(label1) | |
view.addSubview(label2) | |
view.addSubview(label3) | |
view.addSubview(multilineLabel1) | |
view.backgroundColor = .white | |
let margin: CGFloat = 8 | |
NSLayoutConstraint.activate([ | |
label1.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: margin), | |
label1.topAnchor.constraint(equalTo: view.topAnchor, constant: topMargin), | |
label2.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: margin), | |
label2.topAnchor.constraint(equalTo: label1.bottomAnchor), | |
label3.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: margin), | |
label3.topAnchor.constraint(equalTo: label2.bottomAnchor), | |
multilineLabel1.leadingAnchor.constraint(equalTo: label1.trailingAnchor), | |
multilineLabel1.topAnchor.constraint(equalTo: view.topAnchor, constant: topMargin), | |
multilineLabel1.widthAnchor.constraint(equalToConstant: 40) | |
]) | |
} | |
} | |
PlaygroundPage.current.liveView = ViewController() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment