Skip to content

Commit 5f4c18d

Browse files
authored
added extensions.
Added extensions of CALayer, UIView, String, UIColor, NSNumber, UITextField, UIScrollView
1 parent f261cd3 commit 5f4c18d

7 files changed

+314
-0
lines changed

CALayer+Extension.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// CALayer+Extension.swift
3+
// SHoaib
4+
//
5+
// Created by Shoaib Sarwar Cheema on 26/05/2016.
6+
// Copyright © 2016. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension CALayer {
12+
13+
func addBorder(_ edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
14+
15+
let border = CALayer()
16+
17+
switch edge {
18+
case UIRectEdge.top:
19+
border.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: thickness)
20+
break
21+
case UIRectEdge.bottom:
22+
border.frame = CGRect(x: 0, y: self.frame.height - thickness, width: self.frame.width, height: thickness)
23+
break
24+
case UIRectEdge.left:
25+
border.frame = CGRect(x: 0, y: 0, width: thickness, height: self.bounds.height)
26+
break
27+
case UIRectEdge.right:
28+
border.frame = CGRect(x: self.frame.width - thickness, y: 0, width: thickness, height: self.bounds.height)
29+
break
30+
default:
31+
break
32+
}
33+
34+
border.backgroundColor = color.cgColor;
35+
36+
self.addSublayer(border)
37+
}
38+
39+
}

NSNumber+Extension.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// Int+Extension.swift
3+
// Shoaib
4+
//
5+
// Created by Shoaib Sarwar Cheema on 07/04/2016.
6+
// Copyright © 2016. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
extension NSNumber {
12+
func format(_ f: String) -> String {
13+
return String(format: "%\(f)f", self)
14+
}
15+
}
16+
17+
extension Int {
18+
func boolValue() ->Bool {
19+
switch self {
20+
case 0:
21+
return false
22+
default:
23+
return true
24+
}
25+
}
26+
}
27+
extension Bool {
28+
func intValue() ->Int {
29+
switch self {
30+
case false:
31+
return 0
32+
default:
33+
return 1
34+
}
35+
}
36+
}
37+
38+
39+
extension Int {
40+
func format(_ f: String) -> String {
41+
return String(format: "%\(f)d", self)
42+
}
43+
}
44+
45+
extension Float {
46+
func format(_ f: String) -> String {
47+
return String(format: "%\(f)f", self)
48+
}
49+
}
50+
51+
extension Double {
52+
func format(_ f: String) -> String {
53+
return String(format: "%\(f)f", self)
54+
}
55+
}

String+Extensions.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// String+Extension.swift
3+
// Shoaib
4+
//
5+
// Created by Shoaib Sarwar Cheema on 14/10/2015.
6+
// Copyright © 2015. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
extension String {
12+
13+
mutating func removeAllSpaces() {
14+
replaceOccurrences(" ", withString: "")
15+
}
16+
var stringByRemovingExcessiveSpaces: String {
17+
let components = self.components(separatedBy: CharacterSet.whitespaces)
18+
let filtered = components.filter({!$0.isEmpty})
19+
return filtered.joined(separator: " ")
20+
}
21+
22+
mutating func replaceOccurrences(_ target: String, withString: String)
23+
{
24+
self = replacingOccurrences(of: target, with: withString)
25+
}
26+
27+
mutating func encodeForUrl() {
28+
self = self.addingPercentEncoding( withAllowedCharacters: CharacterSet.urlQueryAllowed)!
29+
}
30+
func encodedStringForUrl() -> String {
31+
let string = self
32+
return string.addingPercentEncoding( withAllowedCharacters: CharacterSet.urlQueryAllowed)!
33+
}
34+
35+
func hasAnyPrefix(Prefixes prefixes: [String]) -> Bool{
36+
37+
for prefix in prefixes{
38+
if self.hasPrefix(prefix){
39+
return true
40+
}
41+
}
42+
return false
43+
}
44+
45+
static func randomStringWithLength (_ len : Int) -> String {
46+
47+
let letters : NSString = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
48+
let randomString : NSMutableString = NSMutableString(capacity: len)
49+
50+
for _ in 1...(len-1) {
51+
let length = UInt32 (letters.length)
52+
let rand = arc4random_uniform(length)
53+
randomString.appendFormat("%C", letters.character(at: Int(rand)))
54+
}
55+
56+
return randomString as String
57+
}
58+
59+
func fromBase64() -> String? {
60+
guard let data = Data(base64Encoded: self) else {
61+
return nil
62+
}
63+
64+
return String(data: data, encoding: .utf8)
65+
}
66+
67+
func toBase64() -> String {
68+
return Data(self.utf8).base64EncodedString()
69+
}
70+
}

UIColor+Extension.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// UIColor+Extension.swift
3+
// Shoaib
4+
//
5+
// Created by Shoaib Sarwar Cheema on 14/10/2015.
6+
// Copyright © 2015. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UIColor {
12+
convenience init(rgb: Int, alpha: CGFloat) {
13+
let r = CGFloat((rgb & 0xFF0000) >> 16)/255
14+
let g = CGFloat((rgb & 0xFF00) >> 8)/255
15+
let b = CGFloat(rgb & 0xFF)/255
16+
self.init(red: r, green: g, blue: b, alpha: alpha)
17+
}
18+
19+
convenience init(rgb: Int) {
20+
self.init(rgb:rgb, alpha:1.0)
21+
}
22+
}

UIScrollView+Extension.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// UIScrollView+Extension.swift
3+
// Shoaib
4+
//
5+
// Created by Shoaib Sarwar Cheema on 19/10/2016.
6+
// Copyright © Arhamsoft. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UIScrollView {
12+
func scrollToBottom(animated: Bool) {
13+
self.setContentOffset(CGPoint(x: 0, y: max(self.contentSize.height - self.bounds.size.height, 0) ), animated: animated)
14+
15+
}
16+
}

UITextField+Extension.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// UITextField+Extension.swift
3+
// Shoaib
4+
//
5+
// Created by Shoaib Sarwar Cheema on 18/07/2017.
6+
// Copyright © 2017. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
extension UITextField {
12+
@IBInspectable var placeholderColor: UIColor {
13+
get {
14+
guard let currentAttributedPlaceholderColor = attributedPlaceholder?.attribute(NSForegroundColorAttributeName, at: 0, effectiveRange: nil) as? UIColor else { return UIColor.clear }
15+
return currentAttributedPlaceholderColor
16+
}
17+
set {
18+
guard let currentAttributedString = attributedPlaceholder else { return }
19+
let attributes = [NSForegroundColorAttributeName : newValue]
20+
21+
attributedPlaceholder = NSAttributedString(string: currentAttributedString.string, attributes: attributes)
22+
}
23+
}
24+
}
25+
26+
class LinedTextField: UITextField {
27+
28+
required init?(coder aDecoder: NSCoder) {
29+
super.init(coder: aDecoder)
30+
31+
setupView()
32+
}
33+
34+
override init(frame: CGRect) {
35+
super.init(frame: frame)
36+
37+
setupView()
38+
39+
}
40+
41+
func setupView(){
42+
43+
borderStyle = .none
44+
addBorder(.bottom, color: .white, thickness: 1)
45+
placeholderColor = UIColor(red: 1, green: 1, blue: 1, alpha: 0.7)
46+
textColor = UIColor.white
47+
clipsToBounds = true
48+
}
49+
}

UIView+Extension.swift

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// UIView+Extension.swift
3+
// Shoaib
4+
//
5+
// Created by Shoaib Sarwar Cheema on 25/02/2016.
6+
// Copyright © 2016. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
@IBDesignable extension UIView {
12+
13+
@IBInspectable var borderColor:UIColor? {
14+
set {
15+
layer.borderColor = newValue!.cgColor
16+
}
17+
get {
18+
if let color = layer.borderColor {
19+
return UIColor(cgColor:color)
20+
}
21+
else {
22+
return nil
23+
}
24+
}
25+
}
26+
@IBInspectable var borderWidth:CGFloat {
27+
set {
28+
layer.borderWidth = newValue
29+
}
30+
get {
31+
return layer.borderWidth
32+
}
33+
}
34+
@IBInspectable var cornerRadius:CGFloat {
35+
set {
36+
layer.cornerRadius = newValue
37+
clipsToBounds = newValue > 0
38+
}
39+
get {
40+
return layer.cornerRadius
41+
}
42+
}
43+
@IBInspectable var padding:CGFloat {
44+
45+
set {
46+
self.bounds = self.frame.insetBy(dx: padding, dy: padding);
47+
}
48+
get {
49+
return self.bounds.origin.x
50+
}
51+
}
52+
53+
class func loadFromNibNamed(_ nibNamed: String, bundle : Bundle? = nil,owner: AnyObject) -> UIView? {
54+
return UINib(
55+
nibName: nibNamed,
56+
bundle: bundle
57+
).instantiate(withOwner: owner, options: nil)[0] as? UIView
58+
}
59+
60+
func addBorder(_ edge: UIRectEdge, color: UIColor, thickness: CGFloat) {
61+
self.layer.addBorder(edge, color: color, thickness: thickness)
62+
}
63+
}

0 commit comments

Comments
 (0)