2013-04-18 9 views
7

[NSTextView selectedTextAttributes] के लिए डिफ़ॉल्ट मान मेरे ऐप में अनुपयोगी है, क्योंकि मैं उपयोगकर्ता को रंग (सिंटैक्स हाइलाइटिंग) चुनने की अनुमति देता हूं जो लगभग पृष्ठभूमि रंग के समान ही होते हैं।मैं पृष्ठभूमि विंडो पर [NSTextView चयनितTextAttributes] कैसे सेट कर सकता हूं?

मैं एक उपयुक्त रंग निर्धारित करने के लिए कुछ गणित में लिखा है और इस का उपयोग यह निर्धारित करने के लिए कर सकते हैं:

textView.selectedTextAttributes = @{ 
    NSBackgroundColorAttributeName: [NSColor yellowColor], 
    NSForegroundColorAttributeName: [NSColor redColor] 
    }; 

लेकिन जब विंडो पृष्ठभूमि में है, यह अभी भी सिस्टम डिफ़ॉल्ट हल्के भूरे रंग का उपयोग करता है।

मैंने सक्रिय बनाम निष्क्रिय विंडो के साथ उपरोक्त कोड के स्क्रीनशॉट संलग्न किए हैं। - मैं निष्क्रिय विंडो के चयनित टेक्स्ट पृष्ठभूमि रंग को कैसे बदल सकता हूं?

active inactive

+0

क्या आपने एनएसविंडो को उपclassing और 'resignKeyWindow' ओवरराइड करने का प्रयास किया है? – CodaFi

+0

@CodaFi मुझे उस विधि में क्या करना चाहिए? मैंने बस चयनित टेक्स्ट एटिरबूट्स सेट करने का प्रयास किया लेकिन इसका कोई प्रभाव नहीं पड़ा। –

+1

एचएम ... NSWindow.h जांचें। जब भी विंडो इस्तीफा देता है/मुख्य स्थिति प्राप्त करता है तो उस पर कब्जा करने के लिए आप जिन कार्यों का उपयोग कर सकते हैं, उनमें एक नाव भार है। आप वहां से गुण असाइन कर सकते हैं। – CodaFi

उत्तर

6

आप NSLayoutManager की विधि ड्राइंग अधिभावी द्वारा रंग ओवरराइड कर सकते हैं।

final class LayoutManager1: NSLayoutManager { 
    override func fillBackgroundRectArray(rectArray: UnsafePointer<NSRect>, count rectCount: Int, forCharacterRange charRange: NSRange, color: NSColor) { 
     let color1 = color == NSColor.secondarySelectedControlColor() ? NSColor.redColor() : color 
     color1.setFill() 
     super.fillBackgroundRectArray(rectArray, count: rectCount, forCharacterRange: charRange, color: color1) 
     color.setFill() 
    } 
} 

और NSTextView के लेआउट मैनेजर को प्रतिस्थापित करें।

textView.textContainer!.replaceLayoutManager(layoutManager1) 

यहां full working example है।


@Kyle setFill के कारण के लिए पूछता है के रूप में, मैं कुछ अद्यतन जोड़ें।

एप्पल मैनुअल से:

... charRange और रंग मापदंडों केवल सूचना के प्रयोजनों के लिए में पारित कर रहे हैं; रंग पहले ही ग्राफिक्स स्थिति में सेट है। यदि किसी भी कारण से आप इसे संशोधित करते हैं, तो आपको इस विधि से लौटने से पहले इसे पुनर्स्थापित करना होगा। ...

जिसका मतलब है कि गुजर-इन अन्य रंग super कॉल में कोई प्रभाव नहीं है, और आप बस इसे super कॉल के साथ काम करने के लिए NSColor.setFill की जरूरत है। इसके अलावा, मैन्युअल को इसे मूल पर वापस सेट करने की आवश्यकता है।

+0

बस उत्सुक, 'सेटफिल' क्यों कॉल करें? मैं नए रंग को 'fillBackgroundRectArray' से गुजरने की अपेक्षा करता हूं, सब ठीक से संभाल लेंगे? – Kyle

+1

@ केली मैंने उस जानकारी को प्रदान करने के लिए अपना उत्तर अपडेट किया। टिप्पणी में होना बहुत लंबा है। – Eonil

5

यह जब खिड़की पृष्ठभूमि यह है जब NSTextView चयनित नहीं है में है नहीं है। मुझे नहीं लगता कि आप उस व्यवहार को बदल सकते हैं। enter image description here

आप एक जिम्मेदार स्ट्रिंग बना सकते हैं और जब यह फोकस खो देता है तो चयनित टेक्स्ट की सीमा में NSBackgroundColorAttributeName विशेषता जोड़ सकता है। फोकस खो जाने पर भी जिम्मेदार स्ट्रिंग एक ही रंग में रहता है।

NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"hello world"]; 
[string addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:NSMakeRange(1, 7)]; 
[string addAttribute:NSBackgroundColorAttributeName value:[NSColor yellowColor] range:NSMakeRange(1, 7)]; 
[self.myTextView insertText:string]; 

enter image description here

संपादित करें अभि बेकर्ट द्वारा: यह कैसे मैं इस सवाल का जवाब (ध्यान दें मैं भी में चयनित पाठ विशेषताओं का निर्माण किया है, वरना वे मैं सेट कर रहा हूं ओवरराइड को निष्क्रिय करने के लिए किया था लागू किया है):

@implementation MyTextView 

- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
    if (!(self = [super initWithCoder:aDecoder])) 
    return nil; 

    // disable built in selected text attributes 
    self.selectedTextAttributes = @{}; 

    return self; 
} 

- (id)initWithFrame:(NSRect)frameRect textContainer:(NSTextContainer *)container 
{ 
    if (!(self = [super initWithFrame:frameRect textContainer:container])) 
    return nil; 

    // disable built in selected text attributes 
    self.selectedTextAttributes = @{}; 

    return self; 
} 

- (void)setSelectedRanges:(NSArray *)ranges affinity:(NSSelectionAffinity)affinity stillSelecting:(BOOL)stillSelectingFlag 
{ 
    // remove from old ranges 
    for (NSValue *value in self.selectedRanges) { 
    if (value.rangeValue.length == 0) 
     continue; 

    [self.textStorage removeAttribute:NSBackgroundColorAttributeName range:value.rangeValue]; 
    } 

    // apply to new ranges 
    for (NSValue *value in ranges) { 
    if (value.rangeValue.length == 0) 
     continue; 

    [self.textStorage addAttribute:NSBackgroundColorAttributeName value:[NSColor yellowColor] range:value.rangeValue]; 
    } 

    [super setSelectedRanges:ranges affinity:affinity stillSelecting:stillSelectingFlag]; 
} 

@end 
+0

धन्यवाद! मैंने अपना सबक्लास ओवरराइड सेट किया है चयनित चयन: टेक्स्ट स्टोरेज में मैन्युअल रूप से गुण लागू करने के लिए, और फिर चयनित टेक्स्ट को एक खाली शब्दकोश में सेट करें, और यह काम कर रहा है। मैं आपके द्वारा उपयोग किए जाने वाले कोड के लिए एक सेकंड में अपना उत्तर संपादित करूंगा। –

+0

खुशी मैं मदद कर सकता था! –

+0

सावधान रहें कि टेक्स्ट स्टोरेज में सेटिंग विशेषताएँ का मतलब है कि आप रंगीन पाठ के हिस्से के रूप में गुणों को सहेजते हैं, जैसा कि आप समृद्ध टेक्स्ट संपादकों में करते हैं। मैं इसके बजाय NSLayoutManager के अस्थायी विशेषताओं का उपयोग करने की अनुशंसा करता हूं, जो इस तरह क्षणिक स्टाइल के लिए लक्षित हैं। – ctietze

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