Reputation: 38162
Is there any standard way to know the number of characters a UITableViewCell can occupy in one line (without wrapping) in regular and group table view? Basically some chart with text font size and number of characters.
Upvotes: 0
Views: 63
Reputation: 535557
There is no such thing as characters in a UITableViewCell. What you are presumably concerned with the characters in some interface object in a UITableViewCell - for example, perhaps it's a UILabel. The cell - at least, the way you've posed the question - is irrelevant.
If that's the case, you can use the label's features to learn whether a given piece of text would wrap. If you are using a plain NSString, look at the string drawing methods in the NSString UIKit Additions Reference. If you are using an NSAttributedString, look at the drawing methods in the NSAttributedString UIKit Additions Reference. Methods are provided that tell you how much space a given string will take up, in a given font, using a given line break option, when constrained to a given width - and you can get all that information from the label. You are guaranteed that the label uses the same string drawing methods, so the result that you get will be correct.
Upvotes: 2
Reputation: 45571
You can't just use number of characters and text size unless the font is fixed-width.
You need to have the string to display and ask for it's size using:
NSString *stringToCheck = @"...";
CGSize size = [stringToCheck sizeWithFont:[UIFont systemFontOfSize:[UIFont systemFontSize]]];
// Check the table view cell size available
CGSize cellSize = cell.contentView.bounds.size;
Upvotes: 1