14

मेरे पास कुछ दृश्य हैं जो नेविगेशन नियंत्रक में दिखाई देते हैं। नेविगेशन बार के लिए इनमें से दो विचारों का लंबा शीर्षक है।गतिशील रूप से नेविगेशन बार में शीर्षक का आकार कैसे बदलें

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

क्या कोई तरीका है कि मैं नेविगेशन बार को स्वचालित रूप से फिट करने के लिए शीर्षक टेक्स्ट को फिर से आकार देने के लिए कह सकता हूं?

+0

इस लिंक http://stackoverflow.com/questions/2422383/uinavigationbar-multi-line-title यह आपकी समस्या को उम्मीद है कि हल करती है जाएगा जाँच करें। – gopal

उत्तर

30

viewDidLoad में नीचे दिए गए कोड में प्रयुक्त की जरूरत है।

उद्देश्य सी

self.title = @"Your TiTle Text"; 
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)]; 
tlabel.text=self.navigationItem.title; 
tlabel.textColor=[UIColor whiteColor]; 
tlabel.font = [UIFont fontWithName:@"Helvetica-Bold" size: 30.0]; 
tlabel.backgroundColor =[UIColor clearColor]; 
tlabel.adjustsFontSizeToFitWidth=YES; 
tlabel.textAlignment = NSTextAlignmentCenter; 
self.navigationItem.titleView=tlabel; 

स्विफ्ट संस्करण

self.title = "Your TiTle Text" 
let tlabel = UILabel(frame: CGRectMake(0, 0, 200, 40)) 
tlabel.text = self.title 
tlabel.textColor = UIColor.whiteColor() 
tlabel.font = UIFont(name: "Helvetica-Bold", size: 30.0) 
tlabel.backgroundColor = UIColor.clearColor() 
tlabel.adjustsFontSizeToFitWidth = true 
tlabel.textAlignment = .center; 
self.navigationItem.titleView = tlabel 

यह you.Thanks

+0

धन्यवाद बॉब जो खूबसूरती से काम करता है! – PhoenixDev

+0

आपका स्वागत है! – jamil

0

आप UILabel के साथ नेविगेशन पट्टी शीर्षक दृश्य को अनुकूलित और फ़ॉन्ट आकार को समायोजित प्रदान करने के लिए ..

[self.navigationItem setTitleView:<"Include any UI View subclass">]; 
-1

लिए काम करता है यहाँ स्विफ्ट में एक उदाहरण है कि भी कई पंक्तियों के लिए अनुमति देता है आशा है। ऑटो लेआउट को सरल बनाने के लिए PureLayout का उपयोग करना।

override func viewDidLoad() { 
    super.viewDidLoad() 
    configureTitleView() 
} 

func configureTitleView() { 
    let titleLabel = UILabel() 
    titleLabel.numberOfLines = 0 
    titleLabel.textAlignment = .Center 
    titleLabel.font = UIFont.boldSystemFontOfSize(17.0) 
    titleLabel.text = searchLoc.mapItem.name 
    navigationItem.titleView = titleLabel 
    titleLabel.autoPinEdgesToSuperviewMargins() // PureLayout method 
    titleLabel.adjustsFontSizeToFitWidth = true 
} 

और एक के उपयोग का उदाहरण:

enter image description here

+0

काम करने के लिए सीम नहीं है क्योंकि जब शीर्षक Label.autoPinEdgesToSuperviewMargins कहा जाता है, शीर्षक लेबल का पर्यवेक्षण (अभी भी) शून्य है और इस प्रकार बाधाओं का दावा करता है। (xcode 7.3, आईओएस 9) – HixField

1

ऊपर समाधान सीवन में से कोई भी मेरे लिए मज़बूती से काम करने के लिए। हालांकि मुझे प्रदान किए गए उत्तरों के विभिन्न तत्वों का उपयोग करके समाधान मिला, यह स्विफ्ट 2 में है और वास्तव में सुरुचिपूर्ण है क्योंकि प्रत्येक बार जब आप लेबल बदलते हैं तो किसी भी कस्टम कोड की आवश्यकता नहीं होती है, यह शीर्षक पर संपत्ति पर्यवेक्षकों का उपयोग करता है।

ध्यान दें कि मेरे मामले में, मेरे पास नेविगेशन बार के बाईं तरफ एक बैक बटन था, जिसने स्क्रीन को स्क्रीन के केंद्र से बाहर रखा था, इसे ठीक करने के लिए मैं जिम्मेदार टेक्स्ट और टेलिंडेंट का उपयोग कर रहा हूं। सभी टिप्पणियां/नीचे कोड में जानकारी:

class VCHowToTopic : UIViewController { 


    //add handlers so that any manipulation of the title is caught and transferred to the custom drawn UILabel 
    override var title : String? { 
     set { 
      super.title = newValue 
      configureTitleView() 
     } 
     get { 
      return super.title 
     } 
    } 

    //MARK: - lifecycle 


    func configureTitleView() { 
     //some large number that makes the navigationbar schrink down our view when added 
     let someVeryLargeNumber = CGFloat(4096) 
     //create our label 
     let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: someVeryLargeNumber, height: someVeryLargeNumber)) 
     //0 means unlimited number of lines 
     titleLabel.numberOfLines = 0 
     //define style of the text (we will be using attributed text) 
     let style = NSMutableParagraphStyle() 
     style.alignment = .Center 
     //top compensate for the backbutton which moves the centered text to the right side of the screen 
     //we introduce a negative tail indent, the number of 56 has been experimentally defined and might 
     //depend on the size of your custom back button (if you have one), mine is 22x22 px 
     style.tailIndent = -56 
     //create attributed text also with the right color 
     let attrText = NSAttributedString(string: title!, attributes: [NSParagraphStyleAttributeName : style, 
      NSForegroundColorAttributeName : UIColor.whiteColor()]) 
     //configure the label to use the attributed text 
     titleLabel.attributedText = attrText 
     //add it as the titleview 
     navigationItem.titleView = titleLabel 
    } 


} 
+0

'सीजीईजीईटी (एक्स: 0, वाई: 0, चौड़ाई: कुछवृत्त संख्या संख्या, ऊंचाई: कुछVeryLargeNumber) शीर्षक को दृश्य में दृश्यमान बनाने की कुंजी है –

5

स्वीकृत की स्विफ्ट संस्करण उत्तर केन्द्र पर लेबल पाठ डाल +:

स्विफ्ट 2.3:

self.title = "Your TiTle Text" 
    let tlabel = UILabel(frame: CGRectMake(0, 0, 200, 40)) 
    tlabel.text = self.title 
    tlabel.textColor = UIColor.whiteColor() 
    tlabel.font = UIFont.boldSystemFontOfSize(17) //UIFont(name: "Helvetica", size: 17.0) 
    tlabel.backgroundColor = UIColor.clearColor() 
    tlabel.adjustsFontSizeToFitWidth = true 
    tlabel.textAlignment = .Center 
    self.navigationItem.titleView = tlabel 

और स्विफ्ट 3:

self.title = "Your TiTle Text" 
    let frame = CGRect(x: 0, y: 0, width: 200, height: 40) 
    let tlabel = UILabel(frame: frame) 
    tlabel.text = self.title 
    tlabel.textColor = UIColor.white 
    tlabel.font = UIFont.boldSystemFont(ofSize: 17) //UIFont(name: "Helvetica", size: 17.0) 
    tlabel.backgroundColor = UIColor.clear 
    tlabel.adjustsFontSizeToFitWidth = true 
    tlabel.textAlignment = .center 
    self.navigationItem.titleView = tlabel 
0

यदि आपके पास शीर्षक दृश्य में जोड़ा गया दृश्य है, और आप दृश्य का आकार बदलना चाहते हैं, तो आप इस कोड का उपयोग कर सकते हैं (स्विफ्ट 3):

self.translatesAutoresizingMaskIntoConstraints = false 
self.layoutIfNeeded() 
self.sizeToFit() 
self.translatesAutoresizingMaskIntoConstraints = true 
संबंधित मुद्दे