我正在尝试使用圆角和投影创建自定义NSView.我创建了一个NSView子类并具有以下drawRect:方法
- (void)drawRect:(NSRect)dirtyRect
{
NSRect rect = NSMakeRect([self bounds].origin.x + 3, [self bounds].origin.y + 3, [self bounds].size.width - 6, [self bounds].size.height - 6);
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:rect xRadius:5.0 yRadius:5.0];
[path addClip];
NSShadow *shadow = [[[NSShadow alloc] init] autorelease];
[shadow setShadowColor:[NSColor redColor]];
[shadow setShadowBlurRadius:2.0f];
[shadow setShadowOffset:NSMakeSize(0.f, -1.f)];
[shadow set];
[[NSColor controlColor] set];
NSRectFill(rect);
[super drawRect:dirtyRect];
}
Run Code Online (Sandbox Code Playgroud)
结果是使用圆角绘制的NSView,但没有阴影(但我确实在抗锯齿中看到角落周围的红色色调).如果我注释掉NSBezierPath,那么我将得到一个带阴影的方形NSView.我没有在文档中看到任何暗示NSShadow和NSBezierPath是互斥的,所以我显然遗漏了一些东西.
任何想法都非常感谢!
我想在无边界的NSWindow上获得一个自定义的NSShadow,因为我还在窗口上应用了一些动画,所以我将窗口的内容视图设置为图层支持.
在应用NSShadow时contentView
,阴影将被剪切在视图的边框处:
一种可能性是减少contentView
'rect(NSInsetRect),但是然后NSWindow调整边框的大小与窗口的外观不匹配!
有没有机会在它的边界之外绘制图层的阴影?
编辑:显示的屏幕截图已经有-10插入矩形!
我有一个UIView
已创建的自定义,它具有多个自定义UIControl
实例,这些实例是非矩形按钮(而不是使用UIButton
实例)。之所以这样做,是因为我需要能够绘制非矩形按钮,并使它们仅在非矩形区域对水龙头做出反应。
因为UIControl
不附带UILabel
,所以我使用CATextLayer
来添加此功能。我希望我正在绘制的文本(通过将设置为layer.string
等于NSAttributedString
)应用阴影,但是它似乎CATextLayer
忽略了任何NSShadow
属性。
我找不到任何文档可以告诉我任何一种方法,但是实际上,以下操作不起作用:
NSMutableParagraphStyle *s = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
s.lineSpacing = 0.9f;
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor orangeColor];
shadow.shadowOffset = CGSizeMake(10,10);
NSDictionary *textAttrs = @{NSFontAttributeName : [UIFont someFont],
NSParagraphStyleAttributeName : [s copy],
NSShadowAttributeName : shadow};
Run Code Online (Sandbox Code Playgroud)
也就是说CATextLayer
,除了,显示的内容与您预期的一样NSShadow
。因此,以上代码与该代码完全相同(据我所知):
NSMutableParagraphStyle *s = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
s.lineSpacing = 0.9f;
NSDictionary *textAttrs = @{NSFontAttributeName : [UIFont someFont], …
Run Code Online (Sandbox Code Playgroud) 我花了一些时间试图弄清楚如何在阴影中添加阴影NSView
。现在,我正在尝试使用NSShadow
该类来完成此任务。我的代码如下。我试图在NSView
子类的自定义init方法中创建阴影。无论我尝试什么,都不会出现阴影。
NSShadow *dropShadow = [[NSShadow alloc] init];
[dropShadow setShadowColor:[NSColor blackColor]];
[self setWantsLayer:YES];
[self setShadow:dropShadow];
Run Code Online (Sandbox Code Playgroud)
编辑
这是我尝试使用的方法CALayer
。
self.layer.shadowOffset = CGSizeMake(10, 10);
self.layer.shadowOpacity = 1.0;
self.layer.shadowRadius = 10.0;
self.layer.shadowPath = [self quartzPathFromBezierPath:[NSBezierPath bezierPathWithRect:frame]];
Run Code Online (Sandbox Code Playgroud)
quartzPathFromBezierPath:
将转换NSBezierPath
为CGPath
。
我正在尝试使用投影来绘制多行文本,而不使用已弃用的API.它适用于单行.相关代码如下所示:
-(void)drawRect:(CGRect)rect
{
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle defaultParagraphStyle] mutableCopy];
paragraph.lineBreakMode = NSLineBreakByWordWrapping;
paragraph.alignment = NSTextAlignmentCenter;
UIFont *f = [UIFont systemFontOfSize:20.0];
NSMutableDictionary *attributes = [NSMutableDictionary new];
[attributes setValuesForKeysWithDictionary:@{ NSFontAttributeName : f,
NSParagraphStyleAttributeName : paragraph,
NSForegroundColorAttributeName : [UIColor blueColor] }];
NSShadow * shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(4,4);
shadow.shadowColor = [UIColor redColor];
[attributes setValue:shadow forKey:NSShadowAttributeName];
rect.origin.y = 100;
[@"test string on one line" drawInRect:rect withAttributes:attributes];
rect.origin.y = 150;
[@"test string spanning more than one line" drawInRect:rect withAttributes:attributes];
}
Run Code Online (Sandbox Code Playgroud)
输出看起来像这样:
我在iPhone 5(7.1.2),iPhone …
我有奇怪的问题,与这个问题的答案有关:
我将此代码用于自定义视图的drawRect方法.我有这个:
- (void)drawRect:(NSRect)rect
{
// Create and fill the shown path
NSBezierPath *path = [NSBezierPath
bezierPathWithRoundedRect:[self bounds]
xRadius:4.0f
yRadius:4.0f];
[[NSColor colorWithCalibratedWhite:0.8f alpha:0.2f] set];
[path fill];
// Save the graphics state for shadow
[NSGraphicsContext saveGraphicsState];
// Set the shown path as the clip
[path setClip];
// Create and stroke the shadow
NSShadow * shadow = [[[NSShadow alloc] init] autorelease];
[shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.0f alpha:0.8f]];
[shadow setShadowBlurRadius:2.0];
[shadow set];
[path stroke];
// Restore the graphics state
[NSGraphicsContext restoreGraphicsState];
if ( highlight …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在NSView的背景上绘制一个NSShadow.我想用它作为NSGradient的替代品,因为我需要支持Mac OS X Tiger.我该怎么办?我知道这一定很容易,我一定是犯了一些错误.
谢谢!
nsshadow ×7
cocoa ×5
objective-c ×4
nsview ×3
ios ×2
macos ×2
nsbezierpath ×2
calayer ×1
catextlayer ×1
drawing ×1
ios8 ×1
nsstring ×1
nswindow ×1