I am trying to find the height/space occupied by an UILabel on the screen programmatically whose number of lines increases or decreases based on different devices and string internalization. So I tried to get the height of the UILable by the following method.
uiLable.bounds.size.height/ uilable.frames.size.height
This method was implemented in both viewDidAppear and viewDidLoad methods. In both methods, the height returned is same on all devices and when the number of lines increases/decreases. Please find the screenshot of the UI that I have. I have to align the switch button to the centre of both text (Remember for all meetings and These can be changed in settings).
Any help would be appreciated. Thanks in Advance
After updating the text in your label you can force the layout system to update its UI and then get the correct bounds of your label.
// set your label text
label.text = "multiline text here"
// Now force layout system to update its UI instantly
self.view.setNeedsLayout()
self.view.layoutIfNeeded()
// Now you will get the correct bounds
let bounds = label.bounds
I am trying to find the height/space occupied by an UILabel on the screen programmatically whose number of lines increases or decreases based on different devices and string internalization.
Do not try to do that. You have no reason to want to know it.
Please find the screenshot of the UI that I have. I have to align the switch button to the centre of both text
Then align it with auto layout. That is exactly what it is for. Let the runtime take care of the problem for you. You cannot possibly internationalize successfully without using auto layout to take care of this sort of thing.
You can place both the label in a single view and set autolayout as verticalCenter of view is equal to vertical center of toggle button.
Related
I am using JSQMessagesViewController and I created a custom cell according to this answer.
How to add custom view to JSQMessagesViewController cell so that it includes one view with some buttons and text view?
Now I am struggling to set auto layout constrains correctly. So, the message bubble not display correctly. Please help me set the constraints correctly
So lets take a stab at this. So first things that I would do it set a constraint for the first button to be equal hight of the other. You can accomplish a couple of ways but I will only describe one here for brevity. You can do hold the control button on your computer and select button1 and drag to button2. This will present you with a couple of options that look like this.
You want to select Equal Heights this will make it so both your buttons have the same hight. Then you will want to give it a hight. Once again hold down the control button on your keyboard but this time click button1 drag and release within button1. you should get something along the lines of this.
If you do not get the desired options try dragging in a diagonal direction. Xcode is tying to guess what constraints you want based on the direction of your drag. I.E. If you drag vertically you should get the Height option.
Then you can go to the properties inspector on the right and set a number for how high you would like. Text is normally around 12pt so I would go with about 30pt or more for the hight of a button. Then add a constraint for spacing between them and leading and trailing to the containing view or you could give them a standard width and center them in the view. Which ever fits best for you.
Edit:
You should also adjust the bubble size calculation.
It can be found in the JSQMessagesBubblesSizeCalculator class.
Eg: In the
- (CGSize)messageBubbleSizeForMessageData {
if([messageData isOptionMessage]){
// add button height also (In this case i have set constant 200. But we have to calculate the actual button heights instead)
finalSize = CGSizeMake(finalWidth, stringSize.height + verticalInsets + 200)
} else {
finalSize = CGSizeMake(finalWidth, stringSize.height + verticalInsets)
}
}
Let me know if you need any more help and keep up the good work. 👍🏼 🚜
I have a view of limited width, lets say it 100.
I have multiple labels of same width inside that view and its text is centre aligned. So that text seems to be center aligned always.
Problem is when I got long text label it shows ellipses at the end of the text but I want to show whole text in one time while keeping superview width unchanged. Is it possible to attain this? If not then show how can I wrap that text if it is larger than its width. I'm new to IOS programming.
There is an issue with UILabel and autolayout. If you using it - check this answer: iOS: multiline uilabel only shows one line with autolayout
Also, check actual size of label and insure clipsToBound of superview is set to NO.
I have 3 labels .First one at the top of the screen, second in the middle and third below. I used both( size class(cW rH)+ autolayout)to layout the labels.The third label appears in all sizes expect the 3.5inch screen. Can autolayout and/or size class help me accommodate my third label in some way?.I know putting a scroll view or doing programatically will help me achieve what i want but just want to know whether the current iOS SDK is intelligent enough to cut this.
You have to use different constraint for each label.
Please check below screenshot to understand how to set constraint to each label.
May be it will help you to set labels.
I have Submit label as third label and I did following for it . Try using it and dont forget to click add constraint after doing it
I'm trying to make a Facebook clone for practicing iOS and I can't see why a label I have on my news feed gets unnecessary padding.
It only occurs on some labels, others on my news feed turn out fine. However for a select few there's a block of white pace above and below. At first I thought it was an alignment issue so I changed the labels background to green to show that the constraints hold out.
Anyone know as to why it's placing the padding, only for a select few?
By default UILabel will center its content vertically. Therefore, if label.bounds.size.height is greater than size of the text, the label's instinct is to center the content vertically, which will results in the vertical padding that you see in the attached image. Ensure that the label's height is being set according to the height of the text it contains and the problem should go away.
As dbart pointed out your label's frame is likely higher than the text needed. You can fix this by calling -sizeToFit. You can also check the amount of space the label is actually using for text by using +textRectWithBounds:maximumNumberOfLines:
If your cell is using autolayout to determine the height of the label you should set the preferredmaxlayoutwidth to an appropriate value (for example table width) before laying out the cell.
I am building an iOS app that uses a UILabel to present a question at the top of the screen and then displays a number of UIButtons with text in the titleLabel to get the answer. I am using this View to display many different questions at different times and each question and set of answers has a different number of words in it. As a result, each UIButton can end up with a very different number of words, which creates a layout problem. I am currently using some code like this:
self.firstAnswer.titleLabel.numberOfLines = 0;
self.firstAnswer.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.firstAnswer.titleLabel.textAlignment = NSTextAlignmentCenter;
[self.firstAnswer sizeToFit];
which allows the buttons to resize itself to keep all the text onscreen. It also, however, causes the uibuttons to overlap vertically when a button resizes too much vertically (ie, the titleLabel is now 4 lines instead of 1). Is there any way to cause a button that is vertically below another button that has resized itself to move down to make room for the newly resized button?
What if you update the position of the following buttons based on the new height of the firstAnswer:
self.secondItem.frame = CGRectMake(self.secondButton.frame.origin.x,
self.firstAnswer.frame.origin.y + self.firstAnswer.frame.size.height + 10,
self.secondButton.frame.size.width,
self.secondButton.frame.size.height);
If you've got multiple other items, you can reposition them after each subsequent one, or use a general offset for all.
EDIT:
If multiple buttons follow, you cold also just put them inside a single tranparent UIView, then just offset that UIView alone.
Yes, use auto layout and give the buttons vertical spacing constraints to their nearest top and bottom neighbors. This will keep the spacing the same no matter how many lines you need for the button.