I've got a label we'll call selectedTextLabel with multiple labels underneath it forming a list (yes, I know about repeaters), when I select one of the lower labels I throw in that label's text description into selectTextLabel. The problem I've running into is that, though selectedTextLabel is aligned center (positionally) and the text within it is center aligned, when new text that is longer or shorter than the original gets put within it from an action, it keeps the same top left xy coordinants and increases or decreases the width accordingly making my design look misaligned.
It's almost like it's not obeying text-align: center on dynamic text like it does in the editor.
Does anyone know a solution? Is this a bug? I've googled but only have gotten editor based answers as opposed to dynamic browser based answers.
Thanks.
After an insight, it turns out it's rather easy. You just have to make the label the full width that you want it to be centered within. Then if you have text align center set, it'll keep the text centered.
What was happening in my instance was that I only had the label set to the width of the first selection and since the label didn't know what to do when something longer was set as the text, it expanded it's width but kept the same top left x and y coordinants, making the new center misaligned.
I hope this helps someone out there.
Related
I am trying to create an Android-esque Snackbar. All the behaviour is correct but I am left with a layout issue. I have actually broken this out into a test app to simplify things a bit.
The bar is pinned to the bottom of the view and contains an Icon (UIImageView) along with some text (UILabel). This text can be up to 2 lines max.
For example, it should be able to exand out like this:-
However to further complicate things, i need it so that the icon and text are centred within the red bar and then spread out from the centre until such point that it needs to wrap. Note that there is a padding to the start of the icon and end of the text to prevent it touching the sides. An example of centred content would be..
I haven't been able to get the correct layout. I think the issue has been trying to centre the two items whilst simultaneously conforming to a width that doesn't exceed the edge bounds, causing a constraint conflict. I have also tried embedding the icon and label in a horizontal stack view but couldn't seem to find the correct fill option whilst centring everything.
I have even tried using NSAttributed string and adding the image to the text itself but when the text wrapped, the icon was being resized and/or misplaced.
As you can see, i have been doing this in Storyboard but i am more than happy to do this programmatically if it serves this purpose better.
Created this Demo for reference , it's a simple task of making a nested view inside the red view with a centerX constraint of priority 1000 and a leading constraint with 999 priority
Now you have this effect
i have a similiar problem as asked in How to resolve the Storyboard's warnings on Xcode?
However my left label can get really wide at runtime and the standard behaviour is, that the right label is truncated. How can i force the interface builder to truncate the left label and not the right one?
Both texts in the labels are set on runtime, so i have no knowledge of the resulting width of each label.
As stated above i used the solution from the linked question but the wrong label is truncated. So my problem differs from the one stated in the link.
There is a property called content compression resistance.
This determines how hard it resists compression of content.
So... for the label that you want to keep full length run this...
label.setContentCompressionResisyancePriority(.required, axis: .horizontal)
I’m typing on my phone so I may have messed up the parameter names. But auto complete will help.
This will make sure that the other label is truncated first.
You can also set this in IB in the measurements panel near the bottom.
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 have a view which has a lot of labels. Some of them get the text set dynamically. Sometimes the text is to long to display it within one line. I know how i can display the text in multiple lines. My problem is that when i do display the text in multiple lines then the margins are broken between the labels. Sometimes a label even swapes into the content of the next label.
I want to avoid fixing this "by hand" (repositioning of all other elements). Maybe Autolayout can fix this, but sadly i'm not able to use it for now. But it would be a good argument to convert my project from springs&struts into autolayout. However, maybe there is another way to fix this issue. Would be nice if somebody can help me out. Thanks in advance!
You can use sizeWithFont:constrainedToSize:lineBreakMode: to calculate the height which will be required by each of the labels. You should use this in a loop which iterates over each label in the order they should appear on screen. As you go, increment the y position by the height of the current label and the margin. Each label will now have the correct position and at the end of the loopy will hold the full required height.
Say, a default EditText is created. It has a default padding around the white editbox.
The default padding at the bottom is larger than the padding on the top.
This means: the center horizontal line of the EditText is not at the center of the white editbox.
If I put a imageView, Align Right and Center Vertical with the EditText, the imageview is not at the center of the white editbox.
I could manually nudge it. But I am afraid it will not apply to all devices.
A better solution?
Thanks!
I would suggest you follow this tutorial, this will achieve what you want and look great at the same time. http://www.wapolabs.com/2012/10/17/android-tip-1-customizing-an-edittext-view/
It is not good practice to put an anything in an edit text but text. Stick with customizing the edit text view as its not hard and keeps good functionality.
and as always set the padding when you customize an edit text to force the text to the exact spot you want it. padding will look fine on all phones.