如何抚摸 NSAttributedString 的_outside_?
我一直在 NSAttributedString
对象上使用 NSStrokeWidthAttributeName
来在绘制文本时在文本周围放置轮廓。问题在于笔划位于文本的填充区域内。当文本较小(例如 1 像素厚)时,描边会使文本难以阅读。我真正想要的是外面的一击。有什么办法可以做到这一点吗?
我尝试过没有偏移且模糊的 NSShadow
,但它太模糊并且很难看清。如果有一种方法可以增加阴影的大小而不产生任何模糊,那也是可行的。
I've been using NSStrokeWidthAttributeName
on NSAttributedString
objects to put an outline around text as it's drawn. The problem is that the stroke is inside the fill area of the text. When the text is small (e.g. 1 pixel thick), the stroking makes the text hard to read. What I really want is a stroke on the outside. Is there any way to do that?
I've tried an NSShadow
with no offset and a blur, but it's too blurry and hard to see. If there was a way to increase the size of the shadow without any blur, that would work too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然可能还有其他方法,但实现此目的的一种方法是首先仅使用笔画绘制字符串,然后仅使用填充绘制字符串,直接在先前绘制的内容之上。 (Adobe InDesign 实际上有这个内置功能,它看起来只将笔划应用于字母的外部,这有助于提高可读性)。
这只是一个示例视图,展示了如何完成此操作(灵感来自 http ://developer.apple.com/library/mac/#qa/qa2008/qa1531.html):
首先设置属性:
这会产生以下输出:
现在,它并不完美,因为字形有时会落在分数边界上,但是,它看起来肯定比默认值更好。
如果性能是一个问题,您始终可以考虑降低到稍低的级别,例如 CoreGraphics 或 CoreText。
While there may be other ways, one way to accomplish this is to first draw the string with only a stroke, then draw the string with only a fill, directly overtop of what was previously drawn. (Adobe InDesign actually has this built-in, where it will appear to only apply the stroke to the outside of letter, which helps with readability).
This is just an example view that shows how to accomplish this (inspired by http://developer.apple.com/library/mac/#qa/qa2008/qa1531.html):
First set up the attributes:
This produces the following output:
Now, it's not perfect, since the glyphs will sometimes fall on fractional boundaries, but, it certainly looks better than the default.
If performance is an issue, you could always look into dropping down to a slightly lower level, such as CoreGraphics or CoreText.
只需将我的解决方案基于 @NSGod 的答案留在这里,结果就非常相同,只需在
UILabel
内进行适当的定位。当使用默认系统字体抚摸字母时,在 iOS 14 上出现错误时也很有用(另请参阅这个问题)
错误:
Swift 版本
Just leave here my solution based on answer of @NSGod, result is pretty the same just having proper positioning inside
UILabel
It is also useful when having bugs on iOS 14 when stroking letters with default system font (refer also this question)
Bug:
Swift version