Quelqu'un peut-il m'informer sur le moyen le plus simple de modifier la taille de la police du texte dans l'en-tête d'une section UITableView?
J'ai les titres de section mis en œuvre en utilisant la méthode suivante:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Ensuite, je comprends comment changer avec succès la hauteur de l’en-tête de section en utilisant cette méthode:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
J'ai les cellules UITableView remplies en utilisant cette méthode:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Cependant, je ne sais pas comment augmenter réellement la taille de la police - ou le style de police utilisé - du texte d'en-tête de section?
Quelqu'un peut-il s'il vous plaît aider? Merci.
Malheureusement, vous devrez peut-être remplacer ceci:
En Objective-C:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
En rapide:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
Essayez quelque chose comme ça:
En Objective-C:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *myLabel = [[UILabel alloc] init];
myLabel.frame = CGRectMake(20, 8, 320, 20);
myLabel.font = [UIFont boldSystemFontOfSize:18];
myLabel.text = [self tableView:tableView titleForHeaderInSection:section];
UIView *headerView = [[UIView alloc] init];
[headerView addSubview:myLabel];
return headerView;
}
En rapide:
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let myLabel = UILabel()
myLabel.frame = CGRect(x: 20, y: 8, width: 320, height: 20)
myLabel.font = UIFont.boldSystemFont(ofSize: 18)
myLabel.text = self.tableView(tableView, titleForHeaderInSection: section)
let headerView = UIView()
headerView.addSubview(myLabel)
return headerView
}
Une autre façon de faire serait de répondre à la méthode UITableViewDelegate
_ willDisplayHeaderView
. La vue transmise est en fait une instance d'un UITableViewHeaderFooterView
.
L'exemple ci-dessous modifie la police et centre également le texte du titre verticalement et horizontalement dans la cellule. Notez que vous devez également répondre à heightForHeaderInSection
pour que toute modification de la hauteur de votre en-tête soit prise en compte dans la présentation de la vue tableau. (Autrement dit, si vous décidez de modifier la hauteur de l'en-tête avec cette méthode willDisplayHeaderView
.)
Vous pouvez ensuite répondre à la méthode titleForHeaderInSection
pour réutiliser cet en-tête configuré avec différents titres de section.
Objective-C
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.textLabel.textColor = [UIColor redColor];
header.textLabel.font = [UIFont boldSystemFontOfSize:18];
CGRect headerFrame = header.frame;
header.textLabel.frame = headerFrame;
header.textLabel.textAlignment = NSTextAlignmentCenter;
}
Swift 1.2
(Remarque: si votre contrôleur de vue est un descendant d'un UITableViewController
, il devra être déclaré en tant que override func
.)
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int)
{
let header:UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
header.textLabel.textColor = UIColor.redColor()
header.textLabel.font = UIFont.boldSystemFontOfSize(18)
header.textLabel.frame = header.frame
header.textLabel.textAlignment = NSTextAlignment.Center
}
Swift 3.
Ce code garantit également que l'application ne plante pas si votre vue d'en-tête est autre chose qu'un UITableViewHeaderFooterView:
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
guard let header = view as? UITableViewHeaderFooterView else { return }
header.textLabel?.textColor = UIColor.red
header.textLabel?.font = UIFont.boldSystemFont(ofSize: 18)
header.textLabel?.frame = header.frame
header.textLabel?.textAlignment = .center
}
Bien que la réponse de mosca1337 soit une solution correcte, soyez prudent avec cette méthode. Pour un en-tête avec du texte plus long qu'une ligne, vous devrez effectuer les calculs de la hauteur de l'en-tête dans tableView:heightForHeaderInSection:
, ce qui peut être fastidieux.
Une méthode très préférée consiste à utiliser l’API d’apparence:
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setFont:[UIFont boldSystemFontOfSize:28]];
Cela va changer la police, tout en laissant la table pour gérer les hauteurs elle-même.
Pour obtenir des résultats optimaux, sous-classez la vue tabulaire et ajoutez-la à la chaîne de confinement (dans appearanceWhenContainedIn:
) pour vous assurer que la police n'est modifiée que pour les vues tabulaires spécifiques.
Pour iOS 7, je l’utilise,
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.textLabel.font = [UIFont boldSystemFontOfSize:10.0f];
header.textLabel.textColor = [UIColor orangeColor];
}
Voici la version Swift 3. avec redimensionnement de l'en-tête
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let header = view as? UITableViewHeaderFooterView {
header.textLabel!.font = UIFont.systemFont(ofSize: 24.0)
header.textLabel!.textColor = UIColor.orange
}
}
Swift 3:
Moyen le plus simple de régler niquement taille:
func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
let header = view as! UITableViewHeaderFooterView
if let textlabel = header.textLabel {
textlabel.font = textlabel.font.withSize(15)
}
}
Swift 2.:
Implémentez viewForHeaderInSection, comme suit:
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionTitle: String = self.tableView(tableView, titleForHeaderInSection: section)!
if sectionTitle == "" {
return nil
}
let title: UILabel = UILabel()
title.text = sectionTitle
title.textColor = UIColor(red: 0.0, green: 0.54, blue: 0.0, alpha: 0.8)
title.backgroundColor = UIColor.clearColor()
title.font = UIFont.boldSystemFontOfSize(15)
return title
}
Implémentez willDisplayHeaderView, comme suit:
override func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let view = view as? UITableViewHeaderFooterView {
view.backgroundView?.backgroundColor = UIColor.blueColor()
view.textLabel!.backgroundColor = UIColor.clearColor()
view.textLabel!.textColor = UIColor.whiteColor()
view.textLabel!.font = UIFont.boldSystemFontOfSize(15)
}
}
Rappelez-vous: si vous utilisez des cellules statiques, le premier en-tête de section est rempli plus haut que les autres en-têtes de section en raison du sommet de UITableView; pour résoudre ce problème:
Implémentez heightForHeaderInSection, comme suit:
override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 30.0 // Or whatever height you want!
}
Avec cette méthode, vous pouvez définir taille de la police, style de la police et arrière-plan de l'en-tête également. il y a 2 méthode pour cela
Première méthode
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.backgroundView.backgroundColor = [UIColor darkGrayColor];
header.textLabel.font=[UIFont fontWithName:@"Open Sans-Regular" size:12];
[header.textLabel setTextColor:[UIColor whiteColor]];
}
Deuxième méthode
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
// myLabel.frame = CGRectMake(20, 8, 320, 20);
myLabel.font = [UIFont fontWithName:@"Open Sans-Regular" size:12];
myLabel.text = [NSString stringWithFormat:@" %@",[self tableView:FilterSearchTable titleForHeaderInSection:section]];
myLabel.backgroundColor=[UIColor blueColor];
myLabel.textColor=[UIColor whiteColor];
UIView *headerView = [[UIView alloc] init];
[headerView addSubview:myLabel];
return headerView;
}
Swift 4 version de Leo Natan réponse est
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = UIFont.boldSystemFont(ofSize: 28)
Si vous voulez définir une police personnalisée, vous pouvez utiliser
if let font = UIFont(name: "font-name", size: 12) {
UILabel.appearance(whenContainedInInstancesOf: [UITableViewHeaderFooterView.self]).font = font
}
Swift 2:
Comme l'OP l'a demandé, niquement ajustez la taille, sans la définir en tant que police système en gras ou quoi que ce soit:
func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView, textLabel = headerView.textLabel {
let newSize = CGFloat(16)
let fontName = textLabel.font.fontName
textLabel.font = UIFont(name: fontName, size: newSize)
}
}