经纬仪
实用组件
安装
pod 'Alidade', '~> 5.0.0'
子规格
所有可用功能都可在独立的子规格中找到。
核心
基本扩展,用于基本Swift类型,如:CGRect
、CGPoint
、Sequence
、UIColor
等。例如非常有用的安全索引访问。
guard let model = models[safe: index] else { return }
几何学
一些有用的基本几何概念,如Ray
、Line
或Segment
。
向量
将SIMD向量类型一致性添加到某些CoreGraphics和UI结构中:CGPoint、CGRect、UIEdgeInsets等。还提供了一套有用的操作符集,可以用来操纵基本UIKit/CoreGraphics类型作为多维度向量。例如,CGRect和UIEdgeInsets都是4维向量。这允许客户端执行以下操作
let rect: CGRect
let insets: UIEdgeInsets
let rectWithInsets = rect + insets
let bounds = CGRect([0, 0, 640, 1136])
let size = CGSize([120, 60])
let origin = bounds.midpoint - (size * 0.5).pointValue
let frame = CGRect(origin: origin, size: size)
字符串
计算和缓存String
和NSAttributedString
文本大小。此外,使用NSAttributedString.DocumentType.html
将HTML转换为NSAttributedString
的能力。
UI
为UIView提供一些实用的工具,例如:
func addSubviews(_ subviews: [UIView])
func addSubviews(_ subviews: UIView...)
自定义视图:GradientView
、PathView
设置阴影的访问器
view.shadow = .init(color: shadowColor, blur: 10.0, opacity: 0.5, offset: .zero, path: nil)
Sketch/Zeplin的阴影也提供支持
view.shadow = .sketch(color: shadowColor, alpha: 0.5, bounds: shadowBounds, x: 0.0, y: 10.0, blur: 10.0, spread: 0.0)
UIExtension
最有用的是UIScalable
协议,允许您使用相同比例进行设计工作。您需要从设计设置最小尺寸作为基本值
UI.setBaseWidths([.pad: 768, .phone: 375])
然后您可以使用.ui
扩展来访问调整后的值
let width: CGFloat = 180.ui
如果您有不同的屏幕尺寸设计,还有您自己的独立模块或设计,您可以使用与您模块相关联的Intent
UI.setBaseWidths([.pad: 768, .phone: 320], for: Module.intent)
// then you can access
let width: CGFloat = 180.uiValue(for: Module.intent)
Boxed
用于不可变容器中不可变性质的Box容器:注意!每次将包含Boxed对象的struct通过参数传递给函数时,都会使运行时增加Boxed对象的保留计数
struct SomeData {
let a = 1
var b = 2.0
let c = Boxed(false)
}
let immutable = SomeData()
immutable.a = 2 // is forbidden
var mutable = SomeData()
mutable.b = 2.0 // is valid for mutable instances
// is valid for both cases
immutable.c.value = false
mutable.c.value = true
可关联的
使得使用 ObjC 运行时关联更加容易
private enum SomeClassYouWantToExtendViaObjcAssociationConst {
static var propertyName = 0
}
extension SomeClassYouWantToExtendViaObjcAssociation {
var readwriteValue: PropertyType? {
get { return associated.value(for: &SomeClassYouWantToExtendViaObjcAssociationConst.propertyName) }
set { associated.set(newValue, for: &SomeClassYouWantToExtendViaObjcAssociationConst.propertyName) }
}
}
Flowable
用于就地创建和同时设置对象的惊人而有用的扩展。例如
let label = UILabel {
$0.text = "Text"
$0.numberOfLines = 2
}
FormatterPool
用于创建和缓存不同类型格式化器的工具。例如日期、日期间隔、长度、姓名等。
let date = Date()
let locale = Locale.current
let template = "EEEEEEE, d MMM"
let dateFormat = DateFormatter.dateFormat(fromTemplate: template, options: 0, locale: locale) ?? template
let dateString = DateFormatter.cached(format: dateFormat, locale: locale)
.string(from: date)