
J'ai un UIBarButtonItemdans mon UIToolbartitre Done . Maintenant, je veux changer la police par défaut en "Trebuchet MS" avec Gras. Comment puis je faire ça?

J'ai un UIBarButtonItemdans mon UIToolbartitre Done . Maintenant, je veux changer la police par défaut en "Trebuchet MS" avec Gras. Comment puis je faire ça?
Réponses:
Étant donné que UIBarButtonItem hérite de UIBarItem, vous pouvez essayer
- (void)setTitleTextAttributes:(NSDictionary *)attributes
forState:(UIControlState)state
mais ce n'est que pour iOS5. Pour iOS 3/4, vous devrez utiliser une vue personnalisée.
Pour être précis, cela peut être fait comme ci-dessous
[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica-Bold" size:26.0], NSFontAttributeName,
[UIColor greenColor], NSForegroundColorAttributeName,
nil]
forState:UIControlStateNormal];
Ou avec la syntaxe littérale d'objet:
[buttonItem setTitleTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Bold" size:26.0],
NSForegroundColorAttributeName: [UIColor greenColor]
} forState:UIControlStateNormal];
Pour plus de commodité, voici l'implémentation Swift:
buttonItem.setTitleTextAttributes([
NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
NSAttributedStringKey.foregroundColor: UIColor.green],
for: .normal)
Pour ceux qui souhaitent utiliser UIAppearancepour styliser leurs UIBarButtonItempolices dans toute l'application, cela peut être accompli en utilisant cette ligne de code:
Objectif c:
NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
Swift 2.3:
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white
],
for: .normal)
Swift 3
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
Swift 4
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSAttributedStringKey.font : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSAttributedStringKey.foregroundColor : UIColor.white,
], for: .normal)
Ou pour un seul UIBarButtonItem (pas pour toutes les applications), si vous avez une police personnalisée pour un bouton en particulier:
Swift 3
let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
NSFontAttributeName : UIFont(name: "FontAwesome", size: 26)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"
Swift 4
let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
NSAttributedStringKey.font : UIFont(name: "FontAwesome", size: 26)!,
NSAttributedStringKey.foregroundColor : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"
Bien sûr, vous pouvez changer la police et la taille de votre choix. Je préfère mettre ce code dans le AppDelegate.mfichier dans la didFinishLaunchingWithOptionssection.
Attributs disponibles (ajoutez-les simplement au NSDictionary):
NSFontAttributeName: Changer la police avec un UIFontNSForegroundColorAttributeName: Changer de couleur avec un UIColorNSShadow: Ajouter une ombre portée (voir NSShadowréférence de classe)(Mis à jour pour iOS7 +)
Dans Swift, vous procédez comme suit:
backButtonItem.setTitleTextAttributes([
NSFontAttributeName : UIFont(name: "Helvetica-Bold", size: 26)!,
NSForegroundColorAttributeName : UIColor.blackColor()],
forState: UIControlState.Normal)
Ce sont d'excellentes réponses ci-dessus. Mise à jour juste pour iOS7:
NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Thin" size:18.0] , NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
Swift3
buttonName.setAttributedTitle([
NSFontAttributeName : UIFont.systemFontOfSize(18.0),
NSForegroundColorAttributeName : UIColor.red,NSBackgroundColorAttributeName:UIColor.black],
forState: UIControlState.Normal)
rapide
barbutton.setTitleTextAttributes([
NSFontAttributeName : UIFont.systemFontOfSize(18.0),
NSForegroundColorAttributeName : UIColor.redColor(),NSBackgroundColorAttributeName:UIColor.blackColor()],
forState: UIControlState.Normal)
Objectif c
[ barbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica-Bold" size:20.0], NSFontAttributeName,
[UIColor redColor], NSForegroundColorAttributeName,[UIColor blackColor],NSBackgroundColorAttributeName,
nil]
forState:UIControlStateNormal];
Pour ce faire pour certains UIBarButtonItemsmais pas tous, je recommande l'approche suivante.
UIBarButtonItemsous - classe. N'ajoutez rien - vous ne l'utiliserez que comme classe personnalisée dans le storyboard et pour son proxy d'apparence ...UIBarButtonItemsdans votre sous-classeUIBarButtonItemsous-classe et ajoutez la ligne suivante àapplication:didFinishLaunchingWithOptions:Dans mon cas, j'ai sous- UIBarButtonItemclassé dans le seul but de mettre le texte en gras:
[[BoldBarButtonItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:18.0], NSFontAttributeName,nil]
forState:UIControlStateNormal];
Dans Swift 4 , vous pouvez changer la police et la couleur de UIBarButtonItemen ajoutant le code suivant.
addTodoBarButton.setTitleTextAttributes(
[
NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 17)!,
NSAttributedStringKey.foregroundColor: UIColor.black
], for: .normal)
C'est la bonne façon: déclarez votre barButtonItem (dans ce cas rightBarButtonItem) et ajoutez-le setTitleTextAttributes.
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Go!", style: .plain, target: self, action: #selector(yourFuncDestination))
après avoir ajouté des attributs de titre
navigationItem.rightBarButtonItem?.setTitleTextAttributes([.font : UIFont.systemFont(ofSize: 18, weight: .bold), .foregroundColor : UIColor.white], for: .normal)
vous pouvez changer la taille, le poids (.bold, .heavy,. regular etc.) et la couleur comme vous préférez ... J'espère que cette aide :)
Implémentation Swift 5
rightButtonItem.setTitleTextAttributes([
NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
NSAttributedString.Key.foregroundColor: UIColor.green],
for: .normal)
Dans toute l'application:
if let font = UIFont(name: "AvenirNext-DemiBold", size: 15) {
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName:TOOLBAR_TITLE_COLOR], forState: UIControlState.Normal)
}
En supposant que vous souhaitiez prendre en charge iOS4 et les versions antérieures, votre meilleur pari est de créer un bouton de barre en utilisant la initWithCustomView:méthode et de fournir votre propre vue qui pourrait être quelque chose comme un UIButton où vous pouvez facilement personnaliser la police.
Vous pouvez également faire glisser un UIButton sur une barre d'outils ou une barre de navigation dans Interface Builder si vous souhaitez créer le bouton par glisser-déposer plutôt que par programmation.
Malheureusement, cela signifie créer vous-même l'image d'arrière-plan du bouton. Il n'y a aucun moyen de personnaliser la police d'un UIBarButtonItem standard avant iOS5.
Vous pouvez créer un UIViewprogramme personnalisé :
UIView *buttonItemView = [[UIView alloc] initWithFrame:buttonFrame];
Ensuite, ajoutez des images, des étiquettes ou tout ce que vous voulez à votre vue personnalisée:
[buttonItemView addSubview:customImage];
[buttonItemView addSubview:customLabel];
...
Maintenant, mettez-le dans votre UIBarButtomItem.
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItemView];
Et enfin, ajoutez barButtonItem à votre barre de navigation.
Pour terminer, j'aimerais ajouter cette méthode, toujours utilisée en Objective-C en 2019. :)
_titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
_titleLabel.text = _titleBarButtonItem.title;
_titleLabel.textColor = UIColor.whiteColor;
_titleLabel.font = [UtilityMethods appFontProDisplayBold:26.0];
[_titleLabel sizeToFit];
UIBarButtonItem *titleLabelItem = [[UIBarButtonItem alloc] initWithCustomView:_titleLabel];