框架 1.0.0

框架 1.0.0

测试测试
语言语言 SwiftSwift
许可 MIT
发布最后发布2023年2月
SPM支持SPM

Maciek Grzybowski维护。



框架 1.0.0

framing-logo

框架

Swifty风格的框架布局定义。

安装

SPM

.package(url: "https://github.com/ncreated/Framing.git", from: "1.0.0"),

CocoaPods

pod 'Framing'

什么是框架

框架是一个小巧的框架,用于以更Swifty的方式定义视图框架。将其视为一个简单的对CGRect的包装器,该包装器可以在定位视图框架时节省您大量时间。

let background = Frame(width: 300, height: 300)

let bottomLine1 = Frame(width: 300, height: 20)
    .putInside(background, alignTo: .bottomCenter)

let bottomLine2 = Frame(width: 300, height: 30)
    .putAbove(bottomLine1)

let F1 = Frame(width: 50, height: 180)
    .putInside(background, alignTo: .middleLeft)
    .offsetBy(x: 90, y: -10)

let F2 = Frame(width: 70, height: 50)
    .putOnRight(of: F1, alignTo: .top)

let F3 = F2.offsetBy(y: 70)

Frame有一个.rect属性,它返回一个可以用于UIViewCGRect(例如:greenView.frame = bottomLine2.rect)。

framing-logo-framed

框架不是什么?

它不是动物。🐷或者UI/布局框架。它没有在任何地方导入UIKit

你可以使用Framing做什么?

这将返回沿两个方向移动了10pt的框架。

frame.offsetBy(x: 10, y: 10)

这将返回在每个方向上缩小了5pt的框架。

frame.inset(top: 5, left: 5, bottom: 5, right: 5)

这将返回放置在基础框架之上的框架。

frame.putAbove(base, alignTo: .left)
frame.putAbove(base, alignTo: .right)
frame.putAbove(base, alignTo: .center)
frame.putBelow(..., alignTo: ...) // ...

左侧/右侧定位也是如此。

frame.putOnLeft(of: base, alignTo: .top)
frame.putOnLeft(of: base, alignTo: .bottom)
frame.putOnLeft(of: base, alignTo: .middle)
frame.putOnRight(of: ..., alignTo: ...) // ...

这将返回放置在基础框架内部的框架。

frame.putInside(base, alignTo: .topLeft)
frame.putInside(base, alignTo: .topCenter)
...
frame.putInside(base, alignTo: .middleCenter) // centers `frame` inside `base`
...

这将框架分成5列,并返回第2列的框架。

frame.divideIntoEqual(columns: 5, take: 1)

对行也适用。

frame.divideIntoEqual(rows: 5, take: 1)

还可以有条件地应用变换。

let shouldBeOffsetted: Bool = ...
frame.if(condition: shouldBeOffsetted,
         then: { $0.offsetBy(x: 10).inset(top: 5) },
         else: { $0.inset(top: 5) })
         
// Or simpler:
frame.inset(top: 5)
     .if(shouldBeOffsetted) { $0.offsetBy(x: 10) }

缺少某些API?提交一个Pull Request或填写问题。

许可证

Framing在MIT许可证下发布。有关更多详细信息,请参阅LICENSE文件。