2012-12-26 2 views
1

내가 다운로드 한 트리거 해달라고 "-"앱을 열고 기호를 입력 할 경우, 그것은 정상적으로 작동 enter link description here를 NSTextView 강조는

가 빨간색으로 변합니다,하지만 난 문제에 직면했다. .H 파일 내부에 IBOutlet NSTextView * change_text을하고 하는 .m 파일 내부에 쓴 : 내가 쓴 응용 프로그램은이 기호를 표시해야합니다 열 때

-(void)awakeFromNib { 



    [text_View setString:@"-"]; 


} 

생각

이었다 "-"화면으로 레드. 그것은 기호를 보여주었습니다 : "-"그러나 그것을 강조하지 않고, 내가 수동으로 반환 또는 공간을 클릭 할 때까지만 빨간색으로 변했습니다. 다른 사람이 텍스트를 다시 변경하기를 기다리는 대신 빨간색으로 기호를 바꾸기를 원합니다. 내가 제대로 이해한다면

from Foundation import * 
from AppKit import * 

import objc 

class PyObjC_HighlightAppDelegate(NSObject): 

    # The connection to our NSTextView in the UI 
    highlightedText = objc.IBOutlet() 

    # Default font size to use when highlighting 
    fontSize = 12 

    def applicationDidFinishLaunching_(self, sender): 
     NSLog("Application did finish launching.") 

    def textDidChange_(self, notification): 




     """ 
     Delegate method called by the NSTextView whenever the contents of the 
     text view have changed. This is called after the text has changed and 
     been committed to the view. See the Cocoa reference documents: 

     http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSText_Class/Reference/Reference.html 
     http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextView_Class/Reference/Reference.html 

     Specifically the sections on Delegate Methods for information on additional 
     delegate methods relating to text control is NSTextView objects. 
     """ 

     # Retrieve the current contents of the document and start highlighting 

     content = self.highlightedText.string() 
     self.highlightText(content) 

    def setAttributesForRange(self, color, font, rangeStart, rangeLength): 
     """ 
     Set the visual attributes for a range of characters in the NSTextView. If 
     values for the color and font are None, defaults will be used. 

     The rangeStart is an index into the contents of the NSTextView, and 
     rangeLength is used in combination with this index to create an NSRange 
     structure, which is passed to the NSTextView methods for setting 
     text attributes. If either of these values are None, defaults will 
     be provided. 

     The "font" parameter is used as an key for the "fontMap", which contains 
     the associated NSFont objects for each font style. 
     """ 
     fontMap = { 
        "normal" : NSFont.systemFontOfSize_(self.fontSize), 
        "bold" : NSFont.boldSystemFontOfSize_(self.fontSize) 
        } 

     # Setup sane defaults for the color, font and range if no values 
     # are provided 
     if color is None: 
      color = NSColor.blackColor() 
     if font is None: 
      font = "normal"   

     if font not in fontMap: 
      font = "normal" 

     displayFont = fontMap[font] 

     if rangeStart is None: 
      rangeStart = 0 

     if rangeLength is None: 
      rangeLength = len(self.highlightedText.string()) - rangeStart 

     # Set the attributes for the specified character range 
     range = NSRange(rangeStart, rangeLength) 
     self.highlightedText.setTextColor_range_(color, range) 
     self.highlightedText.setFont_range_(displayFont, range) 

    def highlightText(self, content): 
     """ 
     Apply our customized highlighting to the provided content. It is assumed that 
     this content was extracted from the NSTextView. 
     """ 

     # Calling the setAttributesForRange with no values creates 
     # a default that "resets" the formatting on all of the content 
     self.setAttributesForRange(None, None, None, None) 

     # We'll highlight the content by breaking it down into lines, and 
     # processing each line one by one. By storing how many characters 
     # have been processed we can maintain an "offset" into the overall 
     # content that we use to specify the range of text that is currently 
     # being highlighted. 
     contentLines = content.split("\n") 
     highlightOffset = 0 

     for line in contentLines: 

      if line.strip().startswith("#"): 
       # Comment - we want to highlight the whole comment line 
       self.setAttributesForRange(NSColor.greenColor(), None, highlightOffset, len(line)) 


      elif line.find(":") > -1: 
       # Tag - we only want to highlight the tag, not the colon or the remainder of the line 
       startOfLine = line[0: line.find(":")] 
       yamlTag = startOfLine.strip("\t ") 
       yamlTagStart = line.find(yamlTag) 
       self.setAttributesForRange(NSColor.blueColor(), "bold", highlightOffset + yamlTagStart, len(yamlTag)) 

      elif line.strip().startswith("-"): 
       # List item - we only want to highlight the dash 
       listIndex = line.find("-") 
       self.setAttributesForRange(NSColor.redColor(), None, highlightOffset + listIndex, 1) 


      # Add the processed line to our offset, as well as the newline that terminated the line 
      highlightOffset += len(line) + 1 

답변

1

여기서 핵심적인 문제는 -[NSTextView setString]NSTextDidChangeNotification을 게시하지 않으므로 코드가 textDidChange_ 메서드를 호출하지 않는다는 것입니다. 그래서 가장 쉬운 해결책은 문자열을 설정 한 후에 해당 알림을 게시하는 것입니다. 정말 PyObjC 모르지만, 이것은 당신이 목표 - C에서 그것을 할 거라고 방법은 다음과 같습니다

[[NSNotificationCenter defaultCenter] postNotificationName:NSTextDidChangeNotification object:text_view]; 

내가 직접 방법 textDidChange_ 메소드를 호출 추천하지 않을 것 같은 (또는 당신이 사용하는 라이브러리)는 텍스트가 변경 될 때 호출되는 것에 의존하기 때문에 동일한 알림에 대해 반응해야합니다.

+0

나는 너에게 키스하고 싶다!, 효과가 있었다! : D 감사합니다 !! –

-2

그냥 applicationDidFinishLaunchingtextDidChange를 호출 : 같은 파이썬 스크립트를 볼

을 저를 도와주세요.

+0

이 프로그램의 범위는 적절할 것입니다. – paulmelnikow