Extra 👌🏼
一个 Swift 库,为您的 Cocoa Touch 项目提供有用且轻量级的扩展。
示例
要运行示例项目,请克隆仓库,然后从 Example 目录首先运行 pod install
。
需求
- iOS 9+
- Xcode 10+, Swift 4.2
安装
Extra 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中
pod "Extra"
# or via subspec :
pod "Extra/UIKit"
pod "Extra/Foundation"
pod "Extra/CoreAnimation"
pod "Extra/Realm"
内容
用户界面 | 实用工具 | 第三方库 |
---|---|---|
应用程序接口 | 字符串 | Realm |
UICollectionView | 序列 | |
UITableView | ||
UIView | ||
UIImage | ||
UIButton | ||
UILabel | ||
UITextView | ||
UIColor | ||
UIDevice | ||
UIViewController | ||
UINavigationController | ||
CALayer |
使用
使用当前类型中的 ex
变量来访问扩展方法
UIApplication.ex.hideActivityIndicator()
🎨
UIKit 扩展UIApplication
UIActivityIndicatorView
可访问性:hideActivityIndicator()
/showActivityIndicator()
isLandscape()
/isPortrait()
快速访问
UICollectionView
UICollectionView
currentIndexPathForCenter()
: 返回 UICollectionView 中中心(x,y)所显示的单元格的索引路径public func scrollVertically(to: IndexPath, accordingTo: IndexPath, animated: Bool = default)
: 滚动到指定条目,并确保尽可能让另一个条目可见
UICollectionViewCell
collectionView
属性:获取与单元格相关的当前 UICollectionView
UITableView
UITableView
setAndLayoutTableHeaderView(_ headerView: UIView, edges: UIEdgeInsets? = default)
: 配置并设置全局表格HeaderView,并正确设置适配大小的framelayoutTableHeaderView(_ headerView: UIView, edges: UIEdgeInsets? = default)
: 为您的头部自动设置高度。在 didLayoutSubviews 时调用它setAndLayoutTableFooterView(_ footerView: UIView, edges: UIEdgeInsets? = default)
: 配置并设置全局表格FooterView,并正确设置适配大小的framelayoutTableFooterView(_ footerView: UIView, edges: UIEdgeInsets? = default)
: 为您的页脚自动设置高度。在 didLayoutSubviews 时调用它scrollToTop(animated: Bool)
: 将 tableView 滚动到顶部(包括其 headerView)。
UITableViewCell
tableView
属性:返回与单元格相关的当前 UITableView
UIColor
toImage(size: CGSize = default) -> UIImage?
: 将当前颜色转换为可缩放的 UIImagefromHexa(_ hexaString: String, alpha: CGFloat = default) -> UIColor?
: 从十六进制字符串创建颜色
UIDevice
isPad
/isPhone
快速访问
UIImage
from(color: UIColor, size: CGSize, cornerRadius: CGFloat = default) -> UIImage?
: 从 UIColor 创建 UIImage,这对于为 states 设置 UIButton 的填充背景颜色很有用。toBlackAndWhite() -> UIImage?
: 将当前图像转换为新的图像,使用CIColorMonochrome
过滤器,黑色输入颜色。imageRotatedByDegrees(degrees: CGFloat) -> UIImage?
: 根据所需度数正确旋转图像。scaledFilledToSize(size: CGSize) -> UIImage?
: 将 UIImage 调整大小到目标大小。这将填充您的 UIImage(可能还会放大)通过这种方式,它不会在顶部/底部或左侧/右侧产生空空间,比例不会改变。resizableImageByCenter() -> UIImage
: 基于图像中心进行简单调整。cutTile(with rect: CGRect) -> UIImage?
: 通过指定 rect 截取并返回图像的瓷砖。scaledAspectFill(to size: CGSize) -> UIImage?
: 创建图像的副本,模拟填充到指定大小。
UIButton
configureTitle(with title: String, font: UIFont, color: UIColor, highlightedColor: UIColor? = nil)
: 配置按钮的标题,常态和高亮状态configureLeftIcon(with image: UIImage, insets: UIEdgeInsets = .zero)
: 配置按钮的左侧图标configureBackground(with color: UIColor, highlightedColor: UIColor? = nil)
: 配置按钮的背景,常态和高亮状态
UILabel
width(fitting containerHeight: CGFloat, string: String, attributes: [NSAttributedStringKey: Any]) -> CGFloat
: 估算用于显示指定文本并适应容器高度的标签的宽度。width(fitting containerHeight: CGFloat, attributedString: NSAttributedString) -> CGFloat
: 估算用于显示指定文本并适应容器高度的标签的宽度。height(fitting containerWidth: CGFloat, string: String, attributes: [NSAttributedStringKey: Any]) -> CGFloat
: 估算用于显示指定文本并适应容器宽度的标签的高度。height(fitting containerWidth: CGFloat, attributedString: NSAttributedString) -> CGFloat
: 估算用于显示指定文本并适应容器宽度的标签的高度。
UITextView
setNoPadding()
: 配置 textView 不带填充。
UINavigationController
popPreviousAndPushViewController(_ controller: UIViewController)
: 效果类似于替换当前栈,但仅替换最后一个 UIViewController。popViewControllers(numberOf: Int, animated: Bool = default)
: 通过指定数量弹出 Navigation Controller 当前的栈。
UIView
initXib()
: 在你的自定义 UIView 中使用此方法带指定 Xib,用于创建时添加 xib 内容instantiateFromNib() -> UIView
: 根据当前的 UIView 类名创建并检索 Xib 的根UIViewaddSubview(_ subview: UIView, insets: UIEdgeInsets)
: 将子视图添加到当前UIView中,并具有可能的填充。removeAllSubViews()
: 递归删除所有相关子视图setAllSubviewsHidden(_ hidden: Bool)
: 递归隐藏所有相关子视图addClearToDarkGradient(radius: CGFloat = default, direction: ExtraGradientDirection)
: 这将在当前UIView中创建渐变(因此适用于UIImageView),确保具有深色背景颜色以获得良好的效果roundCorners(_ corners: UIRectCorner, radius: CGSize)
: 将圆角蒙版应用于当前视图addDashedBorder(color: CGColor, thickness: CGFloat)
: 将虚线边框应用于当前视图addShadowBorder(color: UIColor, size: CGFloat)
: 将阴影应用于所有边缘,除了顶部边框。addBottomDivider(color: UIColor, alpha: CGFloat, widthMultiplier: CGFloat)
: 将底部分隔线应用于当前视图
UIViewController
topMost() -> UIViewController?
: 返回当前应用程序的顶部视图控制器。addChildViewController(_ childController: UIViewController, in container: UIView, insets: UIEdgeInsets = default)
: 简单地通过编程方式添加子视图控制器switchChilds(from originController: UIViewController?, to destinationController: UIViewController, in viewContainer: UIView, duration: TimeInterval = default, transitionOptions: UIViewAnimationOptions = default, completion: ((Bool) -> Void)? = default)
: 在子视图控制器之间切换
Foundation 扩展
String
heightConstrained(to width: CGFloat, attributes: [String : Any]? = default) -> CGFloat
: 与 boundingRect() 相同,但简化了!isValidEmail() -> Bool
:[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}
firstCapitalized() -> String
: 仅将整个字符串的第一个字母大写(本地化大写)matchesRegex(regex: String) -> Bool
: 检查字符串是否匹配传入的正则表达式base64() -> String?
: Base-64编码的字符串
Sequence
withoutDuplicates() -> [Iterator.Element]
: 返回一个数组,其中包含此序列的内容,删除了所有重复的元素。
其他扩展
CALayer
applySketchShadow()
: 根据Zeplin或Sketch的参数,重现并应用几乎完美匹配设计的阴影。
Realm
💾
DetachableObject
简单地将对象从Realm中分离到内存中,并保留其所有属性。
Object
对于Object
和Realm
类,使用ex
属性访问扩展。
查询
static findFirst<T>(in context: Realm? = nil, object type: T.Type = T.self) -> T? where T: Object
: 查找特定类型的第一对象static findAll<T>(in context: Realm? = nil, object type: T.Type = T.self) -> Results<T> where T: Object
: 查找特定类型的所有对象
写入
add(in context: Realm? = nil, update: Bool = true) throws
: 简单地在Realm中写入并添加或更新你的对象static update<T>(object: T, in context: Realm? = nil) throws where T: Object
: 在Realm中添加或更新一个对象static update<T>(objects: [T], in context: Realm? = nil) throws where T: Object
: 在Realm中添加或更新多个对象
删除
static delete<T>(object: T?, from context: Realm? = nil) throws where T: Object & ObjectRelationDeletable
: 从Realm中删除一个对象static delete<T>(objects: List<T>?, from context: Realm? = nil) throws where T: Object & ObjectRelationDeletable
: 从Realm中删除多个对象static deleteAll<T>(from context: Realm? = nil, object type: T.Type = T.self) throws where T: Object & ObjectRelationDeletable
: 从realm中删除特定类型的所有对象
Realm
safeInstance() -> Realm
: 在任何时间无异常地返回你的 [try Realm] 实例safeInstance(config: Realm.Configuration) -> Realm
: 从特定的配置在任何时间无异常地返回你的 [try Realm] 实例
Results
subscript(safe index: Int) -> T? { get }
: 对你的结果对象的安全获取器ex_toArray() -> [T]
: 将结果作为一个序列返回,临时名称
RealmSwift.List
ex_toArray() -> [T]
: 将对象列表作为一个序列返回,临时名称
作者
Smart&Soft 的 iOS 团队,软件代理 http://www.smartnsoft.com
许可证
Extra 可在 MIT 许可证下获得。有关更多信息,请参阅 LICENSE 文件。