2009-01-07 12 views
21

मैंने सुझाए गए समाधान के साथ मेरी समस्या का विवरण के साथ "Sizing a UILabel (in the iPhone SDK) to fit?" अपना प्रश्न अपडेट किया है, लेकिन मुझे जवाब नहीं मिला। शायद मैं एक नया सवाल पूछकर बेहतर परिणाम होगा ...मैं कैसे प्राप्त करूं - [एनएसएसटींग आकार WithFont: forWidth: lineBreakMode:] काम करने के लिए?

मैं निम्नलिखित कोड के बाद एक ब्रेकपाइंट सेट:

NSString *theText = @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation."; 
CGSize theSize = [theText sizeWithFont:[UIFont systemFontOfSize:17.0f] forWidth:260.0 lineBreakMode:UILineBreakModeWordWrap]; 

theSize.height 21 और theSize.width 231. है क्या मैं गलत कर रहा हूँ? वह ऊंचाई पिक्सेल में नहीं हो सकती है न ही लाइनों की संख्या में।

ध्यान दें कि [UIFont systemFontOfSize:17.0f] डिफ़ॉल्ट फ़ॉन्ट निर्दिष्ट करने के लिए है।

अद्यतन: मैं करने के लिए कोड बदल दिया है:

CGSize constraintSize; 
constraintSize.width = 260.0f; 
constraintSize.height = MAXFLOAT; 
NSString *theText = @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation."; 
CGSize theSize = [theText sizeWithFont:[UIFont systemFontOfSize:17.0f] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap]; 

theSize.height 273. यही कारण है कि इसे और अधिक की तरह लगता है है।

+0

मेरे पास एक ही परिणाम था: 21. किसी को पता है कि वह संख्या कहां से आती है, या यह सिर्फ उस फ़ॉन्ट आकार के आधार पर है। मूर्ख विधि। – ZaBlanc

+0

यदि आप UITextView का उपयोग कर रहे हैं, तो मेरा जवाब यहां देखें: http://stackoverflow.com/questions/2330939/sizewithfont-doesnt-give-correct-height-for-uitextview-if-there-is-a -लॉन्ग-स्ट्रिन/15408198 # 15408198 – domsom

उत्तर

29

वह ऊंचाई पिक्सेल में है। मुझे उम्मीद है कि यह छंटनी हो रही है, यानी आपको मेट्रिक्स दे रहे हैं जो आप देखेंगे यदि आप इस पाठ को यूलाबेल पर 1 लाइन के साथ सेट करते हैं।

sizeWithFont:constrainedToSize: का उपयोग करने का प्रयास करें और आकार के ऊंचाई घटक के लिए इसे MAXFLOAT दें।

+0

यदि टेक्स्ट काफी लंबा है, तो अधिकतम ऊंचाई MAXFLOAT के साथ एक दृश्य बनाने से एप्लिकेशन क्रैश हो जाएगा। –

7

जैसा कि किसी ने इसे परेशान किया है, वह तरीका गलत तरीके से नामित है और जो आप उम्मीद करेंगे उसके साथ असंगत है (यह अच्छा एपीआई डिज़ाइन का उल्लंघन है, लेकिन जैसा कि किसी भी अनुभवी देव जानता है: ऐप्पल का एपीआई अच्छी तरह डिज़ाइन नहीं किया गया है, क्षमा करें दोस्तों)। पढ़ने योग्य मूल्य: Josh Bloch's presentation अच्छी एपीआई डिजाइन पर।

मुझे लगता है कि उन्हें इसका नाम बदलना चाहिए (अगर इसे रखा जाना चाहिए) और इसे एक उपयोगी बनाएं, जैसे कि आप CGSize में पास होने के सामान्य अभ्यास का उपयोग करके समाप्त नहीं होते हैं, जिसे आप जानते हैं बहुत बड़ा।

[someString sizeWithFont:yourFont 
     constrainedToSize:CGSizeMake(maxWidthYouSpecify, self.frame.size.height) 
      lineBreakMode:UILineBreakModeWordWrap]; 

वैकल्पिक रूप से इस बस के रूप में अच्छी तरह से काम करेगा:

[someString sizeWithFont:yourFont 
     constrainedToSize:CGSizeMake(maxWidthYouSpecify, CGFLOAT_MAX) 
      lineBreakMode:UILineBreakModeWordWrap]; 

यह है यकीनन, कैसे समारोह काम करना चाहिए। (एफडब्ल्यूआईडब्ल्यू, CGFLOAT_MAXCGGeometry में परिभाषित किया गया है)

एक तरफ, बल्कि महत्वपूर्ण: सभी कोर ग्राफिक्स अंक पिक्सल में सौदा नहीं करते हैं।

एक बिंदु स्क्रीन पर एक पिक्सेल के अनुरूप नहीं है।

एक महत्वपूर्ण अंतर है कि और एक आप को समझने के लिए जब विभिन्न प्रस्तावों के साथ (iPhone 3GS बनाम 4 बनाम आईपैड, आदि) काम कर की जरूरत है। ऐप्पल के docs और "पॉइंट्स बनाम पिक्सेल" के लिए कमांड + एफ देखें।

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