Extra 👌🏼
A Swift library with usefull and lightfull extensions for your Cocoa Touch projects.
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Requirements
- iOS 9+
- Xcode 10+, Swift 4.2
Installation
Extra is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "Extra"
# or via subspec :
pod "Extra/UIKit"
pod "Extra/Foundation"
pod "Extra/CoreAnimation"
pod "Extra/Realm"
Content
UI | Utils | Third-party libraries |
---|---|---|
UIApplication | String | Realm |
UICollectionView | Sequence | |
UITableView | ||
UIView | ||
UIImage | ||
UIButton | ||
UILabel | ||
UITextView | ||
UIColor | ||
UIDevice | ||
UIViewController | ||
UINavigationController | ||
CALayer |
Usage
Use the ex
variable on your current type to access to the Extra methods :
UIApplication.ex.hideActivityIndicator()
UIKit extensions 🎨
UIApplication
UIActivityIndicator
accessibility :hideActivityIndicator()
/showActivityIndicator()
isLandscape()
/isPortrait()
quick access
UICollectionView
UICollectionView
currentIndexPathForCenter()
: return the index path of cell displayed at the center(x,y) of the UICollectionViewpublic func scrollVertically(to: IndexPath, accordingTo: IndexPath, animated: Bool = default)
: scroll to an item an be sur to let the other be vivisble if possible
UICollectionViewCell
collectionView
property : return the current UICollectionView related to the cell
UITableView
UITableView
setAndLayoutTableHeaderView(_ headerView: UIView, edges: UIEdgeInsets? = default)
: Configure and set the global table HeaderView and correctly set the frame to the fitting sizelayoutTableHeaderView(_ headerView: UIView, edges: UIEdgeInsets? = default)
: Automatic height for your header. Call it on didLayoutSubviewssetAndLayoutTableFooterView(_ footerView: UIView, edges: UIEdgeInsets? = default)
: Configure and set the global table FooterView and correctly set the frame to the fitting sizelayoutTableFooterView(_ footerView: UIView, edges: UIEdgeInsets? = default)
: Automatic height for your footer. Call it on didLayoutSubviewsscrollToTop(animated: Bool)
: Scrolls the tableView to the top (including its headerView).
UITableViewCell
tableView
property : return the current UITableView related to the cell
UIColor
toImage(size: CGSize = default) -> UIImage?
: Transform the current color to a sizable UIImagefromHexa(_ hexaString: String, alpha: CGFloat = default) -> UIColor?
: Create a color from an hexa string
UIDevice
isPad
/isPhone
quick access
UIImage
from(color: UIColor, size: CGSize, cornerRadius: CGFloat = default) -> UIImage?
: Creates an UIImage from and UIColor This is usefull for your UIButton to set filled background color for states.toBlackAndWhite() -> UIImage?
: Transforms the current image to a new one with theCIColorMonochrome
filter, with black input color.imageRotatedByDegrees(degrees: CGFloat) -> UIImage?
: Correctly rotate your image according to the desired degrees.scaledFilledToSize(size: CGSize) -> UIImage?
: This will resize the UIImage to the destination size This will fill your UIImage (and potentially enlarge it) By this way, this will not produce empty space on top/bottom or left/right sides, the ratio will not change.resizableImageByCenter() -> UIImage
: Simple resize of your image based to it center.cutTile(with rect: CGRect) -> UIImage?
: Cut and return a tile in the image with specified rect.scaledAspectFill(to size: CGSize) -> UIImage?
: Creates a copy of the image simulating aspect fill to specified size
UIButton
configureTitle(with title: String, font: UIFont, color: UIColor, highlightedColor: UIColor? = nil)
: Configures the title of the button in normal and highlighted statesconfigureLeftIcon(with image: UIImage, insets: UIEdgeInsets = .zero)
: Configures left icon of the buttonconfigureBackground(with color: UIColor, highlightedColor: UIColor? = nil)
: Configures background of the button in normal and highlighted states
UILabel
width(fitting containerHeight: CGFloat, string: String, attributes: [NSAttributedStringKey: Any]) -> CGFloat
: Estimates width of a label required to display specified text fitting container height.width(fitting containerHeight: CGFloat, attributedString: NSAttributedString) -> CGFloat
: Estimates width of a label required to display specified text fitting container height.height(fitting containerWidth: CGFloat, string: String, attributes: [NSAttributedStringKey: Any]) -> CGFloat
: Estimates height of a label required to display specified text fitting container width.height(fitting containerWidth: CGFloat, attributedString: NSAttributedString) -> CGFloat
: Estimates height of a label required to display specified text fitting container width.
UITextView
setNoPadding()
: Configures textView without padding.
UINavigationController
popPreviousAndPushViewController(_ controller: UIViewController)
: The effect is like a replacement of the current stack, but just the last one UIViewController.popViewControllers(numberOf: Int, animated: Bool = default)
: Pop the current stack of Navigation Controller by the specified number.
UIView
initXib()
: Use this method in your custom UIView with a specified Xib, to add your xib content at creationinstantiateFromNib() -> UIView
: Create and retrieve the root UIView of your Xib based on the current UIView class nameaddSubview(_ subview: UIView, insets: UIEdgeInsets)
: Add a subview into the current UIView with potential insets.removeAllSubViews()
: Recursively remove all your related subviewssetAllSubviewsHidden(_ hidden: Bool)
: Recursively hide all your related subviewsaddClearToDarkGradient(radius: CGFloat = default, direction: ExtraGradientDirection)
: This will create a gradient into your current UIView (so this is available for UIImageView) Be sure to have a background color dark to have good resultsroundCorners(_ corners: UIRectCorner, radius: CGSize)
: Apply a rounded mask to the current viewaddDashedBorder(color: CGColor, thickness: CGFloat)
: Apply a dashed border to the current viewaddShadowBorder(color: UIColor, size: CGFloat)
: Apply a shadow to all edges except the top border.addBottomDivider(color: UIColor, alpha: CGFloat, widthMultiplier: CGFloat)
: Apply a bottom divider to the current view
UIViewController
topMost() -> UIViewController?
: Returns the current application’s top most view controller.addChildViewController(_ childController: UIViewController, in container: UIView, insets: UIEdgeInsets = default)
: Simply programmatically adding a child view controllerswitchChilds(from originController: UIViewController?, to destinationController: UIViewController, in viewContainer: UIView, duration: TimeInterval = default, transitionOptions: UIViewAnimationOptions = default, completion: ((Bool) -> Void)? = default)
: Switch between child view controllers
Foundation extensions
String
heightConstrained(to width: CGFloat, attributes: [String : Any]? = default) -> CGFloat
: Same as boundingRect(), but simplified !isValidEmail() -> Bool
:[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}
firstCapitalized() -> String
: Capitalize only the first letter of the entire string (localized capitalized)matchesRegex(regex: String) -> Bool
: Check if the string matches to the passed regexbase64() -> String?
: Base-64 encoded string
Sequence
withoutDuplicates() -> [Iterator.Element]
: Returns an array with the content of this sequence after removing all duplicated elements.
Other extensions
CALayer
applySketchShadow()
: Reproduces and applies a shadow that nearly perfectly matches the design according to Zeplin or Sketch parameters.
Realm
💾
DetachableObject
Simply detach an object from Realm into memmory with its entire properties.
Object
For Object
and Realm
classes, use the ex
property to access to the Extra extensions.
Queries
static findFirst<T>(in context: Realm? = nil, object type: T.Type = T.self) -> T? where T: Object
: Finds the first object of a specific typestatic findAll<T>(in context: Realm? = nil, object type: T.Type = T.self) -> Results<T> where T: Object
: Finds all objects of a specific type
Writes
add(in context: Realm? = nil, update: Bool = true) throws
: Simply writes and adds or update your object in Realmstatic update<T>(object: T, in context: Realm? = nil) throws where T: Object
: Adds or updates an object in Realmstatic update<T>(objects: [T], in context: Realm? = nil) throws where T: Object
: Adds or updates many objects in Realm
Delete
static delete<T>(object: T?, from context: Realm? = nil) throws where T: Object & ObjectRelationDeletable
: Delete an object from Realmstatic delete<T>(objects: List<T>?, from context: Realm? = nil) throws where T: Object & ObjectRelationDeletable
: Delete many objects from Realmstatic deleteAll<T>(from context: Realm? = nil, object type: T.Type = T.self) throws where T: Object & ObjectRelationDeletable
: Delete all objects of a specific type from realm
Realm
safeInstance() -> Realm
: Return your [try Realm] instance no-throwable at any timesafeInstance(config: Realm.Configuration) -> Realm
: Return your [try Realm] instance no-throwable at any time froma specific configuration
Results
subscript(safe index: Int) -> T? { get }
: Safety getter object on your resultsex_toArray() -> [T]
: Return results as a sequence Temporary name
RealmSwift.List
ex_toArray() -> [T]
: Return lsit of object as a sequence Temporary name
Author
The iOS Team @Smart&Soft, software agency http://www.smartnsoft.com
License
Extra is available under the MIT license. See the LICENSE file for more info.