Last active
January 26, 2021 20:22
-
-
Save T-Pham/30e2cc17f1179251d2c4df8b86384036 to your computer and use it in GitHub Desktop.
An ugly and quick attempt to convert SnapKit to native NSLayoutConstraint
This file contains hidden or 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 Foundation | |
let s = """ | |
aview.snp.makeConstraints { (make) -> Void in | |
make.centerX.equalTo(view.snp.centerX) | |
make.bottom.equalTo(anotherview.snp.top).offset(-10) | |
make.height.equalTo(40) | |
make.width.equalTo(view.snp.width) | |
} | |
""" | |
let object = String(s.split(separator: ".").first!) | |
let lines = s.split(separator: "\n") | |
print("NSLayoutConstraint.activate([") | |
for i in 1 ..< lines.count - 1 { | |
let line = lines[i] | |
var newLine = String(line) | |
newLine = newLine.replacingOccurrences(of: "make.", with: "\(object).") | |
newLine = newLine.replacingOccurrences(of: ".snp.", with: ".") | |
newLine = newLine.replacingOccurrences(of: ".width.equalTo(", with: ".widthAnchor.constraint(equalToConstant: ") | |
newLine = newLine.replacingOccurrences(of: ".height.equalTo(", with: ".heightAnchor.constraint(equalToConstant: ") | |
newLine = newLine.replacingOccurrences(of: ".lessThanOrEqualTo(", with: ".constraint(lessThanOrEqualTo: ") | |
newLine = newLine.replacingOccurrences(of: ".width.", with: ".widthAnchor.") | |
newLine = newLine.replacingOccurrences(of: ".height.", with: ".heightAnchor.") | |
newLine = newLine.replacingOccurrences(of: ".width,", with: ".widthAnchor,") | |
newLine = newLine.replacingOccurrences(of: ".height,", with: ".heightAnchor,") | |
newLine = newLine.replacingOccurrences(of: ".width)", with: ".widthAnchor)") | |
newLine = newLine.replacingOccurrences(of: ".height)", with: ".heightAnchor)") | |
newLine = newLine.replacingOccurrences(of: ".centerX", with: ".centerXAnchor") | |
newLine = newLine.replacingOccurrences(of: ".centerY", with: ".centerYAnchor") | |
newLine = newLine.replacingOccurrences(of: ".top", with: ".topAnchor") | |
newLine = newLine.replacingOccurrences(of: ".bottom", with: ".bottomAnchor") | |
newLine = newLine.replacingOccurrences(of: ".left", with: ".leadingAnchor") | |
newLine = newLine.replacingOccurrences(of: ".right", with: ".trailingAnchor") | |
newLine = newLine.replacingOccurrences(of: ".equalTo(", with: ".constraint(equalTo: ") | |
newLine = newLine.replacingOccurrences(of: ").offset(", with: ", constant: ") | |
newLine = newLine.replacingOccurrences(of: ".edges.constraint(equalTo: self)", with: | |
""" | |
.topAnchor.constraint(equalTo: topAnchor), | |
\(object).leadingAnchor.constraint(equalTo: leadingAnchor), | |
\(object).bottomAnchor.constraint(equalTo: bottomAnchor), | |
\(object).trailingAnchor.constraint(equalTo: trailingAnchor) | |
""" | |
) | |
newLine = newLine.replacingOccurrences(of: ".edges.constraint(equalTo: super.view)", with: | |
""" | |
.topAnchor.constraint(equalTo: view.topAnchor), | |
\(object).leadingAnchor.constraint(equalTo: view.leadingAnchor), | |
\(object).bottomAnchor.constraint(equalTo: view.bottomAnchor), | |
\(object).trailingAnchor.constraint(equalTo: view.trailingAnchor) | |
""" | |
) | |
newLine = newLine.replacingOccurrences(of: ".center.constraint(equalTo: self)", with: | |
""" | |
.centerXAnchor.constraint(equalTo: centerXAnchor), | |
\(object).centerYAnchor.constraint(equalTo: centerYAnchor) | |
""" | |
) | |
if newLine.contains(".widthAnchor.heightAnchor.") || newLine.contains(".heightAnchor.widthAnchor.") { | |
var newLine1 = newLine | |
var newLine2 = newLine | |
newLine1 = newLine1.replacingOccurrences(of: ".heightAnchor.", with: ".") | |
newLine2 = newLine2.replacingOccurrences(of: ".widthAnchor.", with: ".") | |
newLine = newLine1 + ",\n" + newLine2 | |
} | |
newLine += "," | |
print(newLine) | |
} | |
print("])") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment