Cra*_*tis 4 xcode cocoa drawing objective-c nsview
我正在为我的一个应用程序自定义UI,并且想法是文本区域在失焦时最初以灰色为边框,当它进入焦点时,边框变为亮白色.我的应用程序使用黑色主题,对于单行NSTextField
,这很有用.
但是,我遇到了子类的问题NSTextView
.为了正确地改变边界,我最终必须实际上是父类的子类NSScrollView
,但我仍然看到奇怪的行为.(见下面的截图.)我希望红色框填满整个滚动视图,因为这将允许我描边(而不是填充,仅用于测试)路径,产生一个漂亮的边框.相反,红色框似乎只填充内部子视图.
以下代码片段,用于NSScrollView
子类:
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
NSRect borderRect = self.bounds;
borderRect.origin.y += 1;
borderRect.size.width -= 1;
borderRect.size.height -= 4;
BOOL inFocus = ([[self window] firstResponder] == self);
if (!inFocus) {
inFocus = [self anySubviewHasFocus:self];
}
if (inFocus) {
[[NSColor colorWithDeviceRed:.8 green:.2 blue:0 alpha:1] set];
} else {
[[NSColor colorWithDeviceRed:.1 green:.8 blue:0 alpha:1] set];
}
[NSGraphicsContext saveGraphicsState];
[[NSGraphicsContext currentContext] setShouldAntialias:NO];
[NSBezierPath fillRect:borderRect];
[NSGraphicsContext restoreGraphicsState];
NSLog(@"My bounds: %@", NSStringFromRect(borderRect));
NSLog(@"Super (%@) bounds: %@", [self superview], NSStringFromRect(borderRect));
}
Run Code Online (Sandbox Code Playgroud)
生成屏幕截图,如下所示.另外,请参阅日志中的输出,这表示应填充整个视图.这是唯一显示的输出,无论内部文本的大小如何.输入回车会增加红色框的高度,但不会产生不同的输出.(而且我希望红色框能够填满整个边界.)
2011-04-08 21:30:29.789 MyApp[6515:903] My bounds: {{0, 1}, {196, 87}}
2011-04-08 21:30:29.789 MyApp[6515:903] Super (<EditTaskView: 0x3a0b150>) bounds: {{0, 1}, {196, 87}}
Run Code Online (Sandbox Code Playgroud)
编辑:感谢Josh Caswell的回答.请参阅下文,了解未聚焦时以及聚焦时的正确行为.
正如ughoavgfhw所指出的那样,NSScrollView
通常不会做任何绘图,并且可能以这种方式与其子视图进行奇怪的交互.我建议在文本视图的绘图代码中添加如下内容,以绘制您想要的自定义焦点环*:
// We're going to be modifying the state for this,
// so allow it to be restored later
[NSGraphicsContext saveGraphicsState];
// Choose the correct color; isFirstResponder is a custom
// ivar set in becomeFirstResponder and resignFirstResponder
if( isFirstResponder && [[self window] isKeyWindow]){
[myFocusedColor set];
}
else {
[myNotFocusedColor set];
}
// Create two rects, one slightly outset from the bounds,
// one slightly inset
NSRect bounds = [self bounds];
NSRect innerRect = NSInsetRect(bounds, 2, 2);
NSRect outerRect = NSMakeRect(bounds.origin.x - 2,
bounds.origin.y - 2,
bounds.size.width + 4,
bounds.size.height + 4);
// Create a bezier path using those two rects; this will
// become the clipping path of the context
NSBezierPath * clipPath = [NSBezierPath bezierPathWithRect:outerRect];
[clipPath appendBezierPath:[NSBezierPath bezierPathWithRect:innerRect]];
// Change the current clipping path of the context to
// the enclosed area of clipPath; "enclosed" defined by
// winding rule. Drawing will be restricted to this area.
// N.B. that the winding rule makes the order that the
// rects were added to the path important.
[clipPath setWindingRule:NSEvenOddWindingRule];
[clipPath setClip];
// Fill the rect; drawing is clipped and the inner rect
// is not drawn in
[[NSBezierPath bezierPathWithRect:outerRect] fill];
[NSGraphicsContext restoreGraphicsState];
Run Code Online (Sandbox Code Playgroud)
这应该是AppKit绘制聚焦环时所做的合理近似.当然,AppKit被允许在视图范围之外绘制 - 我不能保证这是完全安全的,但你似乎可以获得3 px的余量.如果你愿意,你可以完全在界限内画出戒指.无论如何,真正的聚焦环在视图内略微延伸(2像素)(就像我在这里所做的那样).
关于设置剪辑区域的 Apple文档.
编辑:重新阅读你对这个问题的评论后,我意识到我可能已经啰嗦地埋葬了真正的答案.尝试子类化NSClipView
并切换滚动视图的剪辑视图,或者使用文档视图的自定义视图.
*:您也可以将它放在自定义视图子类的绘图代码中,该子类被设置为文档视图NSScrollView
; 那么你的文本视图可能是其子视图.或者替换自定义NSClipView
子类.
归档时间: |
|
查看次数: |
2885 次 |
最近记录: |