2012-01-13 3 views
110

UIToolbar with UIBarButtonItemएक UIBarButtonItem

के फ़ॉन्ट बदलें मैं में मेरी UIToolbar शीर्षक हो गया एक UIBarButtonItem है। अब मैं डिफ़ॉल्ट से फ़ॉन्ट को "ट्रेबचेथ एमएस" बोल्ड के साथ बदलना चाहता हूं। मैं उसे कैसे कर सकता हूँ?

उत्तर

107

क्योंकि UIBarButtonItem UIBarItem से विरासत, आप

- (void)setTitleTextAttributes:(NSDictionary *)attributes 
        forState:(UIControlState)state 

कोशिश कर सकते हैं लेकिन यह केवल iOS5 के लिए है। आईओएस 3/4 के लिए, आपको कस्टम व्यू का उपयोग करना होगा।

+0

Belay कि, मैं एक समस्या यह है कि यह अब ठीक काम कर रहा है था। –

+0

आह! अंत में, मैंने तुम्हें @teriiehina पाया! – Martin

+0

@ मार्टिन मैं छुपा नहीं रहा था :) – teriiehina

0

UIBarButton में फ़ॉन्ट बदलने से संबंधित संपत्ति नहीं है। लेकिन आप कस्टम फ़ॉन्ट के साथ एक बटन बना सकते हैं और फिर UIBarButton में जोड़ सकते हैं। यह आपकी समस्या हल हो सकती है

0

मान लें कि आप आईओएस 4 और इससे पहले का समर्थन करना चाहते हैं, तो आपकी सबसे अच्छी शर्त initWithCustomView: विधि का उपयोग करके बार बटन बनाना है और अपना खुद का दृश्य प्रदान करना है जो UIButton की तरह कुछ हो सकता है जहां आप आसानी से अनुकूलित कर सकते हैं फ़ॉन्ट।

यदि आप प्रोग्रामेटिक रूप से ड्रैग-एंड-ड्रॉप के साथ बटन बनाना चाहते हैं तो आप इंटरफ़ेस बिल्डर में टूलबार या नेविगेशन बार पर UIButton भी खींच सकते हैं।

दुर्भाग्यवश इसका मतलब है कि बटन पृष्ठभूमि छवि स्वयं बनाना। आईओएस 5 से पहले मानक UIBarButtonItem के फ़ॉन्ट को कस्टमाइज़ करने का कोई तरीका नहीं है। अब

[buttonItemView addSubview:customImage]; 

[buttonItemView addSubview:customLabel]; 

... 

यह आपके UIBarButtomItem में डाल:

UIView *buttonItemView = [[UIView alloc] initWithFrame:buttonFrame]; 

तो छवियों, लेबल या जो भी आप अपने कस्टम दृश्य चाहते जोड़ें:

0

आप एक कस्टम UIView प्रोग्राम के रूप में बना सकते हैं।

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonItemView]; 

और अंत में आपको नेविगेशन बार में बारबटन इटिम जोड़ें।

187

सटीक होना, इस

[buttonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
    [UIFont fontWithName:@"Helvetica-Bold" size:26.0], NSFontAttributeName, 
    [UIColor greenColor], NSForegroundColorAttributeName, 
    nil] 
          forState:UIControlStateNormal]; 

नीचे के रूप में या वस्तु शाब्दिक वाक्य रचना के साथ किया जा सकता है:

buttonItem.setTitleTextAttributes([ 
     NSFontAttributeName: UIFont(name: "Helvetica-Bold", size: 26.0)!, 
     NSForegroundColorAttributeName: UIColor.greenColor()], 
    forState: UIControlState.Normal) 
+2

आईएसओ 7 के लिए विकास करते समय UITextAttributeFont, और NSForegroundColorAttributeName को UITextAttributeTextColor के बजाय होना चाहिए। – Raz

+0

धन्यवाद! यह ठीक काम कर रहा है। – Raja

87

के लिए:

[buttonItem setTitleTextAttributes:@{ 
    NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Bold" size:26.0], 
    NSForegroundColorAttributeName: [UIColor greenColor] 
} forState:UIControlStateNormal]; 

सुविधा के लिए, यहां स्विफ्ट कार्यान्वयन है UIAppearance का उपयोग करने में रुचि रखने वाले लोग अपने UIBarButtonItem के फ़ॉन्ट्स को टी भरने के लिए वह अनुप्रयोग, यह कोड की इस पंक्ति का उपयोग कर पूरा किया जा सकता:

उद्देश्य सी:

NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Light" size:12.0], NSForegroundColorAttributeName: [UIColor whiteColor]}; 
[[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal]; 

स्विफ्ट 2।3:

UIBarButtonItem.appearance().setTitleTextAttributes(
[ 
    NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!, 
    NSForegroundColorAttributeName : UIColor.white 
], 
for: .normal) 

स्विफ्ट 3,0/4

UIBarButtonItem.appearance().setTitleTextAttributes(
[ 
    NSFontAttributeName : UIFont(name: "HelveticaNeue-Light", size: 12)!, 
    NSForegroundColorAttributeName : UIColor.white, 
], for: .normal) 

या फिर इकलौते UIBarButtonItem (सभी एप्लिकेशन विस्तृत के लिए नहीं) के लिए, आप विशेष रूप से एक बटन के लिए एक कस्टम फ़ॉन्ट है, तो:

let barButtonItem = UIBarButton() 
barButtonItem.setTitleTextAttributes([ 
    NSFontAttributeName : UIFont(name: "FontAwesome", size: 26)!, 
    NSForegroundColorAttributeName : UIColor.white, 
], for: .normal) 
barButtonItem.title = "\u{f02a}" 

बेशक, आप जो भी चाहें फ़ॉन्ट & आकार बदल सकते हैं। मैं इस कोड को फ़ाइल में didFinishLaunchingWithOptions अनुभाग में रखना पसंद करता हूं। एक UIColor

  • NSShadow के साथ रंग बदलें: एक UIFont
  • NSForegroundColorAttributeName साथ फॉन्ट बदलें:

    उपलब्ध विशेषताएं (बस NSDictionary में शामिल करें):

    (आईओएस 7 + के लिए अपडेट किया गया)

  • +3

    UITextAttribute .. कुंजी को आईओएस 7 के रूप में बहिष्कृत किया गया है। NSFontAttribute ... और NSForeground ... आदि का उपयोग करें। –

    +2

    मैंने आईओएस 7 में पेश किए गए बहिष्करण के लिए अद्यतन किया है। सिर के लिए धन्यवाद, @EmmanuelAy! – rog

    +0

    शानदार! मेरा ऐप अब खूबसूरत दिख रहा है (ठीक है, मेरे अपने तरीके से !! - पी) धन्यवाद – Septronic

    7

    कुछ UIBarButtonItems के लिए ऐसा करने के लिए, लेकिन मैं निम्नलिखित दृष्टिकोण की अनुशंसा नहीं करता हूं।

    1. UIBarButtonItem सबक्लास बनाएं। अपने उपवर्ग
    2. करने के लिए सभी वांछित UIBarButtonItems के लिए आप केवल स्टोरीबोर्ड में और अपनी उपस्थिति प्रॉक्सी के लिए एक कस्टम वर्ग के रूप में उपयोग होगा ...
    3. अपने स्टोरीबोर्ड में, बदल कस्टम कक्षा में - यह कुछ भी जोड़ने मत अपने AppDelegate अपने UIBarButtonItem उपवर्ग आयात और application:didFinishLaunchingWithOptions:

    में निम्नलिखित पंक्ति जोड़ मेरे मामले में मैं बोल्ड करने के एकमात्र उद्देश्य के लिए UIBarButtonItem subclassed पाठ:

    [[BoldBarButtonItem appearance] setTitleTextAttributes: 
        [NSDictionary dictionaryWithObjectsAndKeys: 
        [UIFont boldSystemFontOfSize:18.0], NSFontAttributeName,nil] 
                   forState:UIControlStateNormal]; 
    
    16

    ये ऊपर महान जवाब हैं।बस iOS7 के लिए अद्यतन करने:

    NSDictionary *barButtonAppearanceDict = @{NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue-Thin" size:18.0] , NSForegroundColorAttributeName: [UIColor whiteColor]}; 
        [[UIBarButtonItem appearance] setTitleTextAttributes:barButtonAppearanceDict forState:UIControlStateNormal]; 
    
    18

    स्विफ्ट में आप के रूप में पालन इस करना होगा:

    backButtonItem.setTitleTextAttributes([ 
         NSFontAttributeName : UIFont(name: "Helvetica-Bold", size: 26)!, 
         NSForegroundColorAttributeName : UIColor.blackColor()], 
        forState: UIControlState.Normal) 
    
    +1

    फ़ॉन्ट के बाद विस्मयादिबोधक चिह्न (!) को न भूलें। त्रुटि संदेश: '' _ 'परिवर्तनीय नहीं है' स्ट्रिंग '' वास्तव में सहायक नहीं है। – Ciryon

    15

    Swift3

    buttonName.setAttributedTitle([ 
         NSFontAttributeName : UIFont.systemFontOfSize(18.0), 
         NSForegroundColorAttributeName : UIColor.red,NSBackgroundColorAttributeName:UIColor.black], 
                forState: UIControlState.Normal) 
    

    तेज

    barbutton.setTitleTextAttributes([ 
         NSFontAttributeName : UIFont.systemFontOfSize(18.0), 
         NSForegroundColorAttributeName : UIColor.redColor(),NSBackgroundColorAttributeName:UIColor.blackColor()], 
         forState: UIControlState.Normal) 
    

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

     [ barbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
         [UIFont fontWithName:@"Helvetica-Bold" size:20.0], NSFontAttributeName, 
         [UIColor redColor], NSForegroundColorAttributeName,[UIColor blackColor],NSBackgroundColorAttributeName, 
         nil] 
         forState:UIControlStateNormal]; 
    
    1

    अनुप्रयोग के दौरान:

    if let font = UIFont(name: "AvenirNext-DemiBold", size: 15) { 
         UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName:TOOLBAR_TITLE_COLOR], forState: UIControlState.Normal) 
    
        } 
    
    2

    तेज 3

    barButtonName.setTitleTextAttributes([NSFontAttributeName : UIFont.systemFont(ofSize: 18.0),NSForegroundColorAttributeName : UIColor.white], for: .normal) 
    

     संबंधित मुद्दे

    • कोई संबंधित समस्या नहीं^_^