
Bir var UIBarButtonItembenim de UIToolbarbaşlıklı Yapıldı . Şimdi yazı tipini varsayılandan Bold ile "Trebuchet MS" olarak değiştirmek istiyorum . Bunu nasıl yapabilirim?

Bir var UIBarButtonItembenim de UIToolbarbaşlıklı Yapıldı . Şimdi yazı tipini varsayılandan Bold ile "Trebuchet MS" olarak değiştirmek istiyorum . Bunu nasıl yapabilirim?
Yanıtlar:
UIBarButtonItem, UIBarItem'den miras aldığı için deneyebilirsiniz
- (void)setTitleTextAttributes:(NSDictionary *)attributes
forState:(UIControlState)state
ancak bu yalnızca iOS5 içindir. İOS 3/4 için özel bir görünüm kullanmanız gerekir.
Kesin olmak gerekirse, bu aşağıdaki gibi yapılabilir
[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica-Bold" size:26.0], NSFontAttributeName,
[UIColor greenColor], NSForegroundColorAttributeName,
nil]
forState:UIControlStateNormal];
Veya nesne değişmez sözdizimi ile:
[buttonItem setTitleTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Bold" size:26.0],
NSForegroundColorAttributeName: [UIColor greenColor]
} forState:UIControlStateNormal];
Kolaylık sağlamak için Swift uygulaması:
buttonItem.setTitleTextAttributes([
NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
NSAttributedStringKey.foregroundColor: UIColor.green],
for: .normal)
Kullanarak ilgilenen olanlar için UIAppearancestiline kendi UIBarButtonItemapp boyunca 'ın fontları, bu kod satırı kullanılarak gerçekleştirilebilir:
Hedef C:
NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]};
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal];
Hızlı 2.3:
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white
],
for: .normal)
Hızlı 3
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
Hızlı 4
UIBarButtonItem.appearance().setTitleTextAttributes(
[
NSAttributedStringKey.font : UIFont(name: "HelveticaNeue-Light", size: 12)!,
NSAttributedStringKey.foregroundColor : UIColor.white,
], for: .normal)
Veya tek bir UIBarButtonItem için (tüm uygulama için değil), özellikle bir düğme için özel bir yazı tipiniz varsa:
Hızlı 3
let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
NSFontAttributeName : UIFont(name: "FontAwesome", size: 26)!,
NSForegroundColorAttributeName : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"
Hızlı 4
let barButtonItem = UIBarButton()
barButtonItem.setTitleTextAttributes([
NSAttributedStringKey.font : UIFont(name: "FontAwesome", size: 26)!,
NSAttributedStringKey.foregroundColor : UIColor.white,
], for: .normal)
barButtonItem.title = "\u{f02a}"
Tabii ki, yazı tipini ve boyutunu istediğiniz gibi değiştirebilirsiniz. Bu kodu bölümdeki AppDelegate.mdosyaya koymayı tercih ederim didFinishLaunchingWithOptions.
Kullanılabilir öznitelikler (yalnızca şuraya ekleyin NSDictionary):
NSFontAttributeName: Yazı tipini bir UIFontNSForegroundColorAttributeName: İle renk değiştirme UIColorNSShadow: Alt gölge ekleyin ( sınıf referansına bakın NSShadow)(İOS7 + için güncellendi)
Swift'te bunu şu şekilde yaparsınız:
backButtonItem.setTitleTextAttributes([
NSFontAttributeName : UIFont(name: "Helvetica-Bold", size: 26)!,
NSForegroundColorAttributeName : UIColor.blackColor()],
forState: UIControlState.Normal)
Bunlar yukarıdaki harika cevaplar. Sadece iOS7 için güncelleme:
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)
hızlı
barbutton.setTitleTextAttributes([
NSFontAttributeName : UIFont.systemFontOfSize(18.0),
NSForegroundColorAttributeName : UIColor.redColor(),NSBackgroundColorAttributeName:UIColor.blackColor()],
forState: UIControlState.Normal)
Objective-C
[ barbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica-Bold" size:20.0], NSFontAttributeName,
[UIColor redColor], NSForegroundColorAttributeName,[UIColor blackColor],NSBackgroundColorAttributeName,
nil]
forState:UIControlStateNormal];
Bunu yapmak için bazıları UIBarButtonItemsdeğil ama hepsi için aşağıdaki yaklaşımı öneririm.
UIBarButtonItemalt sınıf oluşturun . Ona hiçbir şey eklemeyin - bunu yalnızca film şeridinde özel bir sınıf olarak ve görünüm proxy'si için kullanacaksınız ...UIBarButtonItemsalt sınıfınıza değiştirinUIBarButtonItemalt sınıfınızı içe aktarın ve aşağıdaki satırı ekleyinapplication:didFinishLaunchingWithOptions:Benim durumumda UIBarButtonItem, yalnızca metni kalınlaştırmak amacıyla alt sınıflara ayırdım:
[[BoldBarButtonItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont boldSystemFontOfSize:18.0], NSFontAttributeName,nil]
forState:UIControlStateNormal];
In Swift 4 , sen yazı tipini ve rengini değiştirebilirsiniz UIBarButtonItemaşağıdaki kodu ekleyerek.
addTodoBarButton.setTitleTextAttributes(
[
NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Bold", size: 17)!,
NSAttributedStringKey.foregroundColor: UIColor.black
], for: .normal)
Bu doğru yoldur: barButtonItem'inizi (bu durumda rightBarButtonItem) bildirin ve setTitleTextAttributes ekleyin.
navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Go!", style: .plain, target: self, action: #selector(yourFuncDestination))
başlık özellikleri ekledikten sonra
navigationItem.rightBarButtonItem?.setTitleTextAttributes([.font : UIFont.systemFont(ofSize: 18, weight: .bold), .foregroundColor : UIColor.white], for: .normal)
boyutunu, ağırlığını (.bold, .heavy, .regular vb.) ve tercih ettiğiniz rengi değiştirebilirsiniz ... Umarım bu yardım :)
Swift 5 Uygulaması
rightButtonItem.setTitleTextAttributes([
NSAttributedString.Key.font: UIFont(name: "Helvetica-Bold", size: 26.0)!,
NSAttributedString.Key.foregroundColor: UIColor.green],
for: .normal)
Uygulama Boyunca:
if let font = UIFont(name: "AvenirNext-DemiBold", size: 15) {
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName:TOOLBAR_TITLE_COLOR], forState: UIControlState.Normal)
}
İOS4 ve önceki sürümlerini desteklemek istediğinizi varsayarsak, en iyi seçeneğiniz initWithCustomView:yöntemi kullanarak bir çubuk düğmesi oluşturmak ve yazı tipinizi kolayca özelleştirebileceğiniz bir UIButton gibi bir şey olabilecek kendi görünümünüzü sağlamaktır.
Düğmeyi programlı yerine sürükleyip bırakarak oluşturmak istiyorsanız, bir UIButton'u Arayüz Oluşturucu'da bir araç çubuğuna veya gezinme çubuğuna da sürükleyebilirsiniz.
Ne yazık ki bu düğme arka plan resmini kendiniz oluşturmak anlamına gelir. İOS5'ten önce standart bir UIBarButtonItem'in yazı tipini özelleştirmenin bir yolu yoktur.
UIViewProgramlı olarak özel bir öğe oluşturabilirsiniz :
UIView *buttonItemView = [[UIView alloc] initWithFrame:buttonFrame];
Ardından özel görünümünüze resim, etiket veya istediğinizi ekleyin:
[buttonItemView addSubview:customImage];
[buttonItemView addSubview:customLabel];
...
Şimdi içine koy UIBarButtomItem.
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItemView];
Ve son olarak gezinti çubuğuna barButtonItem ekleyin.
Tamamlanması için hala 2019'da Objective-C'de kullanılan bu yöntemi eklemek istiyorum. :)
_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];