objective-c – 你如何敲击NSAttributedString的_outside_?

objective-c – 你如何敲击NSAttributedString的_outside_?,第1张

概述我在NSAttributedString对象上使用NSStrokeWidthAttributeName,在绘制时围绕文本绘制一个轮廓.问题是笔画在文本的填充区域内.当文本较小(例如1像素厚)时,抚摸使文本难以阅读.我真正想要的是外面的中风.有什么办法吗? 我试过一个没有偏移和模糊的NSShadow,但它太模糊,很难看到.如果有一种方法来增加阴影的大小,没有任何模糊,那么也可以. 虽然可能还有其他方 我在NSAttributedString对象上使用NSstrokeWIDthAttributename,在绘制时围绕文本绘制一个轮廓.问题是笔画在文本的填充区域内.当文本较小(例如1像素厚)时,抚摸使文本难以阅读.我真正想要的是外面的中风.有什么办法吗?

我试过一个没有偏移和模糊的NSShadow,但它太模糊,很难看到.如果有一种方法来增加阴影的大小,没有任何模糊,那么也可以.

解决方法 虽然可能还有其他方法,但一种方法是先绘制一个只有笔画的字符串,然后再绘制一个仅填充的字符串,直接覆盖以前绘制的字符串. (Adobe InDesign实际上有这个内置的,它似乎只能将笔画应用到字母的外面,这有助于可读性).

这只是一个示例视图,显示了如何完成此 *** 作(灵感来自http://developer.apple.com/library/mac/#qa/qa2008/qa1531.html):

首先设置属性:

@implementation MdindesignTextVIEwstatic 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],nil] retain];    }    if (indesignForegroundAttributes == nil) {        indesignForegroundAttributes = [[NSMutableDictionary        dictionaryWithObjectsAndKeys:        [NSFont systemFontOfSize:64.0],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.

总结

以上是内存溢出为你收集整理的objective-c – 你如何敲击NSAttributedString的_outside_?全部内容,希望文章能够帮你解决objective-c – 你如何敲击NSAttributedString的_outside_?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1028057.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存