如何使用某些分隔符为文本的特定部分着色

时间:2012-12-27 23:35:45

标签: python pyobjc

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

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

当有人输入符号时,它会变为绿色。这很好,但它会像这样继续着色这个词:

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

:你好你好:你好 < ---- 除了中间的“你好”之外一切都变色了,因为它没有从标志开始,请帮帮我

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 个答案:

答案 0 :(得分: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)中的差异,我怀疑这会导致您的问题。