|
| 1 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 2 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 3 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 4 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 5 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 6 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 7 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 8 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 9 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 10 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 11 | +// POSSIBILITY OF SUCH DAMAGE. |
| 12 | +// |
| 13 | +// Created by Michael Ong on 2020-05-27. |
| 14 | + |
| 15 | +import UIKit |
| 16 | +import FlexLayout |
| 17 | +import PinLayout |
| 18 | + |
| 19 | +class YogaExampleFView: UIView { |
| 20 | + fileprivate let rootFlexContainer = UIView() |
| 21 | + |
| 22 | + init() { |
| 23 | + super.init(frame: .zero) |
| 24 | + backgroundColor = .white |
| 25 | + |
| 26 | + let imageView = UIView() |
| 27 | + imageView.backgroundColor = .flexLayoutColor |
| 28 | + |
| 29 | + // Yoga's Android example |
| 30 | + rootFlexContainer.flex.padding(20).direction(.column).define { |
| 31 | + |
| 32 | + $0.addItem().direction(.row).alignItems(.baseline).marginBottom(20).define { |
| 33 | + |
| 34 | + let label = UILabel() |
| 35 | + |
| 36 | + label.font = .systemFont(ofSize: 20) |
| 37 | + label.text = "Leading Font" |
| 38 | + |
| 39 | + $0.addItem(label).marginRight(5).markDirty() |
| 40 | + |
| 41 | + let info = UILabel() |
| 42 | + |
| 43 | + info.font = .systemFont(ofSize: 10) |
| 44 | + info.text = "Trailing Font" |
| 45 | + |
| 46 | + $0.addItem(info).markDirty() |
| 47 | + } |
| 48 | + |
| 49 | + $0.addItem().direction(.row).alignItems(.baseline).marginBottom(20).define { |
| 50 | + |
| 51 | + let label = UILabel() |
| 52 | + |
| 53 | + label.font = .systemFont(ofSize: 20) |
| 54 | + label.text = "Leading Multiline\nText Oh yeah!" |
| 55 | + label.numberOfLines = 2 |
| 56 | + |
| 57 | + $0.addItem(label).marginRight(5).markDirty() |
| 58 | + |
| 59 | + let info = UILabel() |
| 60 | + |
| 61 | + info.font = .systemFont(ofSize: 10) |
| 62 | + info.text = "Trailing Font" |
| 63 | + |
| 64 | + $0.addItem(info).markDirty() |
| 65 | + } |
| 66 | + |
| 67 | + $0.addItem().direction(.row).alignItems(.baseline).marginBottom(20).define { |
| 68 | + |
| 69 | + let label = UILabel() |
| 70 | + |
| 71 | + label.font = .systemFont(ofSize: 12) |
| 72 | + label.text = "Name:" |
| 73 | + label.numberOfLines = 2 |
| 74 | + |
| 75 | + $0.addItem(label).marginRight(5).markDirty() |
| 76 | + |
| 77 | + let info = UITextField() |
| 78 | + |
| 79 | + info.font = .systemFont(ofSize: 16) |
| 80 | + info.placeholder = "Enter name here!" |
| 81 | + |
| 82 | + $0.addItem(info).markDirty() |
| 83 | + } |
| 84 | + |
| 85 | + $0.addItem().direction(.row).alignItems(.baseline).marginBottom(20).define { |
| 86 | + |
| 87 | + let label = UILabel() |
| 88 | + |
| 89 | + label.font = .systemFont(ofSize: 12) |
| 90 | + label.text = "Age:" |
| 91 | + label.numberOfLines = 2 |
| 92 | + |
| 93 | + $0.addItem(label).marginRight(5).markDirty() |
| 94 | + |
| 95 | + let info = UITextField() |
| 96 | + |
| 97 | + info.borderStyle = .roundedRect |
| 98 | + |
| 99 | + info.font = .systemFont(ofSize: 16) |
| 100 | + info.placeholder = "Enter name here!" |
| 101 | + |
| 102 | + $0.addItem(info).markDirty() |
| 103 | + } |
| 104 | + |
| 105 | + $0.addItem().direction(.row).alignItems(.baseline).marginBottom(20).define { |
| 106 | + |
| 107 | + let label = UILabel() |
| 108 | + |
| 109 | + label.font = .systemFont(ofSize: 12) |
| 110 | + label.text = "Status:" |
| 111 | + label.numberOfLines = 2 |
| 112 | + |
| 113 | + $0.addItem(label).marginRight(5).markDirty() |
| 114 | + |
| 115 | + let info = UITextField() |
| 116 | + |
| 117 | + info.borderStyle = .line |
| 118 | + |
| 119 | + info.font = .systemFont(ofSize: 16) |
| 120 | + info.placeholder = "Enter name here!" |
| 121 | + |
| 122 | + $0.addItem(info).markDirty() |
| 123 | + } |
| 124 | + |
| 125 | + $0.addItem().direction(.row).alignItems(.baseline).marginBottom(20).define { |
| 126 | + |
| 127 | + let label = UILabel() |
| 128 | + |
| 129 | + label.font = .systemFont(ofSize: 12) |
| 130 | + label.text = "Gender:" |
| 131 | + label.numberOfLines = 2 |
| 132 | + |
| 133 | + $0.addItem(label).marginRight(5).markDirty() |
| 134 | + |
| 135 | + let info = UITextField() |
| 136 | + |
| 137 | + info.borderStyle = .bezel |
| 138 | + |
| 139 | + info.font = .systemFont(ofSize: 16) |
| 140 | + info.placeholder = "Enter name here!" |
| 141 | + |
| 142 | + $0.addItem(info).markDirty() |
| 143 | + } |
| 144 | + |
| 145 | + $0.addItem().direction(.row).alignItems(.baseline).marginBottom(20).define { |
| 146 | + |
| 147 | + let label = UILabel() |
| 148 | + |
| 149 | + label.font = .systemFont(ofSize: 12) |
| 150 | + label.text = "About:" |
| 151 | + label.numberOfLines = 2 |
| 152 | + |
| 153 | + $0.addItem(label).marginRight(5).markDirty() |
| 154 | + |
| 155 | + let info = UITextView() |
| 156 | + |
| 157 | + info.font = .systemFont(ofSize: 16) |
| 158 | + info.text = "Place text here!" |
| 159 | + |
| 160 | + $0.addItem(info).grow(1).height(200).markDirty() |
| 161 | + } |
| 162 | + } |
| 163 | + addSubview(rootFlexContainer) |
| 164 | + } |
| 165 | + |
| 166 | + required init?(coder aDecoder: NSCoder) { |
| 167 | + super.init(coder: aDecoder) |
| 168 | + } |
| 169 | + |
| 170 | + override func layoutSubviews() { |
| 171 | + super.layoutSubviews() |
| 172 | + |
| 173 | + // Layout the flexbox container using PinLayout |
| 174 | + // NOTE: Could be also layouted by setting directly rootFlexContainer.frame |
| 175 | + rootFlexContainer.pin.top(pin.safeArea).horizontally(pin.safeArea).height(300) |
| 176 | + |
| 177 | + // Then let the flexbox container layout itself |
| 178 | + rootFlexContainer.flex.layout() |
| 179 | + } |
| 180 | +} |
0 commit comments