Pārlūkot izejas kodu

add extensions to UIView to create programmatically relative layout like UIs

cyberta 6 gadi atpakaļ
vecāks
revīzija
bcdf0a98a6

+ 0 - 32
deltachat-ios/Helper/Extensions.swift

@@ -198,38 +198,6 @@ extension UIImage {
     }
 }
 
-extension UIView {
-    func makeBorder(color: UIColor = UIColor.red) {
-        self.layer.borderColor = color.cgColor
-        self.layer.borderWidth = 2
-    }
-}
-
-extension UIImage {
-    func resizeImage(targetSize: CGSize) -> UIImage {
-        let size = self.size
-
-        let widthRatio  = targetSize.width  / size.width
-        let heightRatio = targetSize.height / size.height
-
-        var newSize: CGSize
-        if(widthRatio > heightRatio) {
-            newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
-        } else {
-            newSize = CGSize(width: size.width * widthRatio, height: size.height *      widthRatio)
-        }
-
-        let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
-
-        UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
-        draw(in: rect)
-        let newImage = UIGraphicsGetImageFromCurrentImageContext()
-        UIGraphicsEndImageContext()
-
-        return newImage!
-    }
-}
-
 
 extension UIColor {
     convenience init(alpha: Int, red: Int, green: Int, blue: Int) {

+ 23 - 0
deltachat-ios/Helper/UIImage+Extension.swift

@@ -23,4 +23,27 @@ extension UIImage {
         guard let cgImage = image?.cgImage else { return nil }
         self.init(cgImage: cgImage)
     }
+
+	func resizeImage(targetSize: CGSize) -> UIImage {
+		let size = self.size
+
+		let widthRatio  = targetSize.width  / size.width
+		let heightRatio = targetSize.height / size.height
+
+		var newSize: CGSize
+		if(widthRatio > heightRatio) {
+			newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
+		} else {
+			newSize = CGSize(width: size.width * widthRatio, height: size.height *      widthRatio)
+		}
+
+		let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
+
+		UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)
+		draw(in: rect)
+		let newImage = UIGraphicsGetImageFromCurrentImageContext()
+		UIGraphicsEndImageContext()
+
+		return newImage!
+	}
 }

+ 97 - 0
deltachat-ios/Helper/UIView+Extension.swift

@@ -0,0 +1,97 @@
+//
+//  File.swift
+//  deltachat-ios
+//
+//  Created by Macci on 05.08.19.
+//  Copyright © 2019 Jonas Reinsch. All rights reserved.
+//
+
+import UIKit
+
+extension UIView {
+	func makeBorder(color: UIColor = UIColor.red) {
+		self.layer.borderColor = color.cgColor
+		self.layer.borderWidth = 2
+	}
+	
+	func constraintAlignTopTo(_ view: UIView) -> NSLayoutConstraint {
+		return constraintAlignTopTo(view, paddingTop: 0.0)
+	}
+	
+	func constraintAlignTopTo(_ view: UIView, paddingTop: CGFloat) -> NSLayoutConstraint {
+		return NSLayoutConstraint(
+			item: self,
+			attribute: .top,
+			relatedBy: .equal,
+			toItem: view,
+			attribute: .top,
+			multiplier: 1.0,
+			constant: paddingTop)
+	}
+
+	func constraintAlignLeadingTo(_ view: UIView) -> NSLayoutConstraint {
+		return constraintAlignLeadingTo(view, paddingLeading: 0.0)
+	}
+
+	func constraintAlignLeadingTo(_ view: UIView, paddingLeading: CGFloat) -> NSLayoutConstraint {
+		return NSLayoutConstraint(
+			item: self,
+			attribute: .leading,
+			relatedBy: .equal,
+			toItem: view,
+			attribute: .leading,
+			multiplier: 1.0,
+			constant: paddingLeading)
+	}
+	
+	func constraintAlignTrailingTo(_ view: UIView) -> NSLayoutConstraint {
+		return constraintAlignTrailingTo(view, paddingTrailing: 0.0)
+	}
+
+	func constraintAlignTrailingTo(_ view: UIView, paddingTrailing: CGFloat) -> NSLayoutConstraint {
+		return NSLayoutConstraint(
+			item: self,
+			attribute: .trailing,
+			relatedBy: .equal,
+			toItem: view,
+			attribute: .trailing,
+			multiplier: 1.0,
+			constant: paddingTrailing)
+	}
+
+	func constraintToBottomOf(_ view: UIView) -> NSLayoutConstraint {
+		return constraintToBottomOf(view, paddingTop: 8)
+	}
+	
+	func constraintToBottomOf(_ view: UIView, paddingTop: CGFloat) -> NSLayoutConstraint {
+		return NSLayoutConstraint(
+			item: self,
+			attribute: .top,
+			relatedBy: .equal,
+			toItem: view,
+			attribute: .bottom,
+			multiplier: 1.0,
+			constant: paddingTop)
+	}
+
+	func constraintCenterXTo(_ view: UIView) -> NSLayoutConstraint {
+		return NSLayoutConstraint(item: self,
+								  attribute: .centerX,
+								  relatedBy: .equal,
+								  toItem: view,
+								  attribute: .centerX,
+								  multiplier: 1.0,
+								  constant: 0.0)
+	}
+
+	func constraintCenterYTo(_ view: UIView) -> NSLayoutConstraint {
+		return NSLayoutConstraint(item: self,
+								  attribute: .centerY,
+								  relatedBy: .equal,
+								  toItem: view,
+								  attribute: .centerY,
+								  multiplier: 1.0,
+								  constant: 0.0)
+	}
+
+}