3

I have an App that has been working fine prior to iOS7, but now has issues with spaces in text of a tableview cell (style Right Detail). Some cells in the tableview have a discloser indicator and some not, so in order to align all the text to the right I have added a few extra spaces to the text in the cells that don't have disclosure indicators. See the date formatter (spaces after YYYY):

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation : @"UTC"];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"dd MMM YYYY    "]; // includes 4 spaces at the end for alignment
NSString *dateUTC = [formatter stringFromDate:[NSDate date]]; //get todays UTC date
DateCell.detailTextLabel.text = dateUTC;  

Will appreciate a better way that will also work in IOS7.

André
  • 105
  • 10
  • I would imagin this is by design, simply add the spaces to the string afterward, rather than as part of the string format. – Toby Allen Oct 20 '13 at 07:37
  • Hi Toby, I used stringWithFormat to display and added the spaces there but had no effect. – André Oct 20 '13 at 07:47
  • 3
    You shouldn't be using spaces for alignment. This is like people using spaces in Word to make things "right aligned" etc... you should really be using something like Auto Layout to make things line up. How do the dates 1 1 1111 and 30 12 2013 line up? 1 is a lot narrower and so will cause a misalign. – Fogmeister Oct 20 '13 at 08:21

5 Answers5

2

I think you should stop adding spaces separately and override UITableViewCell's layoutSubviews method like shown below:

- (void)layoutSubviews {
    [super layoutSubviews];

    CGRect newFrame = self.detailTextLabel.frame;
    newFrame.size.width -= 10;
    self.detailTextLabel.frame = newFrame;
}
nomann
  • 2,257
  • 2
  • 21
  • 24
  • Thank you, tried this too but still does not the desired effect. – André Oct 20 '13 at 08:18
  • Can you please add the screenshot? – nomann Oct 20 '13 at 08:20
  • 1
    I think you should remove the spaces altogether and subclass UITableViewCell like shown in my updated answer. – nomann Oct 20 '13 at 08:58
  • I quite liked the idea of subclassing the UITableCell, and tried it but this only changes the size of the detailTextLabel. Any ideas on how to shift the label's position to the left ? – André Oct 21 '13 at 09:07
  • Found the answer here: http://stackoverflow.com/questions/5411142/how-do-i-set-the-size-position-of-a-custom-uiview-for-its-placing-within-a-custo?rq=1 – André Oct 21 '13 at 09:56
  • Hi nomannasim, thank you, I liked your idea of using layoutSubviews and just modified your code to make it work for me. See answer below. – André Oct 22 '13 at 04:27
2

What if you set the accessoryView on views without the disclosure indicator to a clear UIView where the width is equal to the disclosure indicator...

That would be the best alternative as you can never be sure how many spaces you will need as character widths very on different fonts / characters

liamnichols
  • 12,419
  • 2
  • 43
  • 62
2

The best/only way to do this is to use a 'UITableViewCell' subclass.

Also, you can use static cells if you're using a 'UIStoryboard'.

Then you can lay the views out exactly as you want them to be laid out.

Using spaces is absolutely not the answer. Especially with iOS 7 where font sizes are determined by a user preference. If they increase the font size then suddenly your four spaces are too big and it won't look right.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • Agreed, using spaces to align the labels/ text was not the best way. Using UIStoryboard would have worked perfectly, but making a UITableViewCell sub class and using layoutSubviews was a bit quicker and easier for me. Thank you in any event for your advice. – André Oct 22 '13 at 22:38
1

Try with stringByPaddingToLegth in order to add spaces in your string:

yourString = [yourString stringByPaddingToLength:4 withString:@" " startingAtIndex:[yourString length]];

Hope it helps!

RFG
  • 2,880
  • 3
  • 28
  • 44
1

Thank you all for your input and I fully agree that using spaces to align labels/text is not a good idea, but it did the job for a while ... :) I managed to align the labels properly with nomannasim's idea of using layoutSubviews, just modified his code a little to work for me.

- (void)layoutSubviews
{
[super layoutSubviews];
CGRect newFrame = self.detailTextLabel.frame;
newFrame.origin.x = newFrame.origin.x - 20;
self.detailTextLabel.frame = newFrame;
}
André
  • 105
  • 10