0

我正在尝试为文本的某些特定部分着色,我试图说:

if word.strip().startswith(":"):
    self.setAttributesForRange(NSColor.greenColor(), None, highlightOffset, len(word))

当有人键入标志时它会变成绿色。这很好,但它会像这样继续为单词着色:

:Hello Hello :Hello <----这一切都变成绿色,但我想要这样的东西:

:Hello Hello :Hello <----除了中间的 "hello" 以外的所有东西都被着色了,因为它不是以符号开头的:,请帮帮我

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
4

1 回答 1

1

这一切都取决于是什么 word

In [6]: word = ':Hello Hello :Hello'

In [7]: word.strip().startswith(':')
Out[7]: True

In [8]: len(word)
Out[8]: 19

相比:

In [1]: line = ':Hello Hello :Hello'.split()

In [2]: line
Out[2]: [':Hello', 'Hello', ':Hello']

In [3]: for word in line:
    print word.strip().startswith(':')
    print len(word)
   ...:     
True
6
False
5
True
6

请注意 中的差异len(word),我怀疑这是导致您的问题的原因。

于 2012-12-27T23:49:21.950 回答