I am trying to display a Label and a button side-by-side in a UITableViewCell. The button sits at the right end & the Label sits at the left end. The label can host a long text and should get truncated if the text would flow out of bounds (I do not want to reduce the font size, etc.). Needless to say, I want this to work for both the orientations.
If I use UITableViewCellStyleDefault (without adding a button) & set a long text for the default label, I observe that the label auto-resizes perfectly when the orientation changes. Most probably because its autoresizingMask is set to UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin. Now, the way I see it, I could insert my button, and make the label truncate properly, if I could configure the value of the right margin that is being used by default (to accommodate the button). I essentially want it to auto-resize within the bounds that I specify. Is there a way this can be accomplished?
I would not prefer listening to each orientation change and setting the bounds of the label's frame. Any other feasible solution?
I finally went with subclassing UITableViewCell & overriding layoutSubViews. Thanks Mark!
Related
There's a UIButton which does transition between two controllers. Either the two controllers have the same functionality of flipping to each other. It works pretty well, except that "sometimes" the button's width shrinks, while most of the time it looks just normal. The height and width of the button are both set to 35, a baseline constraint with its previous object and a trailing constraint with its superview in the storyboard is also declared. The background of the button is assigned to a image set in images.xcassets file. Is there any possible reason that the button width sometimes shrinks?
Normal state of the UIButton
And sometimes the UIButton thinks like this
It looks like the image is making space for title. This may be the case if you're using a standard UIButton. Try setting it to a custom type then you will have more control over the survives it contains.
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 creating a custom UITableViewCell using the Storyboard editor. I've got several components in this cell, but the biggest is a UILabel that is displayed at the top. This label should be left-aligned, however I'm finding that when I run the app in the simulator to test, the text is centered. I've tried specifying the alignment manually, but it always centers itself.
Is there something I need to do to get this working properly in a custom UITableViewCell? I've never encountered anything like this before.
Try using Debug->Color Render layers in the simulator to see if your text is actually centered, or the UILabel moved. Try removing the outlet if any, to prevent anything from tampering with it and see if it helps diagnose.
If the colored layer is offset, make sure you added proper constraints to the label (or autoresizing masks). Anchor it to at least left and top and leave the other 2 sides free.
I'm wonder is there a way to make a button that includes UIImage and title label under the image using auto layout. I made this using title insets left with negative value. But, right now I want to add localisation to my app and, of course, title labels of my buttons won't be a constant value. I see the only way: calculate every insets for every button for a particular text (for every localisation). This is quite annoying.
Is there a way to replace insets with auto layout without separation of UIButton?
I'm writing a calculator app with a shift key. When the shift key is pressed, some of the UIButtons' labels should change. The problem is that when they do, the widths also change and, because of certain design constraints, other buttons will resize themselves too. How to I lock the width of a button (either pragmatically or in the storyboard builder) so that it will never change (regardless of the contents of the button)
Thanks for any help in advance.
Buttons, like labels have an intrinsic size that's set by the width of their text plus some padding. If you want them to have a fixed width, then in IB, just give each button an explicit width from the menu, Editor --> Pin --> Width.
Have you tried using [button sizeToFit]? Call it after you resize the subviews within and then you will likely want to apply some "padding" as this will size the view to exactly fit it's subviews.
UIView sizeToFit