Last active
August 14, 2018 06:15
-
-
Save azhararmar/779aa68219e02eaffab2afc2d5814f16 to your computer and use it in GitHub Desktop.
IOS Helper Methods
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
// 1. UIView Border | |
extension UIView { | |
func addBorder(edges: UIRectEdge, color: UIColor = .black, thickness: CGFloat = 1.0) { | |
func borderView() -> UIView { | |
let view = UIView(frame: .zero) | |
view.translatesAutoresizingMaskIntoConstraints = false | |
view.backgroundColor = color | |
return view | |
} | |
if edges.contains(.top) || edges.contains(.all) { | |
let topView = borderView() | |
topView.tag = 10000 | |
self.addSubview(topView) | |
self.addConstraints([ | |
NSLayoutConstraint(item: topView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0.0), | |
NSLayoutConstraint(item: topView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0.0), | |
NSLayoutConstraint(item: topView, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1.0, constant: 0.0), | |
NSLayoutConstraint(item: topView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: thickness) | |
]) | |
} | |
if edges.contains(.left) || edges.contains(.all) { | |
let leftView = borderView() | |
leftView.tag = 10001 | |
self.addSubview(leftView) | |
self.addConstraints([ | |
NSLayoutConstraint(item: leftView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0.0), | |
NSLayoutConstraint(item: leftView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0.0), | |
NSLayoutConstraint(item: leftView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: thickness), | |
NSLayoutConstraint(item: leftView, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 1.0, constant: 0.0) | |
]) | |
} | |
if edges.contains(.bottom) || edges.contains(.all) { | |
let bottomView = borderView() | |
bottomView.tag = 10002 | |
self.addSubview(bottomView) | |
self.addConstraints([ | |
NSLayoutConstraint(item: bottomView, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 0.0), | |
NSLayoutConstraint(item: bottomView, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 0.0), | |
NSLayoutConstraint(item: bottomView, attribute: .width, relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1.0, constant: 0.0), | |
NSLayoutConstraint(item: bottomView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: thickness) | |
]) | |
} | |
if edges.contains(.right) || edges.contains(.all) { | |
let rightView = borderView() | |
rightView.tag = 10003 | |
self.addSubview(rightView) | |
self.addConstraints([ | |
NSLayoutConstraint(item: rightView, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 0.0), | |
NSLayoutConstraint(item: rightView, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 0.0), | |
NSLayoutConstraint(item: rightView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1.0, constant: thickness), | |
NSLayoutConstraint(item: rightView, attribute: .height, relatedBy: .equal, toItem: self, attribute: .height, multiplier: 1.0, constant: 0.0) | |
]) | |
} | |
} | |
} | |
// 2. UIColor Hex | |
extension UIColor { | |
convenience init(hex: String) { | |
let hexString = hex.trimmingCharacters(in: CharacterSet.alphanumerics.inverted) | |
var int = UInt32() | |
Scanner(string: hexString).scanHexInt32(&int) | |
let a, r, g, b: UInt32 | |
switch hexString.characters.count { | |
case 3: | |
(a, r, g, b) = (255, (int >> 8) * 17, (int >> 4 & 0xF) * 17, (int & 0xF) * 17) | |
case 6: | |
(a, r, g, b) = (255, int >> 16, int >> 8 & 0xFF, int & 0xFF) | |
case 8: | |
(a, r, g, b) = (int >> 24, int >> 16 & 0xFF, int >> 8 & 0xFF, int & 0xFF) | |
default: | |
(a, r, g, b) = (255, 0, 0, 0) | |
} | |
self.init(red: CGFloat(r) / 255, green: CGFloat(g) / 255, blue: CGFloat(b) / 255, alpha: CGFloat(a) / 255) | |
} | |
} | |
// 3. String | |
extension String { | |
func isValidEmailAddress() -> Bool { | |
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}" | |
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) | |
return emailTest.evaluate(with: self) | |
} | |
func isArabic() -> Bool { | |
let predicate = NSPredicate(format:"SELF MATCHES %@", "(?s).*\\p{Arabic}.*") | |
return predicate.evaluate(with: self) | |
} | |
func hasArabicCharacters() -> Bool { | |
return self.isArabic() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment