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.

3
  • I would imagin this is by design, simply add the spaces to the string afterward, rather than as part of the string format. Commented Oct 20, 2013 at 7:37
  • Hi Toby, I used stringWithFormat to display and added the spaces there but had no effect. Commented Oct 20, 2013 at 7: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. Commented Oct 20, 2013 at 8:21

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.
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; } 

6 Comments

Thank you, tried this too but still does not the desired effect.
Can you please add the screenshot?
I think you should remove the spaces altogether and subclass UITableViewCell like shown in my updated answer.
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 ?
|
1

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

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

Hope it helps!

1 Comment

don't 'advertise' using spaces for alignment. See Fogmeister's comment for example
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; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.