- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在为我的一个应用程序自定义 UI,我的想法是文本区域在未聚焦时最初带有灰色边框,而当它聚焦时,边框变为亮白色。我的应用使用深色主题,对于单行 NSTextField
,效果很好。
但是,我遇到了子类 NSTextView
的问题。为了正确地更改边框,我最终不得不实际子类化父 NSScrollView
,但我仍然看到奇怪的行为。 (参见下面的屏幕截图。)我希望红色框填充整个 ScrollView ,因为这将允许我描边(而不是填充,这只是为了测试)路径,产生一个漂亮的边界。相反,红色框似乎只填充到内部 subview 。
以下代码片段,用于 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));
}
生成如下所示的屏幕截图。此外,请查看日志中的输出,其中建议应填充整个 View 。这是唯一显示的输出,无论内部文本的大小如何。输入回车会增加红色框的高度,但不会产生不同的输出。 (我希望红色框填满整个边界。)
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}}
编辑:感谢 Josh Caswell 的回答。请参阅下文了解不专注时和专注时的正确行为。
最佳答案
正如 ughoavgfhw 所指出的,NSScrollView
通常不进行任何绘图,并且可能以这种方式与其 subview 进行奇怪的交互。我建议在您的 TextView 的绘图代码中放置类似以下的内容来绘制您想要的自定义聚焦环*:
// 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];
这应该是 AppKit 在绘制聚焦环时所做的合理近似。当然,AppKit 允许在 View 边界之外绘制——我不能保证这完全安全,但您似乎可以使用 3 px 的边距。如果需要,您可以将环完全画在边界内。无论如何,真正的聚焦环会在 View 内稍微延伸(2 像素)(就像我在这里所做的那样)。
Apple 文档 Setting the Clipping Region .
编辑:在重新阅读您对该问题的评论后,我意识到我可能已经啰嗦地掩埋了真正的答案。尝试子类化 NSClipView
并为此切换 ScrollView 的剪辑 View ,或者为文档 View 使用自定义 View 。
*: 你也可以把它放在一个自定义 View 子类的绘图代码中,它被设置为 NSScrollView
的文档 View ;那么您的 TextView 可能是它的 subview 。或者替换自定义 NSClipView
子类。
关于objective-c - 子类化 NSScrollView drawRect : Method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5602350/
为了澄清这个问题的目的:我知道如何使用 subview 和使用 drawRect 创建复杂的 View 。我试图完全理解何时以及为什么使用一个而不是另一个。 我也明白提前优化并在进行任何分析之前以更困
我是 Swift 的新手,正在尝试让一个非常简单的应用程序运行。我想要做的就是让 UIView.drawRect 在我按下按钮时更新。它会在应用程序首次加载时更新/绘制,然后无论我尝试什么,它都不会更
我有一个名为 Icon 的基于 UIView 的类。在那里,我想要类似于这样的代码: UIView *finalView = [[UIView alloc] initWithFrame:CGRectM
我正在覆盖 drawRect: 在我的一个 View 中,它甚至在不调用 [super drawrect:rect] 的情况下也能工作。这是如何运作的? - (void)drawRect:(CGRec
我不是很明白CALayer的display和drawInContext与 View 中的drawRect有什么关系。 如果我有一个每 1 秒设置一次 [self.view setNeedsDispla
我在 viewController 上放置了一个 View ,我想在此 View 中绘制一个矩形,但具有自定义高度。我根据 viewController 中的参数确定高度。例如。如果我的参数是 50,
这个非常简单的绘图命令创建了一个 11x11 像素的跟踪红色矩形: _sp.graphics.lineStyle( 1, 0xFF0000, 1, true, LineScaleMode.NORMAL
我每两秒从 NSTimer 函数调用 setNeedsDisplayInRect 一次。这工作得很好,并在随机位置绘制一个具有随机颜色的正方形。然而,在某些情况下,我想在 NSTimer 函数中绘制一
你好,我是新来的,也是 Java 编程的新手。最近我正在尝试在 JPanel 中编写绘图功能。问题是,当我添加新的绘制矩形(只需在 JRadiobutton 中选择绘制矩形,然后拖动空白区域)时,它工
我有一个放在WebView上方的UIView。在这个UIView中,我必须画图,但是应用程序因此错误而崩溃 在LibraryViewController.m中 self.drawView =[[Dr
我正在使用 Java 编写一个文件管理器。我需要将矩形选择添加到我的程序中,就像在 Windows 中一样(以便选择矩形内的多个文件)。我的问题是,每当我向 DrawRect 面板添加布局来放置图标时
我正在使用 UIBezierPath 创建自定义 View 绘画。每当状态根据服务器的响应发生变化时,我想更新自定义 View 填充颜色。我正在使用 fillColor属性作为参数来更新 View 的
我有一个名为 TestView 的 UIView 子类,它重写了 DrawRect ,如下所示: import Foundation import UIKit class TestView: UIVi
我的应用程序中有一个泄漏问题,我正在创建一个颜色的自动释放对象,但通过仪器说问题存在。 Malloc 1 04:12.221.102 32 UIKit +[UIColor allocW
我需要知道是否可以全局覆盖 UI 类的 drawRect。类似于“外观”类属性的东西。本质上,我需要为应用程序中的所有按钮使用我自己的绘图例程。最好我不想继承并且必须在 Storyboard 中的每个
我将绘制一条类似于下图中红色曲线的曲线(可以是贝塞尔曲线或我认为对我的目的最方便的任何曲线)。我想找到曲线上的点(illo 中的蓝点)。这些点很可能是曲线长度的相等部分的划分。 我能找到这些点吗?到目
我有两个 UIView 子类,比如“A 类”和“B 类”。类 A 在其初始化程序中具有代码 [self addSubview:instanceOfClassB];。而在 B 类的 drawRect:
我一直在使用 UITextField 的子类在我的文本字段末尾绘制一个“m3”字符串,但有些东西不起作用。我知道正在调用该方法,因为我已经使用 NSLog 对其进行了测试,我做错了什么? 我的类(cl
我有一个 UITableViewCell 的子类,我正在尝试在 drawRect 中画一条线。 有一个属性separatorColor在子类的标题中: @interface DXDecisionsTa
我将动画代码放在我的 UIView 子类的 drawRect 中。我真的在寻找类似 UIViewController 的 viewWillAppear 的东西。但我想使用 UIView 而不是 UIV
我是一名优秀的程序员,十分优秀!