2012-02-22 18 views
10

के साथ NSSecureTextField में लंबवत रूप से केंद्र टेक्स्ट मैं अपने एनएसटीक्स्टफिल्ड्स में लंबवत रूप से टेक्स्ट टेक्स्ट करने की कोशिश कर रहा हूं, लेकिन उनमें से एक पासवर्ड के लिए है, इसलिए यह एक NSSecureTextField है। मैं इसे की क्लास निम्नलिखित कार्यान्वयन के साथ MDVerticallyCenteredSecureTextFieldCell होने के लिए निर्धारित किया है:उप-वर्गीकरण

- (NSRect)adjustedFrameToVerticallyCenterText:(NSRect)frame { 
    // super would normally draw text at the top of the cell 
    NSInteger offset = floor((NSHeight(frame) - 
           ([[self font] ascender] - [[self font] descender]))/2); 
    return NSInsetRect(frame, 0.0, offset+10); 
} 

- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView 
       editor:(NSText *)editor delegate:(id)delegate event:(NSEvent *)event { 
    [super editWithFrame:[self adjustedFrameToVerticallyCenterText:aRect] 
        inView:controlView editor:editor delegate:delegate event:event]; 
} 

- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView 
       editor:(NSText *)editor delegate:(id)delegate 
        start:(NSInteger)start length:(NSInteger)length { 

    [super selectWithFrame:[self adjustedFrameToVerticallyCenterText:aRect] 
        inView:controlView editor:editor delegate:delegate 
        start:start length:length]; 
} 

- (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view { 
    [super drawInteriorWithFrame: 
    [self adjustedFrameToVerticallyCenterText:frame] inView:view]; 
} 

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView 
{ 
    [super drawWithFrame:cellFrame inView:controlView]; 
} 

ऐसा ही एक उपवर्ग पहले से ही नियमित रूप से NSTextFieldCell रों के लिए काम कर रहा है, तो बस इस के संस्करणों सुरक्षित नहीं। ऐसा लगता है जैसे ऐप्पल ने इन विधियों को ओवरराइड होने से सुरक्षित रखा है।

अब पासवर्ड क्षेत्र केवल एक गलत संरेखित है:

misaligned "password" field

किसी को भी सुझाव है कि एक तरह से NSSecureTextFieldCell उपवर्ग के तरीकों के नाम से जाना मिल सकता है या किसी अन्य तरीके से खड़ी पाठ क्षेत्र केंद्रित करने के लिए?

उत्तर

22

अपने NSSecureTextFieldCell subclass के अंदर, नियमित NSTextField का उपयोग करने का प्रयास करें। मुझे एक ही समस्या थी और यह संयोजन काम करता था।

+2

धन्यवाद, यह काम किया। – jrc

+0

यह मेरे लिए भी काम किया। धन्यवाद! – Tommy

2

आपको NSTextField या NSSecureTextField में प्लेसहोल्डर टेक्स्ट को लंबवत रूप से केंद्रित करने के लिए करना है, विशेषताएँ इंस्पेक्टर (टेक्स्ट फ़ील्ड सेक्शन) में "सिंगल लाइन मोड का उपयोग करें" चेकबॉक्स चेक करें। आप आईबी उपयोग नहीं कर रहे हैं, तो आप यह प्रोग्राम के रूप में इतनी तरह सेट कर सकते हैं:

[mySecureTextField.cell setUsesSingleLineMode:YES]; 
4

आप के उपवर्ग बना सकते हैं NSSecureTextField की तरह:

@implementation SubclassTextField 

+ (Class)cellClass { 
    return [SubclassTextFieldCell class]; 
} 

@end 

और सेल के उपवर्ग:

@implementation SubclassTextFieldCell 

- (NSRect)drawingRectForBounds:(NSRect)rect { 
    // It will be working 
    ... 
} 

... 
@end 

यदि आप ऐसा करते हैं तो उपclassed NSSecureTextField से विधियां काम करना शुरू कर देंगे।

+1

यह सही उत्तर है। इंटरफ़ेस बिल्डर में कस्टम सेल क्लास असाइन करना पर्याप्त नहीं है। '- [NSSecureTextField initWithCoder:] 'डीकोडेड सेल की कक्षा की जांच करता है और यदि यह' सेल क्लास 'से मेल नहीं खाता है तो एक नया उदाहरण बनाएगा। –

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