UIFont: Je ne peux pas obtenir la taille de la police. Pourquoi ma police égale à zéro?
Question
Je ne peux pas obtenir la taille de la police. Pourquoi ma police égale à zéro?
- (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);
Et journal:
______font family.Helvetica NeueUI
______font name.HelveticaNeueUI-Bold
______font size0.000000
La solution
Je l'ai remarqué ce problème lorsque la première vue de tableau présenté est construit. vues ultérieures ne présentent pas ce comportement dans mon cas. En d'autres termes, la police est clairement initialisé après tableView: cellForRowAtIndexPath: pour la première vue de table construite uniquement .
Une façon de contourner cela est d'assurer l'initialisation correcte des polices dans le constructeur de la cellule:
Définir un nouveau constructeur UITableViewCell dans une catégorie et créer de nouvelles polices pour le Textlabel et detailTextLabel avec ce que jamais vous choisissez par défaut après avoir appelé super initWithStyle :.
Deux stratégies de construction:
-
utilisez uniquement ce nouveau constructeur dans les vues de table affectées, et faire que vos valeurs par défaut sont compatibles avec les paramètres par défaut du système d'exploitation.
-
remplacer Sinon tous les appels constructeur UITableViewCell avec ce nouveau constructeur pour assurer la cohérence de toutes les vues de table, et omettent le chèque de la pointSize. Je range du côté probablement avec cette stratégie puisque les valeurs par défaut sont soumis à des changements dans les révisions futures OS.
Vous pouvez utiliser les valeurs par défaut indiquées ici comme référence. Bien, je pense qu'ils ont changé depuis iOS 4.2: Taille par défaut de UITableViewCell
Vous pourriez alternativement les connecter en utilisant le simulateur pour les découvrir vous-même dans la seconde UITableView construit.
Exemple de code:
défauts cellulaires:
#define kCellStyleSubtitleTextLabelFont @"Helvetica-Bold"
#define kCellStyleSubtitleTextLabelFontSize 18.0f
#define kCellStyleSubtitleDetailLabelFont @"Helvetica"
#define kCellStyleSubtitleDetailLabelFontSize 14.0f
#define kCellStyle1TextLabelFont @"Helvetica-Bold"
#define kCellStyle1TextLabelFontSize 17.0f
#define kCellStyle1DetailLabelFont @"Helvetica Bold"
#define kCellStyle1DetailLabelFontSize 15.0f
#define kCellStyle2TextLabelFont @"Helvetica-Bold"
#define kCellStyle2TextLabelFontSize 12.0f
#define kCellStyle2DetailLabelFont @"Helvetica"
#define kCellStyle2DetailLabelFontSize 15.0f
UITableViewCell Catégorie:
@interface UITableViewCell (fontDefaults)
-(id) initWithCellStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier;
@end
#define kCheckPointSize 0
@implementation UITableViewCell (fontDefaults)
-(id) initWithCellStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [self initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self == nil)
return nil;
switch (style) {
case UITableViewCellStyleValue1: { //Left aligned label on left and right aligned label on right with blue text (Used in Settings)
#if kCheckPointSize
if(self.textLabel.font.pointSize == 0)
#endif
self.textLabel.font = [UIFont fontWithName:kCellStyle1TextLabelFont size:kCellStyle1TextLabelFontSize];
#if kCheckPointSize
if(self.detailTextLabel.font.pointSize == 0)
#endif
self.detailTextLabel.font = [UIFont fontWithName:kCellStyle1DetailLabelFont size:kCellStyle1DetailLabelFontSize];
break;
}
case UITableViewCellStyleValue2: { //Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
#if kCheckPointSize
if(self.textLabel.font.pointSize == 0)
#endif
self.textLabel.font = [UIFont fontWithName:kCellStyle2TextLabelFont size:kCellStyle2TextLabelFontSize];
#if kCheckPointSize
if(self.detailTextLabel.font.pointSize == 0)
#endif
self.detailTextLabel.font = [UIFont fontWithName:kCellStyle2DetailLabelFont size:kCellStyle2DetailLabelFontSize];
break;
}
case UITableViewCellStyleSubtitle: { //Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
#if kCheckPointSize
if(self.textLabel.font.pointSize == 0)
#endif
self.textLabel.font = [UIFont fontWithName:kCellStyleSubtitleTextLabelFont size:kCellStyleSubtitleTextLabelFontSize];
#if kCheckPointSize
if(self.detailTextLabel.font.pointSize == 0)
#endif
self.detailTextLabel.font = [UIFont fontWithName:kCellStyleSubtitleDetailLabelFont size:kCellStyleSubtitleDetailLabelFontSize];
break;
}
default: //default cell style
case UITableViewCellStyleDefault: { //Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x). No detailTextLabel
#if kCheckPointSize
if(self.textLabel.font.pointSize == 0) //
#endif
self.textLabel.font = [UIFont fontWithName:kCellStyle1TextLabelFont size:kCellStyle1TextLabelFontSize];
break;
}
}
return self;
}
@end
Exemple construction en tableView: cellForRowAtIndexPath:
static NSString *CellIdentifier = @"Style2Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithCellStyle:UITableViewCellStyleValue2
reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}