Skip to content

Commit

Permalink
Added RTL to view extension. Fixed protect level in view extension.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvorobei committed Jul 23, 2021
1 parent 9c2852b commit b6907dc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
Binary file not shown.
76 changes: 43 additions & 33 deletions Sources/SparrowKit/UIKit/Extensions/UIViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
#if canImport(UIKit) && (os(iOS) || os(tvOS))
import UIKit

public extension UIView {
extension UIView {

/**
SparrowKit: Init `UIView` object with background color.
- parameter backgroundColor: Color which using for background.
*/
convenience init(backgroundColor color: UIColor) {
public convenience init(backgroundColor color: UIColor) {
self.init()
backgroundColor = color
}
Expand All @@ -42,7 +42,7 @@ public extension UIView {
- warning:
If view not added to any controller, return nil.
*/
var viewController: UIViewController? {
open var viewController: UIViewController? {
weak var parentResponder: UIResponder? = self
while parentResponder != nil {
parentResponder = parentResponder!.next
Expand All @@ -58,21 +58,21 @@ public extension UIView {
- parameter subviews: Array of `UIView` objects.
*/
func addSubviews(_ subviews: [UIView]) {
open func addSubviews(_ subviews: [UIView]) {
subviews.forEach { addSubview($0) }
}

/**
SparrowKit: Remove all subviews.
*/
func removeSubviews() {
open func removeSubviews() {
subviews.forEach { $0.removeFromSuperview() }
}

/**
SparrowKit: Take screenshoot of view as `UIImage`.
*/
var screenshot: UIImage? {
open var screenshot: UIImage? {
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, 0)
defer {
UIGraphicsEndImageContext()
Expand All @@ -82,6 +82,16 @@ public extension UIView {
return UIGraphicsGetImageFromCurrentImageContext()
}

/**
SparrowKit: If view has LTR interface.
*/
open var ltr: Bool { effectiveUserInterfaceLayoutDirection == .leftToRight }

/**
SparrowKit: If view has TRL interface.
*/
open var rtl: Bool { effectiveUserInterfaceLayoutDirection == .rightToLeft }

// MARK: - Layout

/**
Expand All @@ -90,7 +100,7 @@ public extension UIView {
- warning:
Fit view can be return zero height. View shoud support it.
*/
func setWidthAndFit(width: CGFloat) {
open func setWidthAndFit(width: CGFloat) {
frame.setWidth(width)
sizeToFit()
}
Expand All @@ -101,7 +111,7 @@ public extension UIView {
- warning:
If current view have not superview, center X is set to zero.
*/
func setXCenter() {
open func setXCenter() {
center.x = (superview?.frame.width ?? 0) / 2
}

Expand All @@ -111,7 +121,7 @@ public extension UIView {
- warning:
If current view have not superview, center Y is set to zero.
*/
func setYCenter() {
open func setYCenter() {
center.y = (superview?.frame.height ?? 0) / 2
}

Expand All @@ -121,7 +131,7 @@ public extension UIView {
- warning:
If current view have not superview, center is set to zero.
*/
func setToCenter() {
open func setToCenter() {
setXCenter()
setYCenter()
}
Expand All @@ -131,7 +141,7 @@ public extension UIView {
/**
SparrowKit: Margins of readable frame.
*/
var readableMargins: UIEdgeInsets {
open var readableMargins: UIEdgeInsets {
let layoutFrame = readableContentGuide.layoutFrame
return UIEdgeInsets(
top: layoutFrame.origin.y,
Expand All @@ -144,21 +154,21 @@ public extension UIView {
/**
SparrowKit: Readable width of current view without horizontal readable margins.
*/
var readableWidth: CGFloat {
open var readableWidth: CGFloat {
return readableContentGuide.layoutFrame.width
}

/**
SparrowKit: Readable height of current view without vertical readable margins.
*/
var readableHeight: CGFloat {
open var readableHeight: CGFloat {
return readableContentGuide.layoutFrame.height
}

/**
SparrowKit: Readable frame of current view without vertical and horizontal readable margins.
*/
var readableFrame: CGRect {
open var readableFrame: CGRect {
let margins = readableMargins
return CGRect.init(x: margins.left, y: margins.top, width: readableWidth, height: readableHeight)
}
Expand All @@ -168,7 +178,7 @@ public extension UIView {
/**
SparrowKit: Width of current view without horizontal layout margins.
*/
var layoutWidth: CGFloat {
open var layoutWidth: CGFloat {
// ver 1
// Depricated becouse sometimes return invalid size
//return layoutMarginsGuide.layoutFrame.width
Expand All @@ -180,7 +190,7 @@ public extension UIView {
/**
SparrowKit: Height of current view without vertical layout margins.
*/
var layoutHeight: CGFloat {
open var layoutHeight: CGFloat {
// ver 1
// Depricated becouse sometimes return invalid size
//return layoutMarginsGuide.layoutFrame.height
Expand All @@ -192,7 +202,7 @@ public extension UIView {
/**
SparrowKit: Frame of current view without horizontal and vertical layout margins.
*/
var layoutFrame: CGRect {
open var layoutFrame: CGRect {
return CGRect.init(x: layoutMargins.left, y: layoutMargins.top, width: layoutWidth, height: layoutHeight)
}

Expand All @@ -202,15 +212,15 @@ public extension UIView {
- warning:
If view not have superview, nothing happen.
*/
func setEqualSuperviewBounds() {
open func setEqualSuperviewBounds() {
guard let superview = self.superview else { return }
frame = superview.bounds
}

/**
SparrowKit: Set view equal frame to superview frame via `autoresizingMask`.
*/
func setEqualSuperviewBoundsWithAutoresizingMask() {
open func setEqualSuperviewBoundsWithAutoresizingMask() {
autoresizingMask = [.flexibleWidth, .flexibleHeight]
}

Expand All @@ -220,7 +230,7 @@ public extension UIView {
- warning:
If view not have superview, constraints will not be added.
*/
func setEqualSuperviewBoundsWithAutoLayout() {
open func setEqualSuperviewBoundsWithAutoLayout() {
guard let superview = self.superview else { return }
translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
Expand All @@ -237,7 +247,7 @@ public extension UIView {
- warning:
If view not have superview, constraints will not be added.
*/
func setEqualSuperviewMarginsWithAutoLayout() {
open func setEqualSuperviewMarginsWithAutoLayout() {
guard let superview = self.superview else { return }
translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
Expand All @@ -253,7 +263,7 @@ public extension UIView {
/**
SparrowKit: Wrapper for layer property `masksToBounds`.
*/
var masksToBounds: Bool {
open var masksToBounds: Bool {
get {
return layer.masksToBounds
}
Expand All @@ -271,7 +281,7 @@ public extension UIView {
- parameter corners: Case of `UIRectCorner`
- parameter radius: Amount of radius.
*/
func roundCorners(_ corners: UIRectCorner = .allCorners, radius: CGFloat) {
open func roundCorners(_ corners: UIRectCorner = .allCorners, radius: CGFloat) {
let maskPath = UIBezierPath(
roundedRect: bounds,
byRoundingCorners: corners,
Expand All @@ -288,14 +298,14 @@ public extension UIView {
- important:
Need call after changed frame. Better leave it in `layoutSubviews` method.
*/
func roundCorners() {
open func roundCorners() {
layer.cornerRadius = min(frame.width, frame.height) / 2
}

/**
SparrowKit: Wrapper for layer property `borderColor`.
*/
var borderColor: UIColor? {
open var borderColor: UIColor? {
get {
guard let color = layer.borderColor else { return nil }
return UIColor(cgColor: color)
Expand All @@ -314,7 +324,7 @@ public extension UIView {
/**
SparrowKit: Wrapper for layer property `borderWidth`.
*/
var borderWidth: CGFloat {
open var borderWidth: CGFloat {
get {
return layer.borderWidth
}
Expand All @@ -331,7 +341,7 @@ public extension UIView {
- parameter offset: Vertical and horizontal offset from center fro shadow.
- parameter opacity: Alpha for shadow view.
*/
func addShadow(ofColor color: UIColor, radius: CGFloat, offset: CGSize, opacity: Float) {
open func addShadow(ofColor color: UIColor, radius: CGFloat, offset: CGSize, opacity: Float) {
layer.shadowColor = color.cgColor
layer.shadowOffset = offset
layer.shadowRadius = radius
Expand All @@ -345,7 +355,7 @@ public extension UIView {
- parameter amount: Amount of paralax effect.
*/
func addParalax(amount: CGFloat) {
open func addParalax(amount: CGFloat) {
motionEffects.removeAll()
let horizontal = UIInterpolatingMotionEffect(keyPath: "center.x", type: .tiltAlongHorizontalAxis)
horizontal.minimumRelativeValue = -amount
Expand All @@ -363,7 +373,7 @@ public extension UIView {
/**
SparrowKit: Remove paralax.
*/
func removeParalax() {
open func removeParalax() {
motionEffects.removeAll()
}

Expand All @@ -375,8 +385,8 @@ public extension UIView {
- parameter duration: Duration of animation.
- parameter completion: Completion when animation ended.
*/
func fadeIn(duration: TimeInterval = 0.3, completion: ((Bool) -> Void)? = nil) {
UIView.animate(withDuration: duration, animations: {
open func fadeIn(duration: TimeInterval = 0.3, completion: ((Bool) -> Void)? = nil) {
UIView.animate(withDuration: duration, delay: .zero, options: [.beginFromCurrentState, .allowUserInteraction], animations: {
self.alpha = 1
}, completion: completion)
}
Expand All @@ -387,8 +397,8 @@ public extension UIView {
- parameter duration: Duration of animation.
- parameter completion: Completion when animation ended.
*/
func fadeOut(duration: TimeInterval = 0.3, completion: ((Bool) -> Void)? = nil) {
UIView.animate(withDuration: duration, animations: {
open func fadeOut(duration: TimeInterval = 0.3, completion: ((Bool) -> Void)? = nil) {
UIView.animate(withDuration: duration, delay: .zero, options: [.beginFromCurrentState, .allowUserInteraction], animations: {
self.alpha = 0
}, completion: completion)
}
Expand Down
2 changes: 1 addition & 1 deletion SparrowKit.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = 'SparrowKit'
s.version = '3.2.0'
s.version = '3.2.1'
s.summary = 'Collection of native Swift extensions to boost your development. Support tvOS and watchOS.'
s.homepage = 'https://github.com/ivanvorobei/SparrowKit'
s.source = { :git => 'https://github.com/ivanvorobei/SparrowKit.git', :tag => s.version }
Expand Down

0 comments on commit b6907dc

Please sign in to comment.