session-ios/SignalUtilitiesKit/Utilities/UIView+OWS.swift

441 lines
13 KiB
Swift
Raw Normal View History

//
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
import Foundation
public extension UIEdgeInsets {
2019-04-15 18:23:02 +02:00
init(top: CGFloat, leading: CGFloat, bottom: CGFloat, trailing: CGFloat) {
self.init(top: top,
left: CurrentAppContext().isRTL ? trailing : leading,
bottom: bottom,
right: CurrentAppContext().isRTL ? leading : trailing)
}
}
2019-03-21 15:26:38 +01:00
// MARK: -
@objc
public extension UINavigationController {
2019-04-15 18:23:02 +02:00
func pushViewController(_ viewController: UIViewController,
animated: Bool,
completion: (() -> Void)?) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
pushViewController(viewController, animated: animated)
CATransaction.commit()
}
2019-04-15 18:23:02 +02:00
func popViewController(animated: Bool,
completion: (() -> Void)?) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
popViewController(animated: animated)
CATransaction.commit()
}
2019-04-15 18:23:02 +02:00
func popToViewController(_ viewController: UIViewController,
animated: Bool,
completion: (() -> Void)?) {
CATransaction.begin()
CATransaction.setCompletionBlock(completion)
self.popToViewController(viewController, animated: animated)
CATransaction.commit()
}
}
2019-02-12 16:03:32 +01:00
2019-03-21 15:26:38 +01:00
// MARK: -
2019-04-15 18:23:02 +02:00
@objc
public extension UIView {
func renderAsImage() -> UIImage? {
2019-02-12 16:03:32 +01:00
return renderAsImage(opaque: false, scale: UIScreen.main.scale)
}
2019-04-15 18:23:02 +02:00
func renderAsImage(opaque: Bool, scale: CGFloat) -> UIImage? {
2019-02-12 16:03:32 +01:00
if #available(iOS 10, *) {
let format = UIGraphicsImageRendererFormat()
format.scale = scale
format.opaque = opaque
let renderer = UIGraphicsImageRenderer(bounds: self.bounds,
format: format)
return renderer.image { (context) in
self.layer.render(in: context.cgContext)
}
} else {
UIGraphicsBeginImageContextWithOptions(bounds.size, opaque, scale)
if let _ = UIGraphicsGetCurrentContext() {
drawHierarchy(in: bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
owsFailDebug("Could not create graphics context.")
return nil
}
}
2019-02-12 20:56:58 +01:00
2019-04-15 18:23:02 +02:00
class func spacer(withWidth width: CGFloat) -> UIView {
2019-02-12 20:56:58 +01:00
let view = UIView()
view.autoSetDimension(.width, toSize: width)
return view
}
2019-04-15 18:23:02 +02:00
class func spacer(withHeight height: CGFloat) -> UIView {
2019-02-12 20:56:58 +01:00
let view = UIView()
view.autoSetDimension(.height, toSize: height)
return view
}
2019-02-06 22:00:22 +01:00
2019-04-15 18:23:02 +02:00
class func hStretchingSpacer() -> UIView {
let view = UIView()
view.setContentHuggingHorizontalLow()
view.setCompressionResistanceHorizontalLow()
return view
}
2019-04-15 18:23:02 +02:00
class func vStretchingSpacer() -> UIView {
let view = UIView()
view.setContentHuggingVerticalLow()
view.setCompressionResistanceVerticalLow()
return view
}
2019-04-15 18:23:02 +02:00
func applyScaleAspectFitLayout(subview: UIView, aspectRatio: CGFloat) -> [NSLayoutConstraint] {
2019-02-06 22:00:22 +01:00
guard subviews.contains(subview) else {
owsFailDebug("Not a subview.")
return []
}
// This emulates the behavior of contentMode = .scaleAspectFit using
// iOS auto layout constraints.
//
// This allows ConversationInputToolbar to place the "cancel" button
// in the upper-right hand corner of the preview content.
var constraints = [NSLayoutConstraint]()
constraints.append(contentsOf: subview.autoCenterInSuperview())
constraints.append(subview.autoPin(toAspectRatio: aspectRatio))
constraints.append(subview.autoMatch(.width, to: .width, of: self, withMultiplier: 1.0, relation: .lessThanOrEqual))
constraints.append(subview.autoMatch(.height, to: .height, of: self, withMultiplier: 1.0, relation: .lessThanOrEqual))
return constraints
}
2019-04-15 18:18:41 +02:00
func setShadow(radius: CGFloat = 2.0, opacity: CGFloat = 0.66, offset: CGPoint = .zero, color: CGColor = UIColor.black.cgColor) {
layer.shadowColor = UIColor.black.cgColor
layer.shadowRadius = 2.0
layer.shadowOpacity = 0.66
layer.shadowOffset = .zero
}
2019-02-06 22:00:22 +01:00
}
2019-03-21 15:26:38 +01:00
// MARK: -
2019-04-15 18:23:02 +02:00
@objc
public extension UIViewController {
public func presentAlert(_ alert: UIAlertController) {
self.presentAlert(alert, animated: true)
}
public func presentAlert(_ alert: UIAlertController, animated: Bool) {
2019-03-21 15:26:38 +01:00
self.present(alert,
animated: animated,
completion: {
alert.applyAccessibilityIdentifiers()
})
}
public func presentAlert(_ alert: UIAlertController, completion: @escaping (() -> Void)) {
self.present(alert,
animated: true,
completion: {
alert.applyAccessibilityIdentifiers()
completion()
})
}
2019-03-21 15:26:38 +01:00
}
// MARK: -
2019-02-06 22:00:22 +01:00
public extension CGFloat {
2019-04-15 18:23:02 +02:00
func clamp(_ minValue: CGFloat, _ maxValue: CGFloat) -> CGFloat {
2019-02-27 16:29:30 +01:00
return CGFloatClamp(self, minValue, maxValue)
}
2019-04-15 18:23:02 +02:00
func clamp01() -> CGFloat {
2019-02-27 16:29:30 +01:00
return CGFloatClamp01(self)
}
2019-02-06 22:00:22 +01:00
// Linear interpolation
2019-04-15 18:23:02 +02:00
func lerp(_ minValue: CGFloat, _ maxValue: CGFloat) -> CGFloat {
2019-02-06 22:00:22 +01:00
return CGFloatLerp(minValue, maxValue, self)
}
// Inverse linear interpolation
2019-04-15 18:23:02 +02:00
func inverseLerp(_ minValue: CGFloat, _ maxValue: CGFloat, shouldClamp: Bool = false) -> CGFloat {
2019-02-06 22:00:22 +01:00
let value = CGFloatInverseLerp(self, minValue, maxValue)
return (shouldClamp ? CGFloatClamp01(value) : value)
}
2019-04-15 18:23:02 +02:00
static let halfPi: CGFloat = CGFloat.pi * 0.5
2019-04-15 18:23:02 +02:00
func fuzzyEquals(_ other: CGFloat, tolerance: CGFloat = 0.001) -> Bool {
return abs(self - other) < tolerance
}
2019-04-15 18:23:02 +02:00
var square: CGFloat {
return self * self
}
2019-02-06 22:00:22 +01:00
}
2019-03-21 15:26:38 +01:00
// MARK: -
2019-02-27 16:29:30 +01:00
public extension Int {
2019-04-15 18:23:02 +02:00
func clamp(_ minValue: Int, _ maxValue: Int) -> Int {
2019-02-27 16:29:30 +01:00
assert(minValue <= maxValue)
return Swift.max(minValue, Swift.min(maxValue, self))
}
}
2019-03-21 15:26:38 +01:00
// MARK: -
2019-02-06 22:00:22 +01:00
public extension CGPoint {
2019-04-15 18:23:02 +02:00
func toUnitCoordinates(viewBounds: CGRect, shouldClamp: Bool) -> CGPoint {
2019-02-06 22:00:22 +01:00
return CGPoint(x: (x - viewBounds.origin.x).inverseLerp(0, viewBounds.width, shouldClamp: shouldClamp),
y: (y - viewBounds.origin.y).inverseLerp(0, viewBounds.height, shouldClamp: shouldClamp))
}
2019-04-15 18:23:02 +02:00
func toUnitCoordinates(viewSize: CGSize, shouldClamp: Bool) -> CGPoint {
2019-02-06 22:00:22 +01:00
return toUnitCoordinates(viewBounds: CGRect(origin: .zero, size: viewSize), shouldClamp: shouldClamp)
}
2019-04-15 18:23:02 +02:00
func fromUnitCoordinates(viewBounds: CGRect) -> CGPoint {
return CGPoint(x: viewBounds.origin.x + x.lerp(0, viewBounds.size.width),
y: viewBounds.origin.y + y.lerp(0, viewBounds.size.height))
}
2019-04-15 18:23:02 +02:00
func fromUnitCoordinates(viewSize: CGSize) -> CGPoint {
return fromUnitCoordinates(viewBounds: CGRect(origin: .zero, size: viewSize))
2019-02-06 22:00:22 +01:00
}
2019-04-15 18:23:02 +02:00
func inverse() -> CGPoint {
2019-02-06 22:00:22 +01:00
return CGPoint(x: -x, y: -y)
}
2019-04-15 18:23:02 +02:00
func plus(_ value: CGPoint) -> CGPoint {
2019-02-06 22:00:22 +01:00
return CGPointAdd(self, value)
}
2019-04-15 18:23:02 +02:00
func minus(_ value: CGPoint) -> CGPoint {
2019-02-06 22:00:22 +01:00
return CGPointSubtract(self, value)
}
2019-04-15 18:23:02 +02:00
func times(_ value: CGFloat) -> CGPoint {
2019-02-25 23:18:49 +01:00
return CGPoint(x: x * value, y: y * value)
}
2019-04-15 18:23:02 +02:00
func min(_ value: CGPoint) -> CGPoint {
2019-02-25 23:18:49 +01:00
// We use "Swift" to disambiguate the global function min() from this method.
return CGPoint(x: Swift.min(x, value.x),
y: Swift.min(y, value.y))
}
2019-04-15 18:23:02 +02:00
func max(_ value: CGPoint) -> CGPoint {
2019-02-25 23:18:49 +01:00
// We use "Swift" to disambiguate the global function max() from this method.
return CGPoint(x: Swift.max(x, value.x),
y: Swift.max(y, value.y))
}
2019-04-15 18:23:02 +02:00
var length: CGFloat {
2019-03-01 19:36:09 +01:00
return sqrt(x * x + y * y)
}
2019-04-15 18:23:02 +02:00
static let unit: CGPoint = CGPoint(x: 1.0, y: 1.0)
2019-02-06 22:00:22 +01:00
2019-04-15 18:23:02 +02:00
static let unitMidpoint: CGPoint = CGPoint(x: 0.5, y: 0.5)
2019-02-06 22:00:22 +01:00
2019-04-15 18:23:02 +02:00
func applyingInverse(_ transform: CGAffineTransform) -> CGPoint {
2019-02-06 22:00:22 +01:00
return applying(transform.inverted())
}
2019-04-15 18:23:02 +02:00
func fuzzyEquals(_ other: CGPoint, tolerance: CGFloat = 0.001) -> Bool {
return (x.fuzzyEquals(other.x, tolerance: tolerance) &&
y.fuzzyEquals(other.y, tolerance: tolerance))
}
2019-04-15 18:23:02 +02:00
static func tan(angle: CGFloat) -> CGPoint {
return CGPoint(x: sin(angle),
y: cos(angle))
}
2019-04-15 18:23:02 +02:00
func clamp(_ rect: CGRect) -> CGPoint {
return CGPoint(x: x.clamp(rect.minX, rect.maxX),
y: y.clamp(rect.minY, rect.maxY))
}
}
2019-03-21 15:26:38 +01:00
// MARK: -
public extension CGSize {
var aspectRatio: CGFloat {
guard self.height > 0 else {
return 0
}
return self.width / self.height
}
var asPoint: CGPoint {
return CGPoint(x: width, y: height)
}
var ceil: CGSize {
return CGSizeCeil(self)
}
2019-02-06 22:00:22 +01:00
}
2019-03-21 15:26:38 +01:00
// MARK: -
2019-02-06 22:00:22 +01:00
public extension CGRect {
2019-04-15 18:23:02 +02:00
var center: CGPoint {
2019-02-06 22:00:22 +01:00
return CGPoint(x: midX, y: midY)
}
2019-04-15 18:23:02 +02:00
var topLeft: CGPoint {
2019-02-06 22:00:22 +01:00
return origin
}
2019-04-15 18:23:02 +02:00
var topRight: CGPoint {
2019-02-26 19:27:54 +01:00
return CGPoint(x: maxX, y: minY)
2019-02-25 23:18:49 +01:00
}
2019-04-15 18:23:02 +02:00
var bottomLeft: CGPoint {
2019-02-26 19:27:54 +01:00
return CGPoint(x: minX, y: maxY)
2019-02-25 23:18:49 +01:00
}
2019-04-15 18:23:02 +02:00
var bottomRight: CGPoint {
2019-02-06 22:00:22 +01:00
return CGPoint(x: maxX, y: maxY)
}
}
2019-03-21 15:26:38 +01:00
// MARK: -
2019-02-06 22:00:22 +01:00
public extension CGAffineTransform {
2019-04-15 18:23:02 +02:00
static func translate(_ point: CGPoint) -> CGAffineTransform {
2019-02-06 22:00:22 +01:00
return CGAffineTransform(translationX: point.x, y: point.y)
}
2019-04-15 18:23:02 +02:00
static func scale(_ scaling: CGFloat) -> CGAffineTransform {
2019-02-06 22:00:22 +01:00
return CGAffineTransform(scaleX: scaling, y: scaling)
}
2019-04-15 18:23:02 +02:00
func translate(_ point: CGPoint) -> CGAffineTransform {
2019-02-06 22:00:22 +01:00
return translatedBy(x: point.x, y: point.y)
}
2019-04-15 18:23:02 +02:00
func scale(_ scaling: CGFloat) -> CGAffineTransform {
2019-02-06 22:00:22 +01:00
return scaledBy(x: scaling, y: scaling)
}
2019-04-15 18:23:02 +02:00
func rotate(_ angleRadians: CGFloat) -> CGAffineTransform {
2019-02-06 22:00:22 +01:00
return rotated(by: angleRadians)
}
2019-02-12 16:03:32 +01:00
}
2019-02-26 18:54:29 +01:00
2019-03-21 15:26:38 +01:00
// MARK: -
2019-02-26 18:54:29 +01:00
public extension UIBezierPath {
2019-04-15 18:23:02 +02:00
func addRegion(withPoints points: [CGPoint]) {
2019-02-26 18:54:29 +01:00
guard let first = points.first else {
owsFailDebug("No points.")
return
}
move(to: first)
for point in points.dropFirst() {
addLine(to: point)
}
addLine(to: first)
}
}
// MARK: -
2019-04-15 18:23:02 +02:00
@objc
public extension UIBarButtonItem {
2019-04-15 18:23:02 +02:00
convenience init(image: UIImage?, style: UIBarButtonItem.Style, target: Any?, action: Selector?, accessibilityIdentifier: String) {
self.init(image: image, style: style, target: target, action: action)
self.accessibilityIdentifier = accessibilityIdentifier
}
2019-04-15 18:23:02 +02:00
convenience init(image: UIImage?, landscapeImagePhone: UIImage?, style: UIBarButtonItem.Style, target: Any?, action: Selector?, accessibilityIdentifier: String) {
self.init(image: image, landscapeImagePhone: landscapeImagePhone, style: style, target: target, action: action)
self.accessibilityIdentifier = accessibilityIdentifier
}
2019-04-15 18:23:02 +02:00
convenience init(title: String?, style: UIBarButtonItem.Style, target: Any?, action: Selector?, accessibilityIdentifier: String) {
self.init(title: title, style: style, target: target, action: action)
self.accessibilityIdentifier = accessibilityIdentifier
}
2019-04-15 18:23:02 +02:00
convenience init(barButtonSystemItem systemItem: UIBarButtonItem.SystemItem, target: Any?, action: Selector?, accessibilityIdentifier: String) {
self.init(barButtonSystemItem: systemItem, target: target, action: action)
self.accessibilityIdentifier = accessibilityIdentifier
}
2019-04-15 18:23:02 +02:00
convenience init(customView: UIView, accessibilityIdentifier: String) {
self.init(customView: customView)
self.accessibilityIdentifier = accessibilityIdentifier
}
}
2021-08-05 06:15:34 +02:00
// MARK: -
@objc
public extension UIButton {
func setTemplateImage(_ templateImage: UIImage?, tintColor: UIColor) {
guard let templateImage = templateImage else {
owsFailDebug("Missing image")
return
}
setImage(templateImage.withRenderingMode(.alwaysTemplate), for: .normal)
self.tintColor = tintColor
}
func setTemplateImageName(_ imageName: String, tintColor: UIColor) {
guard let image = UIImage(named: imageName) else {
owsFailDebug("Couldn't load image: \(imageName)")
return
}
setTemplateImage(image, tintColor: tintColor)
}
}
// MARK: -
@objc
public extension UIImageView {
func setTemplateImage(_ templateImage: UIImage?, tintColor: UIColor) {
guard let templateImage = templateImage else {
owsFailDebug("Missing image")
return
}
self.image = templateImage.withRenderingMode(.alwaysTemplate)
self.tintColor = tintColor
}
func setTemplateImageName(_ imageName: String, tintColor: UIColor) {
guard let image = UIImage(named: imageName) else {
owsFailDebug("Couldn't load image: \(imageName)")
return
}
setTemplateImage(image, tintColor: tintColor)
}
}