last day (15 days later) » 

9:44 PM
0
Q: Dynamically adjust size of UILabel according to the number of lines programmatically

Anuar  MaratkhanI have a View where I create a UILabel with numberOfLines equal to zero. I want the label size to be adjusted according to the content of my label (according to numberOfLines). However, I have tried many things including all this, and it still does not work for me. I use Neon library for AutoLayo...

 
I'm having a hard time understanding what you're actually trying to accomplish but right off the bat I see something that is definitely wrong. height: requirementsLabel.frame.size.height makes no sense because that parameter is a constant. You're passing in the constant height you got from sizeToFit so it will not adjust for any other changes after the fact.
 
@Dima I actually have been using AutoHeight property of Neon library that "should" adjust the size automatically. However, it gives me only one line size. So, I just hadn't paid so much attention what I write there because that's the property that I need to find out.
 
I looked it up and I see AutoHeight. Still not sure about the answer to your question without some more details but you should edit your question to have exactly the code that you're using.
 
@Dima I have added whole code for that View
 
I'll try to help you. What is the actual behavior you are seeing happening?
 
9:44 PM
@Dima I see that my requirementsLabel goes out of the actual View because AutoHeight counts only the first line's size as a whole size of label. So, other lines just fall out of my view.
 
Your problem is occurring somewhere else. I just copy pasted your code into a new project by adding the view to a view controller and it works just fine for me, screenshot: take.ms/QVQug So the problem is most likely how you are actually adding and using AdditionalDescriptionView. The internals are fine.
 
@Dima I have the same in my view. However, if you for example change backgroundColor of that view to something else rather than white, you'll see that the last label falls out of that view
 
Let me check
Can you take a screenshot?
I don't really understand, I'm not seeing any weird behavior
The only way I can get it to get cut off, is if I initialize AdditionalDescriptionView with dimensions that are too small
In that case, the bottom label gets cut off
Is that what you mean? Show me how you actually initialize AdditionalDescriptionView
 
10:52 PM
Here's what I am talking about: take.ms/5K4xH
 
Yeah, that's what I thought
show me the code where you create AdditionalDescriptionView
 
lazy var additional: AdditionalDescriptionView = AdditionalDescriptionView()
 
What I am really looking for is where you initialize its frame, or how you are sizing it
 
 self.view.addSubview(additional)
 additional.anchorAndFillEdge(.top, xPad: 0, yPad: 0, otherSize: additional.height)
 additional.updateConstraints()
Sorry for that, having troubles with the chat :)
 
Okay I think I see your issue
You are never defining how your subviews actually relate to your superview
you try to do it with this height property, but that's not really working
you should not have to specify a height for additional at all, you should be able to add a constraint with your other constraints that anchors your requirementsLabel to the bottom of the parent
Trying to see how to do that with neon
 
11:05 PM
There's a method called .alignBetweenVertical
Probably you are talking about that
 
im able to reproduce your problem
going to try now, one minute
 
the thing is that this is my last label, and I can't fix it because there is nothing below it
 
So the right thing to do here is to anchor it to the actual superview
not to any sibling views
But im having a hard time doing that with this Neon library, it is kind of confusing
 
11:20 PM
I've tried that also, it does not solve this issue
That's I started googling and searched for all related stackoverflow questions but that didn't solve the issue too
 
this is a really easy thing to write if you just use normal autolayout
 
maybe there is a way to solve it without neon library?
 
hang on
ok, i fixed it
 
i just used iOS layout anchors
this works great
so here are the differences
1. you get rid of sizeToFit, autolayout handles all of that
2. you have to call translatesAutoresizingMaskIntoConstraints = false on every view you make to let it know to use autolayout
then you can see how i set up the layout anchors
the labels just anchor to one another vertically, and the top one anchors to the top of the superview and the bottom one anchors to the bottom of the superview
then in the view controller, you just do let additional = AdditionalDescriptionView()
self.view.addSubview(additional)
additional.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true
additional.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
you can define where to place it vertically, then just anchor it horizontally
and it will figure out its own height because its subviews are all anchored to each other and will size the it automatically
does that make sense?
 
11:32 PM
Can you please suggest a tutorial or topic for that in order to learn it better?
 
so the benefit of this
is that there are tons of tutorials online
and when you ask questions on stackoverflow, people will be able to answer because it is the standard way of doing it
let me see
 
I haven't worked with that yet, that's why I'm still stuck a bit. But hope I'll look through your code and understand it
 
well first off
grab it and try it for yourself
you should just be able to copy and replace the entire AdditionalDescriptionView class
tons of tutorials
 
Thank you so much
It is a pleasure to get help from such great developer
 
no problem
i edited your question with the other code you gave me
im going to create an answer and post it
so you can mark it as correct
 
11:37 PM
I'm admiring :)
Yes, sure
Can I keep in touch with you? Not for the help of course
 
I posted my answer
Yeah sure
The bottom line with autolayout as a parting note
once you get familiar with the layout anchor stuff
is that its a puzzle
if anything is going to be auto sizing, you have to make sure you know which piece is auto sizing and which pieces are anchored and aligned
so like in your case, the situation was impossible because those relationships weren't defined
 
because neon does not provide that
 
i think it does
its just very confusing
the functions are all very wordy and confusing as to what the side effects are
i like the layout anchor stuff because it is simple and explicit
every line defines exactly 1 relationship so you can quickly see how something is set up
 
they actually have beta for swift 3 now
 
oh its not even swift 3 yet, look at that not updated since October
yeah i would definitely not be using this
you will be much better off getting familiar with the standard stuff, it will pay off
 
11:46 PM
Ok, got that
Can you please share your contacts to keep in touch with you?
 
you can email me at dim1014@gmail.com
 
Xorowo :)
Thanks again
 
no problem, good luck
 
same to you
 

  last day (15 days later) »