2011-12-19 7 views
5

है, मैं एक NSTextView [1] में HTML का सबसेट संपादित कर रहा हूं और मैं < घंटा > टैग अनुकरण करना चाहता हूं।NSTextAttachmentCell एक मील उच्च

मुझे पता चला है कि ऐसा करने का तरीका NSTextAttachment और एक कस्टम NSTextAttachmentCell के साथ है, और इसमें कोड संलग्नक और सेल डालने के लिए लिखा गया है। समस्या यह है कि सेल के नीचे एक खाली मात्रा में रिक्त स्थान है।

यह स्थान सेल का हिस्सा नहीं है-अगर मैं सेल के पूरे क्षेत्र को लाल रंग देता हूं, तो यह बिल्कुल सही आकार है, लेकिन टेक्स्ट व्यू लाल रंग से बहुत नीचे टेक्स्ट की अगली पंक्ति डाल रहा है। यह राशि इस बात पर निर्भर करती है कि से ऊपर कितना टेक्स्ट है; दुर्भाग्य से, मैं लंबे दस्तावेजों के साथ काम कर रहा हूं जहां < एचआर > टैग महत्वपूर्ण हैं, और इससे ऐप के साथ बड़ी समस्याएं आती हैं।

क्या बिल्ली चल रहा है?

मेरे सेल उपवर्ग के पैसे भागों:

- (NSRect)cellFrameForTextContainer:(NSTextContainer *)textContainer 
       proposedLineFragment:(NSRect)lineFrag glyphPosition:(NSPoint)position 
        characterIndex:(NSUInteger)charIndex { 
    lineFrag.size.width = textContainer.containerSize.width; 

    lineFrag.size.height = topMargin + TsStyleBaseFontSize * 
     heightFontSizeMultiplier + bottomMargin; 

    return lineFrag; 
} 

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView 
     characterIndex:(NSUInteger)charIndex 
     layoutManager:(NSLayoutManager *)layoutManager { 
    NSRect frame = cellFrame; 
    frame.size.height -= bottomMargin; 

    frame.size.height -= topMargin; 
    frame.origin.y += topMargin; 

    frame.size.width *= widthPercentage; 
    frame.origin.x += (cellFrame.size.width - frame.size.width)/2; 

    [color set]; 
    NSRectFill(frame); 
} 

[1] मैंने कोशिश की isEditable सेट और मार्कअप यह उत्पादन के साथ एक WebView था unusably गंदा-विशेष रूप से, मैं नहीं एक मिल सकता है < पी > टैग में अच्छी तरह से पाठ लपेटने का तरीका।

- (void)insertHorizontalRule:(id)sender { 
    NSAttributedString * rule = [TsPage newHorizontalRuleAttributedStringWithStylebook:self.book.stylebook]; 

    NSUInteger loc = self.textView.rangeForUserTextChange.location; 

    if(loc == NSNotFound) { 
     NSBeep(); 
     return; 
    } 

    if(loc > 0 && [self.textView.textStorage.string characterAtIndex:loc - 1] != '\n') { 
     NSMutableAttributedString * workspace = rule.mutableCopy; 
     [workspace.mutableString insertString:@"\n" atIndex:0]; 
     rule = workspace; 
    } 

    if([self.textView shouldChangeTextInRange:self.textView.rangeForUserTextChange replacementString:rule.string]) { 
     [self.textView.textStorage beginEditing]; 
     [self.textView.textStorage replaceCharactersInRange:self.textView.rangeForUserTextChange withAttributedString:rule]; 
     [self.textView.textStorage endEditing]; 
     [self.textView didChangeText]; 
    } 

    [self.textView scrollRangeToVisible:self.textView.rangeForUserTextChange]; 

    [self reloadPreview:sender]; 
} 

और TsPage में विधि है कि लगाव स्ट्रिंग निर्माण करती है:


कोड कि क्षैतिज नियम लगाव सम्मिलित करता है के लिए रोब Keniger के अनुरोध का जवाब देने के

+ (NSAttributedString *)newHorizontalRuleAttributedStringWithStylebook:(TsStylebook*)stylebook { 
    TsHorizontalRuleCell * cell = [[TsHorizontalRuleCell alloc] initTextCell:@"—"]; 
    cell.widthPercentage = 0.33; 
    cell.heightFontSizeMultiplier = 0.25; 
    cell.topMargin = 12.0; 
    cell.bottomMargin = 12.0; 
    cell.color = [NSColor blackColor]; 

    NSTextAttachment * attachment = [[NSTextAttachment alloc] initWithFileWrapper:nil]; 
    attachment.attachmentCell = cell; 
    cell.attachment = attachment; 

    NSAttributedString * attachmentString = [NSAttributedString attributedStringWithAttachment:attachment]; 

    NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@""]; 
    [str appendAttributedString:attachmentString]; 
    [str.mutableString appendString:@"\n"]; 

    return str; 
} 
+2

आप कर सकते हैं कोड को पोस्ट करें कि आप अपने 'NSTextView' में टेक्स्ट अटैचमेंट कैसे डाल रहे हैं? –

+0

रोब, मैंने वह कोड जोड़ा है जिसे आप देखना चाहते थे। इसे देखने के लिए धन्यवाद! –

उत्तर

5

बदलने का प्रयास करें अपने सेल के फ्रेम की उत्पत्ति के लिए NSZeroPoint वापस करने के लिए सेल क्लास की cellFrameForTextContainer:proposedLineFragment:glyphPosition: विधि।

(मैं पता नहीं क्यों यह काम करना चाहिए, लेकिन प्रश्नकर्ता है और मैं है यह लिव-डिबगिंग गया है, और यह वास्तव में होता है।)


प्रश्नकर्ता द्वारा जोड़ा गया: ऐसा लग रहा है, यहां तक ​​कि यद्यपि वापस आने वाले आय को "फ्रेम" के रूप में वर्णित किया गया है, इसकी उत्पत्ति उस रेखा के सापेक्ष है, जो दस्तावेज़ के शीर्ष पर नहीं है। इस प्रकार, वापसी मूल्य का origin.y मान शून्य पर सेट किया जाना चाहिए यदि आप इसे एक ही पंक्ति में चाहते हैं।

(origin.x मूल्य, दूसरे हाथ पर, लाइन में सेल की स्थिति का उल्लेख करता है, तो यह lineFrag.origin.x के रूप में ही किया जाना चाहिए जब तक आप सेल की क्षैतिज स्थान बदलना चाहते हैं।)