Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9374021

Browse files
add more extesion
1 parent 1808fa4 commit 9374021

6 files changed

Lines changed: 260 additions & 2 deletions

File tree

Sources/ZYCommonUtil/CustomNavViewController.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ open class NavBarView: UIView {
3232
override init(frame: CGRect) {
3333
super.init(frame: frame)
3434

35-
36-
3735
backBtn = UIButton(type: .custom)
3836
backBtn.setImage(UIImage(named: "common_back_white"), for: .normal)
3937
addSubview(backBtn)
@@ -103,6 +101,10 @@ open class NavBarView: UIView {
103101
delegate?.clickNavBar(type: 1)
104102
} else if btn == right2Btn {
105103
delegate?.clickNavBar(type: 2)
104+
} else if btn == backBtn {
105+
if let vc = next?.next, vc is UIViewController, let v = vc as? UIViewController {
106+
v.navigationController?.popViewController(animated: true)
107+
}
106108
}
107109
}
108110
}

Sources/ZYCommonUtil/String+Util.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@
99
import Foundation
1010
import UIKit
1111

12+
13+
public extension String {
14+
func zy_widthForComment(fontSize: CGFloat, height: CGFloat = 15) -> CGFloat {
15+
let font = UIFont.systemFont(ofSize: fontSize)
16+
let rect = NSString(string: self).boundingRect(with: CGSize(width: CGFloat(MAXFLOAT), height: height), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
17+
return ceil(rect.width)
18+
}
19+
20+
func zy_heightForComment(fontSize: CGFloat, width: CGFloat) -> CGFloat {
21+
let font = UIFont.systemFont(ofSize: fontSize)
22+
let rect = NSString(string: self).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
23+
return ceil(rect.height)
24+
}
25+
26+
func zy_heightForComment(fontSize: CGFloat, width: CGFloat, maxHeight: CGFloat) -> CGFloat {
27+
let font = UIFont.systemFont(ofSize: fontSize)
28+
let rect = NSString(string: self).boundingRect(with: CGSize(width: width, height: CGFloat(MAXFLOAT)), options: .usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
29+
return ceil(rect.height)>maxHeight ? maxHeight : ceil(rect.height)
30+
}
31+
}
32+
33+
1234
public extension NSAttributedString {
1335
/// 获取string文字size
1436
/// - Returns: description

Sources/ZYCommonUtil/UIImage+Util.swift

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,96 @@
99
import Foundation
1010
import UIKit
1111

12+
public extension UIImage {
13+
func resizedImage(size: CGSize) -> UIImage {
14+
if self.size.width < size.width {
15+
return self
16+
}
17+
18+
let renderer = UIGraphicsImageRenderer(size: size)
19+
return renderer.image { (context) in
20+
self.draw(in: CGRect(origin: .zero, size: size))
21+
}
22+
}
23+
24+
func resizedImage(width: CGFloat) -> UIImage {
25+
let h = (self.size.height / self.size.width) * width
26+
return self.resizedImage(size: CGSize(width: width, height: h))
27+
}
28+
29+
func fixedOrientation() -> UIImage? {
30+
guard imageOrientation != UIImage.Orientation.up else {
31+
return self.copy() as? UIImage
32+
}
33+
34+
guard let cgImage = self.cgImage else {
35+
return nil
36+
}
37+
38+
guard let colorSpace = cgImage.colorSpace, let ctx = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: cgImage.bitsPerComponent, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue) else {
39+
return nil //Not able to create CGContext
40+
}
41+
42+
var transform: CGAffineTransform = CGAffineTransform.identity
43+
44+
switch imageOrientation {
45+
case .down, .downMirrored:
46+
transform = transform.translatedBy(x: size.width, y: size.height)
47+
transform = transform.rotated(by: CGFloat.pi)
48+
break
49+
case .left, .leftMirrored:
50+
transform = transform.translatedBy(x: size.width, y: 0)
51+
transform = transform.rotated(by: CGFloat.pi / 2.0)
52+
break
53+
case .right, .rightMirrored:
54+
transform = transform.translatedBy(x: 0, y: size.height)
55+
transform = transform.rotated(by: CGFloat.pi / -2.0)
56+
break
57+
case .up, .upMirrored:
58+
break
59+
@unknown default:
60+
print("")
61+
}
62+
//Flip image one more time if needed to, this is to prevent flipped image
63+
switch imageOrientation {
64+
case .upMirrored, .downMirrored:
65+
transform = transform.translatedBy(x: size.width, y: 0)
66+
transform = transform.scaledBy(x: -1, y: 1)
67+
break
68+
case .leftMirrored, .rightMirrored:
69+
transform = transform.translatedBy(x: size.height, y: 0)
70+
transform = transform.scaledBy(x: -1, y: 1)
71+
case .up, .down, .left, .right:
72+
break
73+
@unknown default:
74+
print("")
75+
}
76+
ctx.concatenate(transform)
77+
switch imageOrientation {
78+
case .left, .leftMirrored, .right, .rightMirrored:
79+
ctx.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: size.height, height: size.width))
80+
default:
81+
ctx.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
82+
break
83+
}
84+
guard let newCGImage = ctx.makeImage() else { return nil }
85+
return UIImage.init(cgImage: newCGImage, scale: 1, orientation: .up)
86+
}
87+
}
88+
89+
public extension UIImage {
90+
static func withColor(_ color: UIColor, size: CGSize = CGSize(width: 1, height: 1)) -> UIImage {
91+
let format = UIGraphicsImageRendererFormat()
92+
format.scale = 1
93+
let image = UIGraphicsImageRenderer(size: size, format: format).image { rendererContext in
94+
color.setFill()
95+
rendererContext.fill(CGRect(origin: .zero, size: size))
96+
}
97+
return image
98+
}
99+
}
100+
101+
12102
public extension UIImage {
13103
/// 根据文字生成包含第一个文字的图片
14104
/// - Parameters:

Sources/ZYCommonUtil/UINavVC.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by edz on 2021/4/21.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
public extension UINavigationController {
12+
func pushViewController(_ viewController: UIViewController, animated: Bool = true, completion: @escaping () -> Void) {
13+
CATransaction.begin()
14+
CATransaction.setCompletionBlock(completion)
15+
pushViewController(viewController, animated: animated)
16+
CATransaction.commit()
17+
}
18+
19+
func popViewController(animated: Bool = true, completion: @escaping () -> Void) {
20+
CATransaction.begin()
21+
CATransaction.setCompletionBlock(completion)
22+
popViewController(animated: animated)
23+
CATransaction.commit()
24+
}
25+
26+
func popToRootViewController(animated: Bool = true, completion: @escaping () -> Void) {
27+
CATransaction.begin()
28+
CATransaction.setCompletionBlock(completion)
29+
popToRootViewController(animated: animated)
30+
CATransaction.commit()
31+
}
32+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by edz on 2021/4/21.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
public extension UIView {
12+
@discardableResult
13+
func addTo(v: UIView) -> Self {
14+
v.addSubview(self)
15+
return self
16+
}
17+
18+
@discardableResult
19+
func bgColor(c: UIColor) -> Self {
20+
backgroundColor = c
21+
return self
22+
}
23+
}
24+
25+
public typealias BtnAction = (UIButton)->()
26+
public extension UIButton{
27+
private struct AssociatedKeys{
28+
static var actionKey = "actionKey"
29+
}
30+
31+
static func custom() -> UIButton {
32+
return UIButton(type: .custom)
33+
}
34+
35+
36+
@discardableResult
37+
func normalTitle(txt: String) -> UIButton {
38+
setTitle(txt, for: .normal)
39+
return self
40+
}
41+
42+
@discardableResult
43+
func fontSize(size: CGFloat) -> UIButton {
44+
titleLabel?.font = UIFont.systemFont(ofSize: size)
45+
return self
46+
}
47+
@discardableResult
48+
func txtColor(col: UIColor) -> UIButton {
49+
setTitleColor(col, for: .normal)
50+
return self
51+
}
52+
53+
54+
@discardableResult
55+
func selectTitle(txt: String) -> UIButton {
56+
setTitle(txt, for: .selected)
57+
return self
58+
}
59+
60+
@discardableResult
61+
func normalImage(name: String) -> UIButton {
62+
setImage(UIImage(named: name), for: .normal)
63+
return self
64+
}
65+
66+
@discardableResult
67+
func selectedImage(name: String) -> UIButton {
68+
setImage(UIImage(named: name), for: .selected)
69+
return self
70+
}
71+
72+
@objc dynamic var action: BtnAction? {
73+
set{
74+
objc_setAssociatedObject(self,&AssociatedKeys.actionKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
75+
}
76+
get{
77+
if let action = objc_getAssociatedObject(self, &AssociatedKeys.actionKey) as? BtnAction{
78+
return action
79+
}
80+
return nil
81+
}
82+
}
83+
84+
@discardableResult
85+
func addUpInsideAction(action:@escaping BtnAction) -> UIButton {
86+
self.action = action
87+
self.addTarget(self, action: #selector(touchUpInSideBtnAction), for: .touchUpInside)
88+
return self
89+
}
90+
91+
@objc func touchUpInSideBtnAction(btn: UIButton) {
92+
if let action = self.action {
93+
action(self)
94+
}
95+
}
96+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// File.swift
3+
//
4+
//
5+
// Created by edz on 2021/4/21.
6+
//
7+
8+
import Foundation
9+
10+
public extension URL {
11+
static var documentPath: URL {
12+
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
13+
let documentsDirectory = paths[0]
14+
return documentsDirectory
15+
}
16+
}

0 commit comments

Comments
 (0)