0

I am having pretty crazy amounts of issues trying to align two labels inside a table cell:

enter image description here

The problem is I need both labels (the region is clickable) to be centred within their cell. I can only seem to get the layout centred based one or the other.

Is this too much to expect from autolayout?

JamieB
  • 923
  • 9
  • 30
  • Maybe this helps: http://stackoverflow.com/questions/24403359/autolayout-center-two-views-center-to-superviews-center – koen Feb 10 '15 at 17:27

1 Answers1

2

Embed the labels in a new view, then centre that view with autolayout. Also, let this new view resize itself according to the labels

So:
- Add leading and trailing constraints between labels and their superview (new view)
- Centre the new view in the UITableViewCell


I just got home, so I opened up a fresh project, with good news :)

So I set up what I described earlier, 2 labels inside an UIView
Select the labels and go to the Size Inspector, there check 'Explicit' and in the 'Preferred Width' add the Maximum - make it the maximum it is allowed to be

Now in code, you can set the labels and they auto-resize!

brechtb
  • 108
  • 6
  • A couple of things: Resize itself according to labels, is this an option that can be enabled? Leading and trailing constraints only work if I know how big the line of text will be at all times surely? – JamieB Feb 10 '15 at 19:00
  • [Check this answer for checking UILabel width](http://stackoverflow.com/a/3527660/3983677) – brechtb Feb 10 '15 at 19:19
  • The question asked how to achieve this with interface builder, I understand that it is possible with code by calculating widths but I was hoping to do it without code. – JamieB Feb 10 '15 at 19:20
  • If the labels are static there won't be a problem with label widths, but if they are not then in Xcode 6 you can select 'Automatic', but it only works for iOS 8.. (Select Label > Size Inspector > make sure the Preferred Width is Automatic (uncheck 'Explicit') – brechtb Feb 10 '15 at 19:24
  • There seems like there are a large amount of steps just to get these centred: create a new view, create two labels and measure their height and width, resize the view to the height and width of the two labels, centre the view. Finally add the view to the table cell – JamieB Feb 10 '15 at 19:32
  • Are you developing for earlier versions than iOS 8? – brechtb Feb 10 '15 at 19:39
  • Yes, unfortunately. iOS 7 and 8 – JamieB Feb 10 '15 at 19:39
  • Thanks for updating the answer, I'm not by my macbook so I can't test - but is this usable on ios7? – JamieB Feb 10 '15 at 20:28
  • Yes, it worked in the iOS 7.1 simulator, didn't test on an actual device – brechtb Feb 10 '15 at 20:43
  • Hey I gave it a go, it works great in the designer but at runtime the view appears right aligned and not centred. – JamieB Feb 11 '15 at 10:19
  • 1
    [The Center Label Project!](https://www.dropbox.com/s/zsqdjhj1ajft8tr/CenteringThatLabel.zip?dl=1) Hope it helps ;) – brechtb Feb 11 '15 at 11:07
  • I've duplicated your constraints as far as I can see and I get this result at runtime (looks fine in designer) http://imgur.com/6DVDwY8,iDlxDnk there are two pictures in that link, one designer and one runtime. – JamieB Feb 11 '15 at 13:04
  • The only thing I can think of, is that your UITableView width is bigger than the screen, could you check that you're constraining the UITableView to be the same size as the screen? Maybe NSLog(@"%@", self.tableView.frame.size.width) – brechtb Feb 11 '15 at 15:12
  • I have a feeling that your suggestion / project was perfectly fine but as our project had initial not used auto layout there were some other problems that were occurring. In the end it took too much time of looking into so I switched to a centred label and used attributed text to get a similar effect. – JamieB Feb 12 '15 at 11:08