2012-01-08 21 views
41

मेरे पास एक UINavigationBar है जो मेरे UIViewController दृश्य में जोड़ा गया है। मैं फोंट गुणों को बदलना चाहता हूं। ध्यान दें कि मैं एक UINavigationBar नियंत्रक नहीं बदलना चाहता हूँ। मेरे ऐप में जहां मैं UINavigationController का उपयोग करता हूं, मैं कस्टम लेबल प्रदर्शित करने के लिए self.navigationItem.titleView = label; का उपयोग करता हूं।UINavigationBar फ़ॉन्ट गुण बदलें?

मैं अपने UINavigationBar में एक कस्टम शीर्षक कैसे प्राप्त कर सकता हूं?

पीएस। मैं अपने UINavigationBar के शीर्षक टेक्स्ट self.navBar.topItem.title = @"Information"; सेट करने के लिए इसका उपयोग करता हूं।

उत्तर

86

आईओएस 5 के बाद से हमें शीर्षक टेक्स्ट टेक्स्ट और टाइटलटेक्स्ट एट्रिब्यूट डिक्शनरी (यूइविगेशन कंट्रोलर क्लास रेफरेंस में पूर्वनिर्धारित शब्दकोश) का उपयोग करके नेविगेशन बार का फ़ॉन्ट सेट करना होगा।

[[UINavigationBar appearance] setTitleTextAttributes: 
    [NSDictionary dictionaryWithObjectsAndKeys: 
     [UIColor blackColor], NSForegroundColorAttributeName, 
      [UIFont fontWithName:@"ArialMT" size:16.0], NSFontAttributeName,nil]]; 

ट्यूटोरियल नीचे UInavigation बार की तरह UIElements के अनुकूलन के लिए सबसे अच्छा ट्यूटोरियल है, नियंत्रण UIsegmented UITabBar। यह आपको

http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

+2

@rolandjitsu पर सेट करें, आपको केवल UITextAttributeFont के बजाय NSFontAttributeName का उपयोग करना होगा। – CedricSoubrie

+1

@CedricSoubrie ~ हाँ, यह सच है, बस लेखक को इसके बारे में सूचित करना चाहता था ताकि हम एक अद्यतन उत्तर प्राप्त कर सकें :) – Roland

+3

और UITextAttributeTextColor को NSForegroundColorAttributeName के साथ प्रतिस्थापित किया जाना चाहिए – bobmoff

21

(यह नया आईओएस 5.0 उपस्थिति एपीआई का उपयोग करना संभव नहीं है)।

संपादित करें:

आईओएस> = 5.0:

सेट शीर्षक पाठ Navigationbar के लिए गुण:

// Customize the title text for *all* UINavigationBars 
NSDictionary *settings = @{ 
    UITextAttributeFont     : [UIFont fontWithName:@"YOURFONTNAME" size:20.0], 
    UITextAttributeTextColor   : [UIColor whiteColor], 
    UITextAttributeTextShadowColor  : [UIColor clearColor], 
    UITextAttributeTextShadowOffset  : [NSValue valueWithUIOffset:UIOffsetZero]}; 

[[UINavigationBar appearance] setTitleTextAttributes:settings]; 

आईओएस < 5,0

UINavigationItem नहीं है लेबल या कुछ नामक एक संपत्ति, केवल एक शीर्षक दृश्य। (सुझाव here के रूप में)

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)]; 
label.font = [UIFont fontWithName:@"YOURFONTNAME" size:20.0]; 
label.shadowColor = [UIColor clearColor]; 
label.textColor =[UIColor whiteColor]; 
label.text = self.title; 
self.navigationItem.titleView = label;  
[label release]; 
+0

दरअसल, टिप्पणी के लिए धन्यवाद। मैं उस समय नहीं मिला। – Tieme

+0

आपको बहुत धन्यवाद :) – Abo3atef

7

बस अपने एप्लिकेशन प्रतिनिधि के

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
में रखते: आप केवल इस शीर्षक दृश्य आप नीचे दिए गए कोड का उपयोग कर के रूप में एक कस्टम लेबल की स्थापना करके फ़ॉन्ट सेट करने में सक्षम हैं

रे Wenderlich से:

http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

[[UINavigationBar appearance] setTitleTextAttributes: 
[NSDictionary dictionaryWithObjectsAndKeys: 
[UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], 
UITextAttributeTextColor, 
[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
UITextAttributeTextShadowColor, 
[NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
UITextAttributeTextShadowOffset, 
[UIFont fontWithName:@"STHeitiSC-Light" size:0.0], 
UITextAttributeFont, nil]]; 
+0

अपने फ़ॉन्ट पर आकार 0 सेट न करें, जो दृश्य नियंत्रक को धक्का देते समय दृश्य त्रुटियों का कारण बन जाएगा। सबसे पहले जब आप फ़ॉन्ट को धक्का देंगे तो आकार 0 होगा और फिर यह उचित रूप से आकार देगा लेकिन यह अभी भी दिखाई दे रहा है - और कष्टप्रद। इसे 18.0 –

0

लिए सहायक हो सकता है अपने लक्ष्य के लिए फ़ॉन्ट को जोड़ने के लिए मत भूलना।

मैंने सबकुछ किया जो फ़ॉन्ट के बारे में किया जा सकता है और मैं इसे लोड नहीं कर सका, वास्तव में, फ़ॉन्ट मेरे लक्ष्य का सदस्य नहीं था।

तो कस्टम सदस्यता के साथ-साथ लक्षित सदस्यता की जांच करें।

1

नीचे लिंक आपको मदद करेगा। यह जहां सभी आवेदन में कहीं भी वर्तमान ViewController के नेविगेशन पट्टी पर फ़ॉन्ट गुणों को सेट करना चाहते हैं पर निर्भर करता है

आप अच्छा ट्यूटोरियल यहाँ% पा सकते हैं http://www.appcoda.com/customize-navigation-status-bar-ios-7/?utm_campaign=iOS_Dev_Weekly_Issue_118&utm_medium=email&utm_source=iOS%2BDev%2BWeekly

मूल विचार NSDictionary उदाहरण बना सकते हैं और साथ भरने के लिए है आपका वांछित फ़ॉन्ट और अन्य गुण।

UIColor *color = [UIColor redColor]; 
NSShadow *shadow = [NSShadow new]; 
UIFont *font = [UIFont fontWithName:@"EnterYourFontName" size:20.0] 
shadow.shadowColor = [UIColor greenColor]; 
shadow.shadowBlurRadius = 2; 
shadow.shadowOffset = CGSizeMake(1.0f, 1.0f); 

//fill this dictionary with text attributes 
NSMutableDictionary *topBarTextAttributes = [NSMutableDictionary new]; 
    //ios 7 
topBarTextAttributes[NSForegroundColorAttributeName] = color; 
    //ios 6 
topBarTextAttributes[UITextAttributeTextColor] = color; 
    //ios 6 
topBarTextAttributes[UITextAttributeTextShadowOffset] = [NSValue valueWithCGSize:shadow.shadowOffset]; 
topBarTextAttributes[UITextAttributeTextShadowColor] = shadow.shadowColor; 
    //ios 7 
topBarTextAttributes[NSShadowAttributeName] = shadow; 
    //ios 6 
topBarTextAttributes[UITextAttributeFont] = font; 
    //ios 7 
topBarTextAttributes[NSFontAttributeName] = font; 

//for all the controllers uncomment this line 
// [[UINavigationBar appearance]setTitleTextAttributes:topBarTextAttributes]; 

//for current controller uncoment this line 
// self.navigationController.navigationBar.titleTextAttributes = topBarTextAttributes; 
4

iOS8 तेज में, फ़ॉन्ट स्थापित करने के लिए निम्न कोड का उपयोग करें::

var navigationBarAppearance = UINavigationBar.appearance() 
let font = UIFont(name: "Open Sans", size: 17) 
if let font = font { 
    navigationBarAppearance.titleTextAttributes = [NSFontAttributeName: font, NSForegroundColorAttributeName: UIColor.whiteColor()] 
} 

के बाद [सुपर viewDidLoad] इस लाइनें डाल अपने -viewDidLoad नियंत्रक विधि में

: मैं इस समाधान के साथ समाप्त

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