LinkedInAttic / LayoutKit

LayoutKit is a fast view layout library for iOS, macOS, and tvOS.
http://layoutkit.org
Apache License 2.0
3.16k stars 267 forks source link

LabelLayout goes beyond screen width #204

Closed kartikthapar closed 6 years ago

kartikthapar commented 6 years ago

Here's a wrapper for a bunch of components/layouts but currently only hosting a label layout.

public class DisplayComponent: InsetLayout<UIView> {
  init() {
    super.init(
      insets: UIEdgeInsets(top: 32, left: 32, bottom: 32, right: 32),
      sublayout:
      StackLayout(
        axis: .vertical,
        spacing: 32,
        alignment: .center,
        viewReuseId: "login-stack",
        sublayouts: [
          LabelLayout(
            text: "This is greater than a single line text",
            font: UIFont.font(ofSize: .extraLarge, withWeight: .heavy),
            numberOfLines: 2,
            alignment: Alignment.topFill,
            flexibility: .max,
            viewReuseId: "hello-login"
           ),
        ],
        config: { (view) in
          view.backgroundColor = UIColor.green
      })
    )
  }
}

Its called here:

class DisplayViewController: UIViewController {
  override func viewDidLoad() {
    view.backgroundColor = .white

    DisplayComponent().arrangement().makeViews(in: view, direction: UserInterfaceLayoutDirection.leftToRight)
  }
}

Here's the result:

screen shot 2018-04-29 at 2 29 31 pm

Now I am unsure what's wrong here:

kartikthapar commented 6 years ago
screen shot 2018-04-29 at 2 34 14 pm

One more thing, if I just have a single label aligned using Alignment.center, it show up as above. It feels like the width of this containing view is wrong but there is nothing much that is going on.

tysonkerridge commented 6 years ago

Just from looking at the code, without testing it myself, it looks like the thing missing in your original post is giving a width/height when making your arrangement.

Layout().arrangement().makeViews(in: view)

should be

Layout().arrangement(width: view.width, height: view.height).makeViews(in: view)

Without passing the sizing information, LayoutKit is going to calculate the frame at the largest size. Check out Layout.swift, method starting at line 121 arrangement(origin:width:height:)

Edit: Same thing for your second post about the single line label.

richardtop commented 6 years ago

@kartikthapar just out of interest, what font are you using in these examples?

staguer commented 6 years ago

The answer by @tysonkerridge appears to be correct. Closing the issue.