iOS - 如何实现UILabel上文字的浮雕效果?
我可以知道如何将浮雕效果设置为文本“提醒”如图所示?
看起来文字是嵌入的?
谢谢
Can I know how to have the emboss effect as the text "Reminders" as shown on the picture?
It looks like the text are embedded?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
iOS 7.0 的更新
在 iOS 7.0 中,Apple 为属性字符串添加了一个新属性
NSTextEffectAttributeName
。如果您的部署目标是 iOS 7.0 或更高版本,您可以将此属性设置为 NSTextEffectLetterpressStyle 来以浮雕样式绘制属性字符串。原创
我无法确定苹果是如何绘制浮雕文本的。在我看来,他们用微红色填充字符串字形,然后在字形的内部边缘周围应用阴影,并沿着字形的顶部外边缘应用非常微弱的阴影。我尝试了一下,如下所示:
顶部是我的渲染。下面是一个带有阴影的简单 UILabel,正如 Chris 在他的回答中所建议的那样。我在后台放置了提醒应用程序的屏幕截图。
这是我的代码。
首先,您需要一个函数来创建字符串的图像蒙版。您将使用遮罩来绘制字符串本身,然后绘制仅出现在字符串内部边缘周围的阴影。该图像只有一个 Alpha 通道,没有 RGB 通道。
其次,您需要一个反转该掩码的函数。您将使用它来使 CoreGraphics 在字符串的内边缘周围绘制阴影。这需要是完整的 RGBA 图像。 (iOS 似乎不支持灰度+alpha 图像。)
您可以在函数中使用它们,以红色绘制字符串并将阴影应用于其内部边缘。
接下来,您需要一个函数来获取图像并返回带有微弱向上阴影的副本。
最后,您可以组合这些功能来创建字符串的浮雕图像。我将最终图像放入
UIImageView
中以便于测试。UPDATE FOR iOS 7.0
In iOS 7.0, Apple added a new attribute,
NSTextEffectAttributeName
, for attributed strings. If your deployment target is iOS 7.0 or later, you can set this attribute toNSTextEffectLetterpressStyle
to draw an attributed string in an embossed style.ORIGINAL
I can't say for certain how Apple draws the embossed text. It looks to me like they fill the string glyphs with a reddish color, then apply a shadow around the interior edges of the glyphs, and also apply a very faint shadow along the top outside edges of the glyphs. I tried it out and here's what it looks like:
On top is my rendering. Below that is a simple UILabel with shadow as Chris suggested in his answer. I put a screen shot of the Reminders app in the background.
Here's my code.
First, you need a function that creates an image mask of your string. You'll use the mask to draw the string itself, and then to draw a shadow that only appears around the inside edges of the string. This image just has an alpha channel and no RGB channels.
Second, you need a function that inverts that mask. You'll use this to make CoreGraphics draw a shadow around the inside edges of the string. This needs to be a full RGBA image. (iOS doesn't seem to support grayscale+alpha images.)
You can use those in a function that draws the string in red and applies a shadow to its interior edges.
Next you need a function that takes an image and returns a copy with a faint upward shadow.
Finally, you can combine those functions to create an embossed image of your string. I put my final image into a
UIImageView
for easy testing.这看起来就像文本周围的阴影。您可以在 IB 中设置文本颜色的同一区域中进行设置 - 为阴影选择适当的颜色并设置您想要的阴影偏移方式(在您发布的示例中,看起来他们将阴影颜色设置为相同的颜色)作为文本,并将其水平偏移 0,垂直偏移 -1(这意味着向上一个像素)。
在代码中,属性设置如下(假设您已经设置了一个名为“label”的 UILabel:
That just looks like a shadow around the text. You set it in IB in the same area where you set the text color - pick an appropriate color for the shadow and set how you want the shadow offset (in the example you posted, it looks like they set the shadow color to the same color as the text and offset it 0 horizontal and -1 vertical (which means one pixel up).
In code, the properties are set like this (assuming you have already set up a UILabel named, appropriately, "label":
您可以在此示例的基础上配置您的效果
You can configure your effect on the basis of this example