【问题标题】:How would you give an Outline View alternating row colors?你会如何给大纲视图交替行颜色?
【发布时间】:2009-06-13 13:13:19
【问题描述】:

我知道在 IB 中有一个复选框,但它只给你白色和蓝色的颜色。我将如何使它使用不同的颜色?

【问题讨论】:

    标签: objective-c cocoa


    【解决方案1】:

    This article 关于 TableView 的渐变(cocoa 不是 cocoa-touch)可能会给你一些指导。

    【讨论】:

    • 您确定我的问题没有更具体的内容吗?
    【解决方案2】:

    我找到了这个代码,

    // RGB values for stripe color (light blue)
    #define STRIPE_RED   (237.0 / 255.0)
    #define STRIPE_GREEN (243.0 / 255.0)
    #define STRIPE_BLUE  (254.0 / 255.0)
    static NSColor *sStripeColor = nil;
    
        @implementation …
    
    // This is called after the table background is filled in,
    // but before the cell contents are drawn.
    // We override it so we can do our own light-blue row stripes a la iTunes.
    - (void) highlightSelectionInClipRect:(NSRect)rect {
        [self drawStripesInRect:rect];
        [super highlightSelectionInClipRect:rect];
    }
    
    // This routine does the actual blue stripe drawing,
    // filling in every other row of the table with a blue background
    // so you can follow the rows easier with your eyes.
    - (void) drawStripesInRect:(NSRect)clipRect {
        NSRect stripeRect;
        float fullRowHeight = [self rowHeight] + [self intercellSpacing].height;
        float clipBottom = NSMaxY(clipRect);
        int firstStripe = clipRect.origin.y / fullRowHeight;
        if (firstStripe % 2 == 0)
            firstStripe++;   // we're only interested in drawing the stripes
                             // set up first rect
        stripeRect.origin.x = clipRect.origin.x;
        stripeRect.origin.y = firstStripe * fullRowHeight;
        stripeRect.size.width = clipRect.size.width;
        stripeRect.size.height = fullRowHeight;
        // set the color
        if (sStripeColor == nil)
            sStripeColor = [[NSColor colorWithCalibratedRed:STRIPE_RED
                               green:STRIPE_GREEN
                               blue:STRIPE_BLUE
                               alpha:1.0] retain];
        [sStripeColor set];
        // and draw the stripes
        while (stripeRect.origin.y < clipBottom) {
            NSRectFill(stripeRect);
            stripeRect.origin.y += fullRowHeight * 2.0;
        }
    }
    

    但我不知道如何子类 NSOutlineView。有人可以告诉我如何将 NSOutline View 子类化吗?

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-05
    • 2010-11-26
    • 2012-07-17
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多