您如何抚摸nsattribedstring的_outside_?
-
11-10-2019 - |
题
我一直在使用 NSStrokeWidthAttributeName
上 NSAttributedString
对象在绘制文本时围绕文本进行轮廓。问题是中风在文本的填充区域内。当文本很小(例如1像素厚)时,抚摸会使文本难以阅读。我真正想要的是外面的中风。有什么方法可以做到吗?
我尝试了一个 NSShadow
没有偏移和模糊,但是太模糊了,很难看到。如果有一种方法可以增加阴影的大小而没有任何模糊,那也可以。
解决方案
虽然可能还有其他方法,但一种方法是首先只用中风绘制字符串,然后仅用填充绘制字符串直接绘制,直接绘制了以前绘制的内容。 (Adobe Indesign实际上具有此内置,似乎只将笔画应用于字母的外部,这有助于可读性)。
这只是一个示例视图,显示如何完成此操作(受 http://developer.apple.com/library/mac/#qa/qa2008/qa1531.html):
首先设置属性:
@implementation MDInDesignTextView
static NSMutableDictionary *regularAttributes = nil;
static NSMutableDictionary *indesignBackgroundAttributes = nil;
static NSMutableDictionary *indesignForegroundAttributes = nil;
- (void)drawRect:(NSRect)frame {
NSString *string = @"Got stroke?";
if (regularAttributes == nil) {
regularAttributes = [[NSMutableDictionary
dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize:64.0],NSFontAttributeName,
[NSColor whiteColor],NSForegroundColorAttributeName,
[NSNumber numberWithFloat:-5.0],NSStrokeWidthAttributeName,
[NSColor blackColor],NSStrokeColorAttributeName, nil] retain];
}
if (indesignBackgroundAttributes == nil) {
indesignBackgroundAttributes = [[NSMutableDictionary
dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize:64.0],NSFontAttributeName,
[NSNumber numberWithFloat:-5.0],NSStrokeWidthAttributeName,
[NSColor blackColor],NSStrokeColorAttributeName, nil] retain];
}
if (indesignForegroundAttributes == nil) {
indesignForegroundAttributes = [[NSMutableDictionary
dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize:64.0],NSFontAttributeName,
[NSColor whiteColor],NSForegroundColorAttributeName, nil] retain];
}
[[NSColor grayColor] set];
[NSBezierPath fillRect:frame];
// draw top string
[string drawAtPoint:
NSMakePoint(frame.origin.x + 200.0, frame.origin.y + 200.0)
withAttributes:regularAttributes];
// draw bottom string in two passes
[string drawAtPoint:
NSMakePoint(frame.origin.x + 200.0, frame.origin.y + 140.0)
withAttributes:indesignBackgroundAttributes];
[string drawAtPoint:
NSMakePoint(frame.origin.x + 200.0, frame.origin.y + 140.0)
withAttributes:indesignForegroundAttributes];
}
@end
这会产生以下输出:
现在,它并不完美,因为字形有时会落在分数边界上,但是看起来肯定比默认值更好。
如果性能是一个问题,您总是可以考虑下降到稍低的级别,例如CoreGraphics或CoreText。
不隶属于 StackOverflow