首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >染一个NSSegmentedControl..。多么?

染一个NSSegmentedControl..。多么?
EN

Stack Overflow用户
提问于 2014-08-09 17:20:17
回答 1查看 1.5K关注 0票数 3

在用户可以传递到向导的下一个阶段之前,必须选择一个NSSegmentedControl。我可以禁用next按钮,迫使用户猜测缺少了什么东西,但我想将控件的红色闪现,比如颜色或边框,如果用户按下NEXT而不选择控件上的选项,则会引起注意。

我没有在谷歌上找到一个页面来说明如何做到这一点。在可可里可以这样做吗?我怎样才能在一个NSSegmentedControl中做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-10 17:20:53

我在滕博中使用了一个有色分段控件。这里的目的不是提请注意该控件,而是让它与彩色导航条相结合。屏幕截图的右上角。

与其从头开始绘制分段控件,不如将标准实现绘制到NSImage中,然后将其绘制为调色,然后再绘制到视图中。

同样的原则也可以用来引起人们对控制的注意。无论何时更改颜色,都需要调用setNeedsDisplay。

代码语言:javascript
复制
@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
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25221149

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档