I can’t get the size of font. Why does my font equal to zero?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kQuestionIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kQuestionIdentifier] autorelease];
}
cell.textLabel.text = [self extractText:indexPath forLabelAttribute:kTextLabel];
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.numberOfLines = 0;
NSLog(@"______font family%@", cell.textLabel.font.familyName);
NSLog(@"______font name%@", cell.textLabel.font.fontName);
NSLog(@"______font size%f", cell.textLabel.font.pointSize);
And log:
______font family.Helvetica NeueUI
______font name.HelveticaNeueUI-Bold
______font size0.000000
I’ve noticed this issue when the first table view presented is constructed. Subsequent views do not exhibit this behavior in my case. In other words, the font is clearly initialized after tableView:cellForRowAtIndexPath: for the first constructed table view only.
One way to get around this is to ensure the proper initialization of fonts in the cell constructor:
Define a new UITableViewCell constructor in a category and create new fonts for the textLabel and detailTextLabel with what ever defaults you choose after calling super initWithStyle:.
Two construction strategies:
only use this new constructor in affected table views, and make
sure your defaults are consistent with the OS defaults.
Alternatively replace all UITableViewCell constructor calls with
this new constructor to ensure consistency for all table views, and
omit the check for the pointSize. I would likely side with this strategy since the defaults are subject to change in future OS revisions.
You can use the defaults listed here as a reference. Although, I think they have changed since iOS 4.2: Default font size of UITableViewCell
You could alternatively log them using the simulator to discover them yourself in the second UITableView constructed.
Example code:
Cell defaults:
UITableViewCell Category:
Example construction in tableView:cellForRowAtIndexPath: