I'm seriously lost on this one.
Here's the shot:
The price on Tea's Tea Matcha... is being cut off. I simply cannot figure out why.
I'm using boundingRectWithSize:...
When I log the height of the label's frame after calling layoutIfNeeded, it's the height that it should be for each respective frame: the same height as boundingRect.size.height.
Also, when I log the text of the label, the price is in the log so it's not that either.
It's just this and a few other labels out of hundreds that are having this issue and I do not know why.
This is my code for setting the height of the label:
CGRect boundingRect = [self.nameLabel.attributedText boundingRectWithSize:CGSizeMake(self.frame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin context:nil];
self.nameLabelHeightConstraint.constant = ceil(boundingRect.size.height);
[self layoutIfNeeded];
One thing I've noticed - if instead of calculating the height with boundingRect and setting it manually I use sizeToFit on the label, it has this same problem but if I scroll past the row and back to it, it's fixed and the price appears underneath.
Anyone know wtf is going on here?
I found I was able to fix the issue by changing the default height constraint constant. I lowered it significantly (it was large enough that it was touching the bottom of the Cell.xib view. I made it spaced closer to the view above it by 1 pixel, lowered the default height constraint constant in IB and changed the type from equals to greater than or equal to. These things combined (I'm not sure which if not all did it) seem to have fixed the problem.
Related
I am very new to iOS development and have never done auto layout before ,I have actually seven buttons on a view controller that needs to look round on every screen without fixing height and width....I have looked many tutorials but couldn't understand that how I can add constraints on those round buttons and show them at same position on every screen. I want the buttons to actually increase size when screen increase and decrease when screen size decreases.Please help and show which constraints should be added.!this shows how buttons are added on my view controller
If you want perfect round buttons(circle) then width and height should be same. For that set the aspect constraint with multipler 1:1 so that width and height will become equal.
Based on the the screenshot you have provided, see below how the constraints should look like:
Well, two points:
Position: Well, you need understand accurately ‘same position in every screen’, I guess you know view.frame = CGRectMake(10, 20, 50, 50)but same code not lead to 'same position' in different screen, important thing is which way you want. Think about a increasing screen, you have a square on it, what do you want this square change? Different change style lead to different code.
Size: You said you want square increasing or decreasing with screen, the basic way is let square.width && square.height changing with screen, if use frame layout you may write view.frame = CGRectMake(10, 20, SCWidth * 0.0666, SCHeight * 0.0833), certainly autoLayout support scale calculate, I recommend you use Masonry to add layout, sample code like:
[square mas_remakeConstraints:^(MASConstraintMaker *make) {
make.width.equalTo(self.mas_width).multipliedBy(0.0083);
}];
of course if you use xib to do it, you can see constraints have multiplier property to fix problem.
Use this code to make round button..
You can programmatically get the current height of Button and then assign the half of height to corner radius to make it round.
[self.view layoutIfNeeded];
[self.view setNeedsLayout];
self.yourButton.layer.cornerRadius = self.yourButton.frame.size.height/2;
self.yourButton.clipsToBound = YES;
I'm getting the wrong height for an UITextView even I'm calculating its size like this:
CGRect requiredHeight = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
options:(NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading)
context:nil];
When I add the height to my frame I can see all text but not the last line. I don't know why. (text was a HTML before I transformed it into a NSAttributedString).
so, the answer es very obvious but not always we see things like that so easily!.
EXPLANATION:
The answer is if we need to get the height for an specific field (it could be label, textView, etc) we should use a method from those fields to calculate its height, due to they could have some specific properties, if we calculate the height from a text (not considering the field) we are not considering those special properties the field could have.
SHORT ANSWER:
So short answer, for UITextView you should use [textView sizeThatFits:] to get its height, it's probably the best way to do it if you are not using auto layout.
advice:
I encourage you to use autolayout if you can easily migrate your UI.
Having a strange issue using Auto Layout with a UILabel where it resizes almost to its content size less one line (driving me nuts). So basically, it seems to get to where it should minus a few pixels causing an ellipses at the end when downsizing to the iPhone 5 or 6. It works fine on the 6+, which is what the XIB is sized for.
Here's what the XIB looks like and what I have the constraints set to. The label is the "Big long label here is the event description".
Based upon this stack post, I thought I was going to figure it out. I tried both methods mentioned in the post. All four of the constraints in the screenshot are set to 1,000. If I do what the post says and add a height constraint with priority of 500, which is less than the vertical hugging and vertical compression resistance priorities, it still doesn't work. I've also just not set the height constraint, kept the preferred width set and had leading and trailing space constraints set, which was mentioned in the comments. The number of lines is set to 0.
To try to give more detail, the UILabel is in a UIView set as contentView. I then add the contentView to a UIScrollView. The UIScrollView is being automatically sized by pinning the contentView at 0 to all four sides. The contentContainer is also constrained to the screen width (not ideal but only way I've been able to get it to work - not sure if it has anything to do with this issue). I'm not getting any constraint issues in the console. translatesAutoresizingMaskIntoContraints is set to NO for the contentView. Here's what the UILabel looks like when ran:
It should be one line longer at the bottom to fit. Someone please help me!
I think I might have solved this, but the solution seems hacky. It seemed to be an issue of setting the preferredMaxLayoutWidth. It worked fine for what I had set for the iPhone 6+, but for some reason almost got there with significantly less width on the iPhone 5/6. Just added this:
self.eventDescriptionLabel.preferredMaxLayoutWidth = self.eventDescriptionLabel.frame.size.width;
And now it works fine.
The bottom line is that I'm trying to reproduce the UI that iMessage has.
For the Label:
The special padding of that type of text made me create a custom UILabel. Here's the code under drawTextInRect:
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, UIEdgeInsetsMake(0, 15.0, 5.0, 15.0))];
No mystery for now.
The problem comes when my cell (that contain that label has to calculate height).
The Label gets a rounding effect on the label like so:
cell.message.layer.cornerRadius = 18;
[cell.message sizeToFit];
Apparently I can't get the proper height and width of that label. I'm using sizeToFit and then I mesure the possible sizes with "sizeWithFont:" (deprecated in iOS 7) and "boundingRectWithSize:".
The only way the text can show properly is adding manually an undetermined amount of size to height and width once the calculations are made.
The best I can get then is a screen that may look good but still has some problems and not draws properly the texts.
The link has a screen of some of the screens not showing properly.
The only answer I've been able to see looking at code from other people is that they at some point make their own calculations based on letter size.
Anyone with this problem check : https://github.com/jessesquires/MessagesTableViewController
It was the only source I could find, at the end for some cases the boundingRectWithSize is not good enough.
I'm creating an iOS view that displays various static text elements. The xib looks like this:
It uses four labels for the title, timestamp, body, and footer. Every view is anchored to the sibling view above it vertically and anchored to the left/right of the parent view. All labels have a fixed height except the body which has a >= height and the number of lines set to 0 with "word wrap" as the line wrapping style. The parent view is a UIScrollView.
On the iPhone it looks like fine:
However on the iPad it looks like this:
Huh? Where is all that extra vertical space in the body label coming from? The xib and its view controller are identical between iPhone and iPad (there is no custom iPad code at the moment). I've found that the vertical space is directly related to how many line-wraps the label renders. If no lines wrap, no extra vertical space. If only a few lines wrap, there's a little extra vertical space. If nearly every line wraps, well, that's what it looks like.
First of all any ideas on why UILabel is behaving this way?
Second of all, if I can't make it stop doing this how can I work around it?
I've already tried a few things. If I call [bodyLabel sizeToFit] within -viewDidLayoutSubViews then it fixes the label but doesn't fix the layout of any of the sibling views (e.g. the Footer label is stuck way at the bottom of the screen instead of pulled up to just under the body). Any attempts to get the entire view to re-layout its children after calling sizeToFit is ignored. I've also tried sizing the UILabel by calculating height based on font, which results in the same behavior as -sizeToFit (albeit with more code).
Replacing the Body UILabel with a UITextView instead doesn't give me the weird vertical spacing issues but I need to calculate the height of the UITextView manually (using font calculations) and something about resizing the UITextView within the parent UIScrollView makes it so the UIScrollView simply refuses to scroll (as if it doesn't know its contents are too big for its bounds).
So at the moment I'm stuck. Even just an explanation of why UILabel behaves this way on the iPad layout would be helpful.
In case anyone else runs into this same issue using autolayout... I may have been able to solve the same issue by creating a constraint as Coche suggests, but I realized I had a preferredMaxLayoutWidth that was too small set on the uilabel. Once I set an accurate preferredMaxLayoutWidth (the actual width of the label) the spacing on top and bottom disappeared.
The main problem is that the method for auto resizing the text inside your Label is failing because in iPad your Label doesn't have a set width from the beginning, it is calculated on run time and that's the source of that mess. On iPhone, as your Label has a set width (on IB) there is no troubles.
There are two ways for solving the problem:
Having two storyboards : one for iPhone and one for iPad
Doing this will make that your Label knows its width since the beginning and it will just works as on iPhone.
Having just one Storyboard for both iPhone and iPad
You can go around the problem by calculating the size that best fits its text and with that result add a height constraint by code to the Label. For calculating the desiredSize you can calculate the width with this formula: Current View's width - (Leading space + Trailing Space). Here is my code
CGSize desiredSize = [_bodyLabel sizeThatFits:CGSizeMake(self.view.frame.size.width-40, 10)];
NSString *visualContraint = [NSString stringWithFormat:#"V:[_bodyLabel(%.0f)]",desiredSize.height];
[_bodyLabel addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:visualContraint
options:NSLayoutFormatDirectionLeadingToTrailing
metrics:nil
views:NSDictionaryOfVariableBindings(_bodyLabel)]];
objective-c