2012-08-23 35 views
12

मैं अपनी वर्तमान चौड़ाई के आधार पर एक टेक्स्ट फ़ील्ड का आकार बदलने/स्वचालित रूप से देखने का प्रयास कर रहा हूं। दूसरे शब्दों में, मैं चौड़ाई स्थिर रहना चाहता हूं लेकिन इसके लिए दिए गए पाठ के अनुसार ऊंचाई का आकार बदलना चाहता हूं।एनएसएसटींग बाउंडिंग RectWithSize सही ऊंचाई को थोड़ा कम करके कम करना - क्यों?

ऐसा लगता है कि ऐसा लगता है लेकिन कुछ कारणों से मैन्युअल रूप से इसे वापस ले जाने के प्रयासों के बावजूद मेरी खिड़की के नीचे संरेखित हो रहा है। क्या कोई देख सकता है कि मैं यहाँ क्या गलत कर रहा हूं?

enter image description here

NSString *temp = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vel felis nec massa ultricies blandit non id arcu. Sed enim est, facilisis a feugiat in, consectetur eget arcu. Aenean rutrum, lacus id euismod congue, nisl erat vulputate lectus, non faucibus tortor purus sed sem. Donec tempor dui egestas velit auctor vitae faucibus diam facilisis. Morbi convallis nulla quis est pulvinar quis ultricies sem sollicitudin."; 

myText.stringValue = temp; 

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSFont systemFontOfSize:12], NSFontAttributeName, 
          [NSParagraphStyle defaultParagraphStyle], NSParagraphStyleAttributeName, 
          nil]; 

NSSize size = NSMakeSize(window.frame.size.width, 200); 
myText.frame = [temp boundingRectWithSize:size options:NSLineBreakByWordWrapping | NSStringDrawingUsesLineFragmentOrigin attributes:attributes]; 

संपादित करें: यहां तक ​​कि जब मैन्युअल रूप से फ्रेम यह स्पष्ट है कि पाठ अभी भी काट रही है घूम रहा है। नया समायोजित आकार काफी नहीं है। enter image description here

इस के संदर्भ में है: NSString boundingRectWithSize slightly underestimating the correct width - why? और NSTextView or NSTextField automatically resize bounds/frame?

+0

हो सकता है कि जिस तरह से बड़ा कुछ करने के लिए 200 से ऊंचाई बदल सकते हैं कि फ्रेम आकार लंबवत बढ़ने के लिए जानता है। – rein

+0

@rein मैं इसके साथ खेलना जारी रखता हूं और जब यह वास्तव में नया फ्रेम फिर से आकार देता है तो यह अभी भी नीचे पाठ को क्लिप कर रहा है। नया फ्रेम काफी बड़ा नहीं है। एक नए स्क्रीनशॉट के लिए अद्यतन प्रश्न देखें। –

उत्तर

15

मुझे एक ही समस्या थी।

सही ढंग से आकर्षित और आकार बहु ​​लाइन पाठ, विकल्प पैरामीटर में NSStringDrawingUsesLineFragmentOrigin पास करने के लिए: मैं the Documentation में निम्नलिखित मिल गया।

यह विधि आंशिक आकार देता है ( के आकार घटक में CGRect लौटाया गया है); आकार के आकार में लौटा आकार का उपयोग करने के लिए, आपको छत समारोह का उपयोग करके निकटतम उच्च पूर्णांक के लिए का मूल्य बढ़ाना होगा।

तो यह मेरे लिए इसे हल:

CGRect rect = [myLabel.text boundingRectWithSize:CGSizeMake(myLabel.frame.size.width, CGFLOAT_MAX) 
                options:NSStringDrawingUsesLineFragmentOrigin 
               attributes:@{NSFontAttributeName: myLabel.font} 
                context:nil]; 
rect.size.width = ceil(rect.size.width); 
rect.size.height = ceil(rect.size.height); 

अद्यतन (स्विफ्ट 3)

एक वैकल्पिक स्विफ्ट 3 इस होगा करने के लिए जिस तरह से:

let size = myLabel.text!.size(
    attributes: [NSFontAttributeName: myLabel.font] 
) 
let rect = CGSize(
    width: ceil(size.width), 
    height: ceil(size.height) 
) 

देखें एक उद्देश्य-सी उदाहरण के लिए this answer

+0

इसके लिए धन्यवाद, मुझे बहुत मदद मिली – phyzalis

+2

NSLineBreakByWordWrapping के साथ या वैध नहीं है और वैसे भी मेरे लिए काम नहीं करता है। यह NSStringDrawingOptions enum का सदस्य नहीं है। – iphone007

+0

हम्म .. आप सही हैं! क्या आपने इसे 'NSStringDrawingUsesLineFragmentOrigin' के साथ आज़माया था? काम किया? मैं तब स्निपेट संपादित करूँगा। –

1

यह है कि मैं क्या के साथ जा रहा (कम से कम कुछ समय के लिए) यह ठीक काम करने लगता है समाप्त हो गया है, लेकिन मैं अभी भी वहाँ की तरह महसूस इसे पूरा करने का एक बेहतर तरीका है।

NSString *temp = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vel felis nec massa ultricies blandit non id arcu. Sed enim est. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vel felis nec massa ultricies blandit non id arcu. Sed enim est."; 

myText.stringValue = temp; 

NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: 
          [NSFont systemFontOfSize:12], NSFontAttributeName, 
          [NSParagraphStyle defaultParagraphStyle], NSParagraphStyleAttributeName, 
          nil]; 

NSSize size = NSMakeSize(window.frame.size.width, MAXFLOAT); 


myText.frame = [temp boundingRectWithSize:size options:NSLineBreakByWordWrapping | NSStringDrawingUsesLineFragmentOrigin attributes:attributes]; 

// Keep current width but add some more on the bottom 
NSSize tF = myText.frame.size; 
[myText setFrameSize:NSMakeSize(tF.width, tF.height + 35)]; 

[myText setFrameOrigin:NSMakePoint((NSWidth(window.frame) - NSWidth(myText.frame))/2, 
            (NSHeight(window.frame) - NSHeight(myText.frame) - 20))]; 

[myText setAutoresizingMask:NSViewMinXMargin | NSViewMaxXMargin | NSViewMinYMargin | NSViewMaxYMargin]; 

इस उत्पादन:

enter image description here

2

यह थोड़ा गोल चक्कर है, लेकिन मैं क्वेरी करने NSTextFieldCell अधिक सटीक/विश्वसनीय ऊंचाई परिणाम [NSAttributedString boundingRectWithSize:options:] का उपयोग करने से देता है पाया है।

मान लिया जाये कि ब्याज की स्ट्रिंग उचित स्वरूपण के साथ एक NSTextField में पहले से ही है/लपेटकर विकल्प सेट, तो क्या आकार यह सोचता है कि वह एक निर्धारित चौड़ाई में फिट करने के लिए की जरूरत है के लिए पाठ सेल क्वेरी:

NSTextField* textField = ...

NSSize cellSize = [textField.cell cellSizeForBounds:NSMakeRect(0, 0, pathText.frame.size.width, CGFLOAT_MAX)];

cellSize.height उस ऊंचाई को देता है जब पाठ वास्तव में खींचे जाने पर उपयोग करता है।

0

मैंने पाया कि NSString # boundingRectWithSize बिल्कुल सही नहीं है, केवल एक चीज अच्छी तरह से काम करने लगता है कि यहाँ वर्णित एक है: https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html

class func sizeForString(_ text : String, withFont font: NSFont, inWidth maxWidth: CGFloat) -> NSSize { 
    let textStorage = NSTextStorage(string: text) 
    let textContainer = NSTextContainer(containerSize: NSSize(width: maxWidth, height: CGFloat.greatestFiniteMagnitude)) 
    let layoutManager = NSLayoutManager() 
    layoutManager.addTextContainer(textContainer) 
    textStorage.addLayoutManager(layoutManager) 
    textStorage.addAttribute(NSFontAttributeName, value: font, range: NSMakeRange(0, textStorage.length)) 
textContainer.lineFragmentPadding = 0 
    layoutManager.glyphRange(for: textContainer) 
    return layoutManager.usedRect(for: textContainer).size 
}