着色NSSegmentedControl ......怎么样?

时间:2014-08-09 17:20:17

标签: macos cocoa nssegmentedcontrol

我必须先选择NSSegmentedControl,然后才能将用户转到向导的下一个阶段。我可以禁用下一个按钮,强制用户猜测某些东西丢失,但是如果用户按下NEXT而不选择控件上的选项,我想将控件红色闪烁,如色调或边框,以引起注意。 / p>

我没有在谷歌上找到一个单页告诉如何做到这一点。可可在可可做到这一点吗?我怎么能为一个NSSegmentedControl做到这一点?

1 个答案:

答案 0 :(得分:4)

我在Tembo中使用了有色分段控件。这里的目的不是要引起对控件的注意,而是将其与彩色导航栏混合。 screenshot的右上角。

我没有从头开始绘制分段控件,而是将标准实现绘制到NSImage中,然后在绘制到视图之前我tint

可以使用相同的原理来引起对控制的注意。每当更改色调颜色

时,您都需要调用setNeedsDisplay
@interface TintedSegmentedCell : NSSegmentedCell
{
    NSMutableDictionary *_frames;
}

@end


@implementation TintedSegmentedControl

@synthesize tintColor = _tintColor;

- (void)dealloc
{
    [_tintColor release], _tintColor = nil;

    [super dealloc];
}

+ (Class)cellClass
{
    return [TintedSegmentedCell class];
}

@end


@implementation TintedSegmentedCell

- (void)drawSegment:(NSInteger)segment inFrame:(NSRect)frame withView:(NSView *)controlView
{
    [_frames setObject:[NSValue valueWithRect:frame] forKey:[NSNumber numberWithInteger:segment]];

    [super drawSegment:segment inFrame:frame withView:controlView];
}

- (void)drawWithFrame:(NSRect)frame inView:(NSView *)view
{
    if ([view isKindOfClass:[TintedSegmentedControl class]]) {
        NSColor *tintColor = [(TintedSegmentedControl*)view tintColor];

        if (tintColor != nil) {
            NSRect bounds = frame;

            bounds.origin.x = 0;
            bounds.origin.y = 0;

            NSSize size = bounds.size;
            NSImage *image = [[[NSImage alloc] initWithSize:size] autorelease];

            NSInteger segmentCount = [self segmentCount];
            NSMutableDictionary *frames = [NSMutableDictionary dictionaryWithCapacity:segmentCount];

            _frames = frames;

            [image lockFocus];
            {
                [super drawWithFrame:bounds inView:view];
            }
            [image unlockFocus];

            NSImage *tintedImage = [[image hh_imageTintedWithColor:[NSColor blackColor]] hh_imageTintedWithColor:tintColor];

            [tintedImage drawInRect:frame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

            NSImage *overlayImage = [[[NSImage alloc] initWithSize:size] autorelease];

            [overlayImage lockFocus];
            {
                _frames = nil;

                for (NSInteger segment = 0; segment < segmentCount; segment++) {
                    NSRect frameRect = [[frames objectForKey:[NSNumber numberWithInteger:segment]] rectValue];

                    [self drawSegment:segment inFrame:frameRect withView:view];
                }
            }
            [overlayImage unlockFocus];

            [overlayImage drawInRect:frame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];

            return;
        }
    }

    [super drawWithFrame:frame inView:view];
}

@end