Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline UILabel #95

Closed
NamtarR opened this issue Nov 1, 2017 · 6 comments
Closed

Multiline UILabel #95

NamtarR opened this issue Nov 1, 2017 · 6 comments
Labels

Comments

@NamtarR
Copy link

NamtarR commented Nov 1, 2017

Hello! Can't find a way of laying out a multiline UILabel properly.

Here is what I have:
The big label is a multiline label and should take all the space left after laying out the small label which has a fixed size.

+--------------------------------------------------------+
+                                                        +
+     +----------------------+                           +
+     + Some text here and   +                           +
+     + it should be a good  +     +---------------+     +
+     + multilne label       +     + another label +     +
+     +----------------------+     +---------------+     +
+--------------------------------------------------------+

Here is what I have tried:

  1. Laying out the small label first, then pinning the big one to it - no luck.
  2. Laying out the big one based on the distance between big label and its superview - again no luck.

The symptoms are all the same: the big one does get proper width but gets zero height. Or if I

FitSize() really does nothing at all even though the label has non-zero width. I have tried to call sizeToFit() method of UILabel but even so layout gets messed up.

I can't find out what am I doing wrong so please can you help me?

@lucdion
Copy link
Member

lucdion commented Nov 1, 2017

Hi @NamtarR, can you share me a piece of your source code?

Normally this should do the trick:

// The size of the smallLabel will be adjusted to the text up to 50% of the space.
smallLabel.pin.bottom().right().maxWidth(50%).fitSize()
// The bigLabel will takes the left remaining space.
bigLabel.pin.bottom().left().before(of: smallLabel).fitSize()

Just be sure that bigLabel.numberOfLines is 0.

@NamtarR
Copy link
Author

NamtarR commented Nov 9, 2017

Sorry for such a long inactivity here.
I have tried something like this, but no luck:

smallLabel.pin.bottom().right().width(72).fitSize()
bigLabel.pin.left().bottom().top().before(of: smallLabel).fitSize()

@lucdion
Copy link
Member

lucdion commented Nov 9, 2017

Can you paste also your controls initialization, or all the file containing this section of code?

@lucdion
Copy link
Member

lucdion commented Nov 22, 2017

@NamtarR did you found a solution?

@NamtarR
Copy link
Author

NamtarR commented Nov 22, 2017

I am sorry for my long replies.

let image = UIImageView()
let footerHolder = UIView()
let title = UILabel()
let expiryDate = UILabel()

override init(frame: CGRect) {
         super.init(frame: frame)
       
         contentView.addSubview(image)
         contentView.addSubview(footerHolder)
         footerHolder.addSubview(title)
         footerHolder.addSubview(expiryDate)
    }

    override func layoutSubviews() {
        super.layoutSubviews()
        image.pin.left().top().right().height(172)
        footerHolder.pin.left().right().below(of: image).bottom()
        expiryDate.pin.bottom(24).right(18).width(68).fitSize()
        title.pin.left(18).before(of: expiryDate).bottom(24).top().fitSize()
    }

This is what I tried and for unknown reasons it did not work though looks fine.

@lucdion
Copy link
Member

lucdion commented Apr 5, 2018

Hi @NamtarR, sorry for the long delay, by default a UILabel is single line, you must set it to multiline using title.numberOfLines = 0 and expiryDate.numberOfLines = 0.

Also, update your code to use sizeToFit(.width) instead of fitSize().

@lucdion lucdion closed this as completed Jun 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants