Cocoa 如何实现Apple';给Mac的消息是什么?
我想复制Messages/iMessage的嵌入式文本样式,或者浅灰色背景上的文本“白色阴影”样式 如你所见,即使在浅灰色的背景上,文本也有“白色阴影”。粗体文本有亚像素渲染,而灰色文本没有(设计?) 我尝试了Cocoa 如何实现Apple';给Mac的消息是什么?,cocoa,nstextfield,nstextfieldcell,Cocoa,Nstextfield,Nstextfieldcell,我想复制Messages/iMessage的嵌入式文本样式,或者浅灰色背景上的文本“白色阴影”样式 如你所见,即使在浅灰色的背景上,文本也有“白色阴影”。粗体文本有亚像素渲染,而灰色文本没有(设计?) 我尝试了-setBackgroundStyle:NSBackgroundStyleRaised。然而,它产生了比背景更暗的阴影-setBackgroundStyle:NSBackgroundStyleLowered更糟糕的是,它甚至超过了我的字体颜色设置 那么,正确的方法是什么呢?任何技巧或只需
-setBackgroundStyle:NSBackgroundStyleRaised
。然而,它产生了比背景更暗的阴影<代码>-setBackgroundStyle:NSBackgroundStyleLowered更糟糕的是,它甚至超过了我的字体颜色设置
那么,正确的方法是什么呢?任何技巧或只需子类化
NSTextFields
?解决方案1:
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:NSMakeSize(0,-1)];
[shadow setShadowColor:[NSColor whiteColor]];
[shadow setShadowBlurRadius:0];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragStyle setAlignment:[self alignment]];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[self font],NSFontAttributeName,
shadow,NSShadowAttributeName,
[self textColor],NSForegroundColorAttributeName,
paragStyle,NSParagraphStyleAttributeName,nil];
[shadow release];
[paragStyle release];
NSAttributedString *string = [[NSAttributedString alloc] initWithString:[self stringValue] attributes:attributes];
[self setAttributedStringValue:string];
[string release];
[[self attributedStringValue] drawInRect:cellFrame];
}
我所能想到的最简单的解决方案是在彼此之间写两个文本(例如顶部为灰色,底部为白色,差值为1px)
解决方案2:
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:NSMakeSize(0,-1)];
[shadow setShadowColor:[NSColor whiteColor]];
[shadow setShadowBlurRadius:0];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragStyle setAlignment:[self alignment]];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[self font],NSFontAttributeName,
shadow,NSShadowAttributeName,
[self textColor],NSForegroundColorAttributeName,
paragStyle,NSParagraphStyleAttributeName,nil];
[shadow release];
[paragStyle release];
NSAttributedString *string = [[NSAttributedString alloc] initWithString:[self stringValue] attributes:attributes];
[self setAttributedStringValue:string];
[string release];
[[self attributedStringValue] drawInRect:cellFrame];
}
当然,可以通过子类化NSTextFieldCell
并添加shadow来完成
就像这样:
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:NSMakeSize(0,-1)];
[shadow setShadowColor:[NSColor whiteColor]];
[shadow setShadowBlurRadius:0];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragStyle setAlignment:[self alignment]];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[self font],NSFontAttributeName,
shadow,NSShadowAttributeName,
[self textColor],NSForegroundColorAttributeName,
paragStyle,NSParagraphStyleAttributeName,nil];
[shadow release];
[paragStyle release];
NSAttributedString *string = [[NSAttributedString alloc] initWithString:[self stringValue] attributes:attributes];
[self setAttributedStringValue:string];
[string release];
[[self attributedStringValue] drawInRect:cellFrame];
}
结果:
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:NSMakeSize(0,-1)];
[shadow setShadowColor:[NSColor whiteColor]];
[shadow setShadowBlurRadius:0];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragStyle setAlignment:[self alignment]];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[self font],NSFontAttributeName,
shadow,NSShadowAttributeName,
[self textColor],NSForegroundColorAttributeName,
paragStyle,NSParagraphStyleAttributeName,nil];
[shadow release];
[paragStyle release];
NSAttributedString *string = [[NSAttributedString alloc] initWithString:[self stringValue] attributes:attributes];
[self setAttributedStringValue:string];
[string release];
[[self attributedStringValue] drawInRect:cellFrame];
}
解决方案1:
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:NSMakeSize(0,-1)];
[shadow setShadowColor:[NSColor whiteColor]];
[shadow setShadowBlurRadius:0];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragStyle setAlignment:[self alignment]];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[self font],NSFontAttributeName,
shadow,NSShadowAttributeName,
[self textColor],NSForegroundColorAttributeName,
paragStyle,NSParagraphStyleAttributeName,nil];
[shadow release];
[paragStyle release];
NSAttributedString *string = [[NSAttributedString alloc] initWithString:[self stringValue] attributes:attributes];
[self setAttributedStringValue:string];
[string release];
[[self attributedStringValue] drawInRect:cellFrame];
}
我所能想到的最简单的解决方案是在彼此之间写两个文本(例如顶部为灰色,底部为白色,差值为1px)
解决方案2:
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:NSMakeSize(0,-1)];
[shadow setShadowColor:[NSColor whiteColor]];
[shadow setShadowBlurRadius:0];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragStyle setAlignment:[self alignment]];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[self font],NSFontAttributeName,
shadow,NSShadowAttributeName,
[self textColor],NSForegroundColorAttributeName,
paragStyle,NSParagraphStyleAttributeName,nil];
[shadow release];
[paragStyle release];
NSAttributedString *string = [[NSAttributedString alloc] initWithString:[self stringValue] attributes:attributes];
[self setAttributedStringValue:string];
[string release];
[[self attributedStringValue] drawInRect:cellFrame];
}
当然,可以通过子类化NSTextFieldCell
并添加shadow来完成
就像这样:
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:NSMakeSize(0,-1)];
[shadow setShadowColor:[NSColor whiteColor]];
[shadow setShadowBlurRadius:0];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragStyle setAlignment:[self alignment]];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[self font],NSFontAttributeName,
shadow,NSShadowAttributeName,
[self textColor],NSForegroundColorAttributeName,
paragStyle,NSParagraphStyleAttributeName,nil];
[shadow release];
[paragStyle release];
NSAttributedString *string = [[NSAttributedString alloc] initWithString:[self stringValue] attributes:attributes];
[self setAttributedStringValue:string];
[string release];
[[self attributedStringValue] drawInRect:cellFrame];
}
结果:
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowOffset:NSMakeSize(0,-1)];
[shadow setShadowColor:[NSColor whiteColor]];
[shadow setShadowBlurRadius:0];
NSMutableParagraphStyle *paragStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[paragStyle setAlignment:[self alignment]];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[self font],NSFontAttributeName,
shadow,NSShadowAttributeName,
[self textColor],NSForegroundColorAttributeName,
paragStyle,NSParagraphStyleAttributeName,nil];
[shadow release];
[paragStyle release];
NSAttributedString *string = [[NSAttributedString alloc] initWithString:[self stringValue] attributes:attributes];
[self setAttributedStringValue:string];
[string release];
[[self attributedStringValue] drawInRect:cellFrame];
}