Skip to content

Commit 2bdbb79

Browse files
author
Luc Dion
authored
Merge pull request #10 from lucdion/update_sample
Update sample
2 parents 1dd4858 + 4d184f4 commit 2bdbb79

File tree

3 files changed

+126
-28
lines changed

3 files changed

+126
-28
lines changed

Example/FlexLayoutSample/UI/Examples/RaywenderlichTutorial/RaywenderlichTutorialView.swift

+19-20
Original file line numberDiff line numberDiff line change
@@ -189,39 +189,38 @@ class RayWenderlichTutorialView: BaseView {
189189
}
190190

191191
fileprivate func showActionViewFor(imageName: String, text: String) -> UIView {
192-
let actionView = UIView(frame: .zero)
193-
actionView.flex.alignItems(.center).width(50).marginRight(20.0)
194-
195-
let actionButton = UIButton(type: .custom)
196-
actionButton.setImage(UIImage(named: imageName), for: .normal)
197-
actionButton.flex.padding(10)
198-
actionView.addSubview(actionButton)
192+
let actionView = UIView()
199193

200-
let actionLabel = showLabelFor(text: text)
201-
actionView.addSubview(actionLabel)
194+
actionView.flex.alignItems(.center).width(50).marginRight(20.0).define { (flex) in
195+
let actionButton = UIButton(type: .custom)
196+
actionButton.setImage(UIImage(named: imageName), for: .normal)
197+
flex.addItem(actionButton).padding(10)
198+
199+
let actionLabel = showLabelFor(text: text)
200+
flex.addItem(actionLabel)
201+
}
202202

203203
return actionView
204204
}
205205

206206
fileprivate func showTabBarFor(text: String, selected: Bool) -> UIView {
207-
let tabView = UIView(frame: .zero)
208-
tabView.flex.alignItems(.center).marginRight(20)
209-
210207
let tabLabelFont = selected ? UIFont.boldSystemFont(ofSize: 14.0) : UIFont.systemFont(ofSize: 14.0)
211208

212209
#if swift(>=4)
213-
let fontSize = text.size(withAttributes: [NSAttributedStringKey.font: tabLabelFont])
210+
let labelSize = text.size(withAttributes: [NSAttributedStringKey.font: tabLabelFont])
214211
#else
215-
let fontSize = text.size(attributes: [NSFontAttributeName: tabLabelFont])
212+
let labelSize = text.size(attributes: [NSFontAttributeName: tabLabelFont])
216213
#endif
217214

218-
let tabSelectionView = UIView(frame: CGRect(x: 0, y: 0, width: fontSize.width, height: 3))
219-
tabSelectionView.flex.width(fontSize.width).marginBottom(5)
220-
tabSelectionView.backgroundColor = selected ? .red : .clear
221-
tabView.addSubview(tabSelectionView)
215+
let tabView = UIView()
222216

223-
let tabLabel = showLabelFor(text: text, font: tabLabelFont)
224-
tabView.addSubview(tabLabel)
217+
tabView.flex.alignItems(.center).marginRight(20).define { (flex) in
218+
let tabSelectionView = UIView()
219+
flex.addItem(tabSelectionView).width(labelSize.width).height(3).marginBottom(5).backgroundColor(selected ? .red : .clear)
220+
221+
let tabLabel = showLabelFor(text: text, font: tabLabelFont)
222+
flex.addItem(tabLabel)
223+
}
225224

226225
return tabView
227226
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ FlexLayout automatically includes the UIView when:
655655
**Method:**
656656

657657
* **`markDirty()`**
658-
Yoga is so highly optimized, that flex item are layouted only when a flex property is changed and when flex container size change. In the event that you want to force FlexLayout to do a layout of a flex item, you can mark it as dirty using `markDirty()`.
658+
The framework is so highly optimized, that flex item are layouted only when a flex property is changed and when flex container size change. In the event that you want to force FlexLayout to do a layout of a flex item, you can mark it as dirty using `markDirty()`.
659659

660660
Dirty flag propagates to the root of the flexbox tree ensuring that when any item is invalidated its whole subtree will be re-calculated.
661661

Sources/FlexLayout.swift

+106-7
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,49 @@ public class Flex {
7676
//
7777
// Creation / definition
7878
//
79+
80+
/// This method adds a flex item (UIView) to a flex container. Internally the methods adds the UIView has subviews and enables flexbox.
81+
///
82+
/// - Returns: The added view flex interface
7983
@discardableResult
8084
public func addItem() -> Flex {
8185
let view = UIView()
8286
return addItem(view)
8387
}
8488

89+
90+
/// This method is similar to `addItem(: UIView)` except that it also creates the flex item's UIView. Internally the method creates an
91+
/// UIView, adds it has subviews and enables flexbox. This is useful to add a flex item/container easily when you don't need to refer to it later.
92+
///
93+
/// - Parameter view: view to add to the flex container
94+
/// - Returns: The added view flex interface
8595
@discardableResult
8696
public func addItem(_ view: UIView) -> Flex {
8797
self.view.addSubview(view)
8898
return view.flex
8999
}
90100

101+
102+
/// This method is used to structure your code so that it matches the flexbox structure. The method has a closure parameter with a
103+
/// single parameter called `flex`. This parameter is in fact, the view's flex interface, it can be used to adds other flex items
104+
/// and containers.
105+
///
106+
/// - Parameter closure:
107+
/// - Returns: Flex interface
91108
@discardableResult
92109
public func define(_ closure: (_ flex: Flex) -> Void) -> Flex {
93110
closure(self)
94111
return self
95112
}
96113

114+
115+
/// The framework is so highly optimized, that flex item are layouted only when a flex property is changed and when flex container
116+
/// size change. In the event that you want to force FlexLayout to do a layout of a flex item, you can mark it as dirty
117+
/// using `markDirty()`.
118+
///
119+
/// Dirty flag propagates to the root of the flexbox tree ensuring that when any item is invalidated its whole subtree will be re-calculated
120+
///
121+
/// - Returns: Flex interface
97122
@discardableResult
98123
public func markDirty() -> Flex {
99124
view.yoga.markDirty()
@@ -103,12 +128,20 @@ public class Flex {
103128
//
104129
// Layout / intrinsicSize/ sizeThatFits
105130
//
131+
106132
public enum LayoutMode {
133+
/// This is the default mode when no parameter is specified. Children are layouted **inside** the container's size (width and height).
107134
case fitContainer
135+
/// In this mode, children are layouted **using only the container's width**. The container's height will be adjusted to fit the flexbox's children
108136
case adjustHeight
137+
/// In this mode, children are layouted **using only the container's height**. The container's width will be adjusted to fit the flexbox's children
109138
case adjustWidth
110139
}
111140

141+
142+
/// The method layout the flex container's children
143+
///
144+
/// - Parameter mode: specify the layout mod (LayoutMode).
112145
public func layout(mode: LayoutMode = .fitContainer) {
113146
if case .fitContainer = mode {
114147
view.yoga.applyLayout(preservingOrigin: true)
@@ -117,30 +150,50 @@ public class Flex {
117150
}
118151
}
119152

153+
/// This property controls dynamically if a flexbox's UIView is included or not in the flexbox layouting. When a
154+
/// flexbox's UIView is excluded, FlexLayout won't layout the view and its children views.
120155
public var isIncludedInLayout: Bool = true {
121156
didSet {
122157
view.yoga.isIncludedInLayout = isIncludedInLayout
123158
}
124159
}
125160

161+
/// This property controls dynamically if a flexbox's UIView is included or not in the flexbox layouting. When a
162+
/// flexbox's UIView is excluded, FlexLayout won't layout the view and its children views.
163+
///
164+
/// - Parameter included: true to layout the view
165+
/// - Returns:
126166
@discardableResult
127167
public func isIncludedInLayout(_ included: Bool) -> Flex {
128168
self.isIncludedInLayout = included
129169
return self
130170
}
131171

172+
/// Item natural size, considering only properties of the view itself. Independent of the item frame.
132173
public var intrinsicSize: CGSize {
133174
return view.yoga.intrinsicSize
134175
}
135176

177+
/// Returns the item size when layouted in the specified frame size
178+
///
179+
/// - Parameter size: frame size
180+
/// - Returns: item size
136181
public func sizeThatFits(size: CGSize) -> CGSize {
137182
return view.yoga.calculateLayout(with: size)
138183
}
139184

140185
//
141186
// direction, wrap, flow
142187
//
188+
189+
/// The `direction` property establishes the main-axis, thus defining the direction flex items are placed in the flex container.
143190
///
191+
/// The `direction` property specifies how flex items are laid out in the flex container, by setting the direction of the flex
192+
/// container’s main axis. They can be laid out in two main directions, like columns vertically or like rows horizontally.
193+
///
194+
/// Note that row and row-reverse are affected by the layout direction (see `layoutDirection` property) of the flex container.
195+
/// If its text direction is LTR (left to right), row represents the horizontal axis oriented from left to right, and row-reverse
196+
/// from right to left; if the direction is rtl, it's the opposite.
144197
///
145198
/// - Parameter value: Default value is .column
146199
@discardableResult
@@ -149,7 +202,7 @@ public class Flex {
149202
return self
150203
}
151204

152-
///
205+
/// The `wrap` property controls whether the flex container is single-lined or multi-lined, and the direction of the cross-axis, which determines the direction in which the new lines are stacked in.
153206
///
154207
/// - Parameter value: Default value is .noWrap
155208
@discardableResult
@@ -158,6 +211,13 @@ public class Flex {
158211
return self
159212
}
160213

214+
215+
/// Direction defaults to Inherit on all nodes except the root which defaults to LTR. It is up to you to detect the
216+
/// user’s preferred direction (most platforms have a standard way of doing this) and setting this direction on the
217+
/// root of your layout tree.
218+
///
219+
/// - Parameter value: new LayoutDirection
220+
/// - Returns:
161221
@discardableResult
162222
public func layoutDirection(_ value: LayoutDirection) -> Flex {
163223
// WORK IN PROGRESS :-)
@@ -180,7 +240,9 @@ public class Flex {
180240
// justity, alignment / position
181241
//
182242

183-
///
243+
/// The `justifyContent` property defines the alignment along the main-axis of the current line of the flex container.
244+
/// It helps distribute extra free space leftover when either all the flex items on a line have reached their maximum
245+
/// size. For example, if children are flowing vertically, `justifyContent` controls how they align vertically.
184246
///
185247
/// - Parameter value: Default value is .start
186248
@discardableResult
@@ -189,7 +251,8 @@ public class Flex {
189251
return self
190252
}
191253

192-
///
254+
/// The align-content property aligns a flex container’s lines within the flex container when there is extra space
255+
/// in the cross-axis, similar to how justifyContent aligns individual items within the main-axis.
193256
///
194257
/// - Parameter value: Default value is .start
195258
@discardableResult
@@ -198,7 +261,9 @@ public class Flex {
198261
return self
199262
}
200263

201-
///
264+
/// The `alignItems` property controls how a child aligns in the cross direction, overriding the `alignItems`
265+
/// of the parent. For example, if children are flowing vertically, `alignSelf` will control how the flex item
266+
/// will align horizontally.
202267
///
203268
/// - Parameter value: Default value is .auto
204269
@discardableResult
@@ -207,7 +272,9 @@ public class Flex {
207272
return self
208273
}
209274

210-
///
275+
/// The `alignItems` property defines how flex items are laid out along the cross axis on the current line.
276+
/// Similar to `justifyContent` but for the cross-axis (perpendicular to the main-axis). For example, if
277+
/// children are flowing vertically, `alignItems` controls how they align horizontally.
211278
///
212279
/// - Parameter value: Default value is .stretch
213280
@discardableResult
@@ -216,7 +283,7 @@ public class Flex {
216283
return self
217284
}
218285

219-
///
286+
/// The position property tells Flexbox how you want your item to be positioned within its parent.
220287
///
221288
/// - Parameter value: Default value is .relative
222289
@discardableResult
@@ -238,7 +305,9 @@ public class Flex {
238305
// grow / shrink / basis
239306
//
240307

241-
///
308+
/// The `grow` property defines the ability for a flex item to grow if necessary. It accepts a unitless value
309+
/// that serves as a proportion. It dictates what amount of the available space inside the flex container the
310+
/// item should take up.
242311
///
243312
/// - Parameter value: Default value is 0
244313
@discardableResult
@@ -247,7 +316,14 @@ public class Flex {
247316
return self
248317
}
249318

319+
/// It specifies the "flex shrink factor", which determines how much the flex item will shrink relative to the
320+
/// rest of the flex items in the flex container when there isn't enough space on the main-axis.
250321
///
322+
/// When omitted, it is set to 0 and the flex shrink factor is multiplied by the flex `basis` when distributing
323+
/// negative space.
324+
///
325+
/// A shrink value of 0 keeps the view's size in the main-axis direction. Note that this may cause the view to
326+
/// overflow its flex container.
251327
///
252328
/// - Parameter value: Default value is 1
253329
@discardableResult
@@ -256,7 +332,11 @@ public class Flex {
256332
return self
257333
}
258334

335+
/// This property takes the same values as the width and height properties, and specifies the initial size of the
336+
/// flex item, before free space is distributed according to the grow and shrink factors.
259337
///
338+
/// Specifying `nil` set the basis as `auto`, which means the length is equal to the length of the item. If the
339+
/// item has no length specified, the length will be according to its content.
260340
///
261341
/// - Parameter value: Default value is 0
262342
@discardableResult
@@ -361,12 +441,25 @@ public class Flex {
361441
return self
362442
}
363443

444+
445+
/// AspectRatio is a property introduced by Yoga that don't exist in CSS. AspectRatio solves the problem of knowing
446+
/// one dimension of an element and an aspect ratio, this is very common when it comes to images, videos, and other
447+
/// media types. AspectRatio accepts any floating point value > 0, the default is undefined.
448+
///
449+
/// - Parameter value:
450+
/// - Returns:
364451
@discardableResult
365452
public func aspectRatio(_ value: CGFloat) -> Flex {
366453
view.yoga.aspectRatio = value
367454
return self
368455
}
369456

457+
/// AspectRatio is a property introduced by Yoga that don't exist in CSS. AspectRatio solves the problem of knowing
458+
/// one dimension of an element and an aspect ratio, this is very common when it comes to images, videos, and other
459+
/// media types. AspectRatio accepts any floating point value > 0, the default is undefined.
460+
///
461+
/// - Parameter value:
462+
/// - Returns:
370463
@discardableResult
371464
public func aspectRatio(of imageView: UIImageView) -> Flex {
372465
if let imageSize = imageView.image?.size {
@@ -575,6 +668,12 @@ public class Flex {
575668
//
576669
// Visual properties
577670
//
671+
672+
673+
/// Set the view background color.
674+
///
675+
/// - Parameter color: new color
676+
/// - Returns: flex interface
578677
@discardableResult
579678
public func backgroundColor(_ color: UIColor) -> Flex {
580679
view.backgroundColor = color

0 commit comments

Comments
 (0)