2015-03-01 30 views
5

UINavigationItem için font özelliğini değiştirmeye çalışıyorum.UINavigationItem yazı tipi nasıl değiştirilir?

titleTextAttributes kullanmayı denedim ancak bir şekilde yalnızca başlık yazı tipini değiştirebiliyorum.

Yazı tipini veya UINavigationItem öğesini nasıl değiştirebilirim? Ben kullanarak denedim

enter image description here

: Örneğin, Geri UIButton için "Daha fazla" metni aşağıda gösterilmiştir

self.navigationController?.navigationBar.titleTextAttributes = [ 
      NSForegroundColorAttributeName: UIColor.blackColor(), 
      NSFontAttributeName: UIFont(name: "Papyrus", size: 18)! 
     ] 

ancak yalnızca resmin değil gösterilir ne gibi değişiklikler "More" Button'un Yazı Tipi.

cevap

10

sen benim bütün proje için global olarak Nav Bar özelleştirmek için bu işlevi kullanın setTitleTextAttributes:forState: kullanmak zorunda olduğunda sadece titleTextAttributes = ... kullandığınız çünkü yöntem çalışmıyor nedeni şudur: Tek bir For

func customizeNavBar(#color: UIColor, titleFont: UIFont, buttonFont: UIFont) { 

    UINavigationBar.appearance().tintColor = color 
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: color, NSFontAttributeName: titleFont] 
    UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: color, NSFontAttributeName: buttonFont], forState: UIControlState.Normal) 
} 

örneğin, aynı işlevleri çağırmak:

someBarButton.tintColor.setTitleTextAttributes([NSForegroundColorAttributeName: color, NSFontAttributeName: buttonFont], forState: UIControlState.Normal) 
+0

Bu, tüm düğmeler için yazı tipini değiştirir, yalnızca belirli bir sınıf/Gezinme Denetimi – Fengson

+0

için bir tane değiştirmem gerekiyor. Sonra UINavigationBar.appearance() 'yerine, aynı işlevleri UINavigationBar örneğinde çağırırsınız (Muhtemelen' navigationController.navigationBar') yanıtı güncellendi – Ian

+0

Bunu zaten denedim, ama bir hata alıyorum "UINavigationItem setTitleTextAttributes adlı bir mamber yok. Ayrıca navigasyonBar ... İlk mesajımda sunduğum sadece birini kullanabilirim ..: Fengson

0

Swift 4

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal) 
İlgili konular