Flexbox in Swift, using Facebook's css-layout.

Related tags

Layout SwiftBox
Overview

SwiftBox

A Swift wrapper around Facebook's implementation of CSS's flexbox.

Example

let parent = Node(size: CGSize(width: 300, height: 300),
                  childAlignment: .Center,
                  direction: .Row,
                  children: [
    Node(flex: 75,
         margin: Edges(left: 10, right: 10),
         size: CGSize(width: 0, height: 100)), 
    Node(flex: 15,
         margin: Edges(right: 10),
         size: CGSize(width: 0, height: 50)),
    Node(flex: 10,
         margin: Edges(right: 10),
         size: CGSize(width: 0, height: 180)),
])

let layout = parent.layout()
println(layout)

//{origin={0.0, 0.0}, size={300.0, 300.0}}
//	{origin={10.0, 100.0}, size={195.0, 100.0}}
//	{origin={215.0, 125.0}, size={39.0, 50.0}}
//	{origin={264.0, 60.0}, size={26.0, 180.0}}

Alternatively, you could apply the layout to a view hierarchy (after ensuring Auto Layout is off):

layout.apply(someView)

See SwiftBoxDemo for a demo.

Comments
  • Support Swift 2.0

    Support Swift 2.0

    There are a few issues with compiling SwiftBox in Xcode 7 (mostly around Printable and DebugPrintable no longer being around). Would it be possible to add a Swift 2.0-compatible branch? Thanks!

    opened by mcudich 3
  • Example in README doesn't produce indicated output

    Example in README doesn't produce indicated output

    If you run the example as given, you don't get the indicated output, because the default direction for a Node is ".Column" instead of ".Row".

    opened by michaelseanhansen 1
  • Using it in TableView

    Using it in TableView

    Hello Josh!

    Cool project, first step in bringing technologies from React Native to real native code, without js! I wonder if this can be used to calculate layout of reusable cells in table view. It seems to be nice fit there since you have to provide height of cell before you provide actual cell to table view. Also, it enables easy caching of cells layout and simple reuse model where views and layout of views already separated from each other.

    My question is there any way to calculate height with fixed width? Cells can have different heights that depends on content size (text size in my case) so in first we have to have some way to provide information about text size then calculate height and other stuff then apply in to view hierarchy.

    As I see I should calculate text size by hand, then create Node hierarchy with that sizes and then ask about final height. Any thoughts how can it be?

    opened by ikashkuta 1
  • NodeImpl not recognised

    NodeImpl not recognised

    Hi,

    I am getting

    Use of undeclared type 'NodeImpl'

    in my project. I have copied all the files and I am able to navigate to NodeImpl file through xcode. Not sure whats causing the error.

    Please help me.

    Thanks, Kanishka

    opened by kanishkatn 0
  • Position: absolute?

    Position: absolute?

    css-layout has support for position: absolute, i.e. elements that don't participate in the flexbox layout and instead have an explicit position. I don’t see this option exposed anywhere in the SwiftBox API. Am I missing something, or is this just not implemented yet?

    opened by steadicat 0
  • Layout wasn't returning correct width/height when child nodes get a bit more complicated

    Layout wasn't returning correct width/height when child nodes get a bit more complicated

    Here's the setup I encountered issue

    let imageText = Node(
        direction: .Column,
        padding: Edges(top: 20, bottom: 20),
        children: strings.map { (text : String) -> Node in
            let image = Node(
                size: CGSizeMake(100, 100)
            )
            let text = Node(
                measure: { w in
                    println("\(w)") // Nan
                    label.text = text
                    label.preferredMaxLayoutWidth = w
                    return label.sizeThatFits(CGSizeMake(w, CGFloat.max))
            })
            let row = Node(
                direction: Direction.Row,
                children:[image, text]
            )
            return row
        }
    )
    println(imageText.layout(maxWidth: 375).frame)  // (0.0, 0.0, 7704.0, 240.0)
    

    Notice the width wasn't obeying the maxWidth we've put into our layout.

    So Basically I want to implement something like this: ios simulator screen shot 12 jul 2015 1 49 25 pm

    I had some sample data setup here:

    let strings : [String] = [
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ex dolor, imperdiet vel tellus sollicitudin, ultrices maximus nunc. Ut non vehicula magna. Etiam sit amet varius lectus, et luctus dui. Sed eget blandit turpis. Nullam laoreet at felis non fermentum. Integer elit sem, tempus eget ex non, mattis porta lorem. Sed ut quam vel nibh luctus bibendum quis sed mi.",
        "Fusce scelerisque rhoncus elementum. Maecenas ultricies est ex, ut varius mauris cursus at. Maecenas sollicitudin orci posuere, ultricies lacus vel, scelerisque nibh. Nullam a turpis a urna elementum posuere. Integer porttitor vestibulum urna lobortis tincidunt. Aliquam consectetur sem metus, et laoreet libero rutrum nec. Donec sed justo dapibus, mollis nibh volutpat, sollicitudin sem. Suspendisse tempor, velit ac ullamcorper pretium, sapien risus molestie enim, aliquam cursus ex massa a nisl. Duis gravida, diam eget rutrum sagittis, dui est malesuada mi, a dapibus eros odio sit amet tellus. Nunc porttitor nunc nec massa mollis, et pellentesque est egestas. Vivamus venenatis dignissim massa in euismod. Vestibulum rutrum ex a interdum varius. Donec convallis gravida dui eget viverra. Suspendisse tempor, lorem sit amet consequat mattis, libero ex dictum quam, eget iaculis felis velit eu nunc. Morbi eros lectus, aliquet at pulvinar sed, tempor malesuada elit. Integer blandit gravida felis.",
        ]
    
    // Also setting up a label to calculate the size
    let label = UILabel()
    label.numberOfLines = 0
    

    I tried a simpler setup and works as expected:

    let textOnly = Node(
        direction: .Column,
        padding: Edges(top: 20, bottom: 20),
        children: strings.map { (text : String) -> Node in
            let text = Node(
                measure: { w in
                    println("\(w)") // 375
                    label.text = text
                    label.preferredMaxLayoutWidth = w
                    return label.sizeThatFits(CGSizeMake(w, CGFloat.max))
            })
            return text
        }
    )
    
    println(textOnly.layout(maxWidth: 375).frame) // (0.0, 0.0, 375.0, 669.0)
    

    So is there something wrong with my setup or it's a bug that I encountered? Thanks for helping out!

    opened by jamztang 6
Releases(0.2.1)
Owner
Josh Abernathy
Josh Abernathy
An flexbox layout aimed at easy to use, which depend on Yoga.

DDFlexbox A flexbox framework for easy using. Install pod 'DDFlexbox' Template install Recommend using templates to create flexbox views. cd Script/

Daniel 12 Mar 23, 2022
Yoga is a cross-platform layout engine which implements Flexbox.

Yoga Building Yoga builds with buck. Make sure you install buck before contributing to Yoga. Yoga's main implementation is in C++, with bindings to su

Meta 15.8k Jan 9, 2023
FlexLayout adds a nice Swift interface to the highly optimized facebook/yoga flexbox implementation. Concise, intuitive & chainable syntax.

FlexLayout adds a nice Swift interface to the highly optimized Yoga flexbox implementation. Concise, intuitive & chainable syntax. Flexbox is an incre

layoutBox 1.7k Dec 30, 2022
Zelda 是一个支持链式语法的 FlexBox 布局库,是针对 YogaKit 的二次封装

Zelda 是一个支持链式语法的 FlexBox 布局库,是针对 YogaKit 的二次封装,可以快速的让 iOS 原生开发人员使用 FlexBox 技术进行 UI 布局。

饼 4 Aug 24, 2021
An awesome Swift CSS DSL library using result builders.

An awesome Swift CSS DSL library using result builders.

Binary Birds 57 Nov 21, 2022
AppStoreClone - Understanding the complex layout of app store using UICompositional layout in swift

AppStoreClone Understanding the complex layout of app store using UICompositiona

Dheeraj Kumar Sharma 8 Dec 28, 2022
Auto Layout (and manual layout) in one line.

Auto Layout (and manual layout) in one line. Quick Look view.bb.centerX().below(view2).size(100) It’s equivalent to iOS 9 API: view.centerXAnchor.cons

Javier Zhang 74 Oct 19, 2022
Auto Layout made easy with the Custom Layout.

Auto Layout made easy with the Custom Layout. Getting started CocoaPods CocoaPods is a dependency manager for Cocoa projects. You can install it with

Malith Nadeeshan 1 Jan 16, 2022
SuperLayout is a Swift library that makes using Auto Layout a breeze.

SuperLayout is a library that adds a few custom operators to Swift that makes using the amazing NSLayoutAnchor API for Auto Layout a breeze. SuperLayo

Lionheart Software 53 Oct 12, 2022
Content Hugging Priority settings using Auto Layout

AutoLayoutContentHugging Swift 5 and Xcode 12. Content Hugging Priority settings using Auto Layout. Content Hugging Priority give you granular control

Camila Rodrigues 0 Jan 21, 2022
Arrange views in your app’s interface using layout tools that SwiftUI provides.

Composing custom layouts with SwiftUI Arrange views in your app's interface using layout tools that SwiftUI provides. Overview This sample app demonst

Apple Sample Code 0 Jun 9, 2022
BrickKit is a delightful layout library for iOS and tvOS. It is written entirely in Swift!

BrickKit is a delightful layout library for iOS and tvOS. It is written entirely in Swift! Deprecated BrickKit is being phased out at Wayfair, and the

Wayfair Tech – Archive 608 Sep 15, 2022
A powerful Swift programmatic UI layout framework.

Build dynamic and beautiful user interfaces like a boss, with Swift. Neon is built around how user interfaces are naturally and intuitively designed.

Mike 4.6k Dec 26, 2022
Fast Swift Views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainable. [iOS/macOS/tvOS/CALayer]

Extremely Fast views layouting without auto layout. No magic, pure code, full control and blazing fast. Concise syntax, intuitive, readable & chainabl

layoutBox 2.1k Dec 22, 2022
A declarative Auto Layout DSL for Swift :iphone::triangular_ruler:

Cartography ?? ?? Using Cartography, you can set up your Auto Layout constraints in declarative code and without any stringly typing! In short, it all

Robb Böhnke 7.3k Jan 4, 2023
An easy way to create and layout UI components for iOS (Swift version).

Introduction Cupcake is a framework that allow you to easily create and layout UI components for iOS 8.0+. It use chaining syntax and provides some fr

nerdycat 288 Oct 9, 2022
Lightweight Swift framework for Apple's Auto-Layout

I am glad to share with you a lightweight Swift framework for Apple's Auto-Layout. It helps you write readable and compact UI code using simple API. A

null 349 Dec 20, 2022
An Impressive Auto Layout DSL for iOS, tvOS & OSX. & It is written in pure swift.

KVConstraintKit KVConstraintKit is a DSL to make easy & impressive Auto Layout constraints on iOS, tvOS & OSX with Swift Installation Using CocoaPods

Keshav Vishwkarma 90 Sep 1, 2022
A compact but full-featured Auto Layout DSL for Swift

Mortar allows you to create Auto Layout constraints using concise, simple code statements. Use this: view1.m_right |=| view2.m_left - 12.0 Instead of:

Jason Fieldman 83 Jan 29, 2022