2010-04-27 13 views
32

मुझे कस्टम UIBarButtonItem में शीर्षक के फ़ॉन्ट आकार को सेट करने का कोई तरीका नहीं मिल रहा है। इस बारे में सोचने का एकमात्र तरीका यह है कि इसे एक ऐसी छवि के रूप में सेट करें जिसे मैं टालना चाहूंगा। कोई अन्य सुझाव?आप UIBarButtonItem पर फ़ॉन्ट आकार कैसे सेट करते हैं?

+0

यहाँ एक समान सवाल है। http://stackoverflow.com/questions/5421121/change-font-size-via-uibarbuttonitem-in-uitextview-works-just-once – acecapades

उत्तर

3

एक UILabel बनाएं और -initWithCustomView: का उपयोग करें।

+0

तो मैं UIBarButtonItem UILabel में पारित और फ़ॉन्ट सामान्य रूप से स्थापित कर सकता ? मुझे आपकी विधि में भी समस्या हो सकती है क्योंकि मैं UIBarButtonItem को UIToolbar में रखने के लिए इंटरफ़ेस बिल्डर का उपयोग कर रहा हूं। – Jim

+1

सेटटेक्स्ट एट्रिब्यूट्स का उपयोग करें: forState: – bigkm

2

क्या KennyTM पता चलता है की एक ठोस उदाहरण के रूप में, आप UIBarButtonItem (कोड में) निम्नलिखित की तरह कुछ के साथ बनाने के लिए:

UILabel *txtLabel = [[UILabel alloc] initWithFrame:rect]; 
txtLabel.backgroundColor = [UIColor clearColor]; 
txtLabel.textColor = [UIColor lightGrayColor]; 
txtLabel.text = @"This is a custom label"; 
UIBarButtonItem *btnText = [[[UIBarButtonItem alloc] initWithCustomView:txtLabel] autorelease]; 

उसके बाद, आप यह केंद्रित पाठ के रूप में एक UIToolbar पर (उदाहरण के लिए) जोड़ सकते हैं निम्नलिखित के साथ:

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:rect]; 
toolBar.barStyle = UIBarStyleBlackTranslucent; 
UIBarButtonItem *flexSpace1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease]; 
UIBarButtonItem *flexSpace2 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease]; 

[toolBar setItems:[NSArray arrayWithItems:flexSpace1, btnText, flexSpace2, nil]]; 

(उचित स्वरूपण प्राप्त करने के लिए, ज़ाहिर है, recttxtLabel और toolBar आरंभ उचित आकार होना चाहिए के लिए इस्तेमाल किया .... लेकिन यह है कि एक और व्यायाम है!)

+1

आप इसे [txtLabel sizeToFit] का उपयोग कर सही आकार बना सकते हैं। एक साइड नोट के रूप में मुझे यकीन नहीं है कि यह विधि ब्लू कैप्सूल पृष्ठभूमि को एक बटन आइटम बनाए रखेगी, इसलिए आपको इसके साथ कुछ कस्टम ग्राफिक्स का उपयोग करना पड़ सकता है। लेकिन यह आपकी सबसे अच्छी शर्त है क्योंकि यदि आप इसे शीर्षक के साथ प्रारंभ करते हैं तो आप वास्तव में बार बटन आइटम का फ़ॉन्ट आकार सेट नहीं कर सकते हैं। – axiixc

+0

आपको लेबल के फ़्रेम-आकार को भी सेट करना चाहिए। मेरे मामले में कोई आइटम जहां प्रदर्शित नहीं किया गया था क्योंकि वे 0. – sprinter252

78

एक आसान तरीका में, बस:

ऑब्जेक्टिव-सी:

NSUInteger fontSize = 20; 
UIFont *font = [UIFont boldSystemFontOfSize:fontSize]; 
NSDictionary *attributes = @{NSFontAttributeName: font}; 

UIBarButtonItem *item = [[UIBarButtonItem alloc] init]; 

[item setTitle:@"Some Text"]; 
[item setTitleTextAttributes:attributes forState:UIControlStateNormal]; 

self.navigationItem.rightBarButtonItem = item; 

स्विफ्ट:

let fontSize:CGFloat = 20; 
let font:UIFont = UIFont.boldSystemFont(ofSize: fontSize); 
let attributes:[String : Any] = [NSFontAttributeName: font]; 

let item = UIBarButtonItem.init(); 

item.title = "Some Text"; 
item.setTitleTextAttributes(attributes, for: UIControlState.normal); 

self.navigationItem.rightBarButtonItem = item; 
+1

की चौड़ाई और ऊंचाई के साथ प्रारंभ हुए थे, आप 'UIAppearance' प्रॉक्सी का उपयोग 'setTitleTextAttributes'' पर भी कर सकते हैं। – livingtech

+4

मामूली नोट: UITextAttributeFont कुंजी मान को आईओएस 7 में बहिष्कृत कर दिया गया है, आप इसके बजाय NSFontAttributeName का उपयोग कर सकते हैं। –

+0

महान, धन्यवाद .. !!! – tesmojones

2
[[UIBarButtonItem appearance]setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName, 
                [UIFont fontWithName:@"FONT-NAME" size:21.0], NSFontAttributeName, nil] 
              forState:UIControlStateNormal]; 
संबंधित मुद्दे