在用户可以传递到向导的下一个阶段之前,必须选择一个NSSegmentedControl。我可以禁用next按钮,迫使用户猜测缺少了什么东西,但我想将控件的红色闪现,比如颜色或边框,如果用户按下NEXT而不选择控件上的选项,则会引起注意。
我没有在谷歌上找到一个页面来说明如何做到这一点。在可可里可以这样做吗?我怎样才能在一个NSSegmentedControl中做到这一点?
发布于 2014-08-10 17:20:53
我在滕博中使用了一个有色分段控件。这里的目的不是提请注意该控件,而是让它与彩色导航条相结合。屏幕截图的右上角。
与其从头开始绘制分段控件,不如将标准实现绘制到NSImage中,然后将其绘制为调色,然后再绘制到视图中。
同样的原则也可以用来引起人们对控制的注意。无论何时更改颜色,都需要调用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];
}
@endhttps://stackoverflow.com/questions/25221149
复制相似问题