iOS:UILabel 在每个文本字母周围带有边框
我试图在文本的每个字母周围绘制边框/轮廓,以帮助文本脱颖而出。阴影对我来说还不够。我查看了另一个问题,但它使用 Quartz 仅在文本字段轮廓周围绘制边框,而不是文本本身。有没有一种相对简单的方法可以为文本添加边框,或者为文本添加多组阴影?
I'm trying to draw a border/outline around each letter of text to help the text stand out. Shadows aren't doing enough for me. I looked at another question, but it used Quartz to only draw a border around the textfield outline, not the text itself. Is there a relatively easy way to add a border to the text, or multiple sets of shadows to text?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以子类化 UILabel 类并重写 drawInRect 方法,如下所示:
You can subclass the UILabel class and override the drawInRect method as such:
当您将阴影应用于标签时,每个字母周围都会有阴影。这就是说,默认情况下,分组表中的节标题被绘制为看起来是浮雕的。
所以,就像你能做的那样,
你也许能做,
我还没有尝试过,但理论上它应该有效。我遇到过一些情况,在某些情况下,这些事情并不总是有效或与您预期的不同。事实上,影子就是一个很好的例子,带有标签。我曾经期望向标签添加阴影会将其应用到标签的矩形周围。但事实上,它将它应用于标签中的每个字母!不是我所期望的,但事后看来非常方便。
无论如何,请尝试一下。如果它不起作用,并且它实际上是您想要的每个字母周围的“边框”,您可能必须使用具有非常对比色(与您的文本颜色)的细阴影来达到您想要的亮点。
使用任何这些图层效果,你需要祝
你好运!回来报告。 :-)
编辑:如果您只想在标签周围有一个矩形边框,则必须将标签嵌入到 UIView 中,并使用视图的 layer.borderXXX 属性(如上所述)对其应用边框。
When you apply shadow to a label, each letter gets the shadow around it. This is how say, section headers, by default, in a grouped table are drawn so as to look embossed.
So, just as you can do
you may be able to do
I haven't tried this, but in theory it ought to work. There are instances I've run into where these kinds of things don't always work or work differently than you might expect in certain circumstances. Shadow is a good example, actually, with labels. I once expected that adding shadow to a label applied it around the label's rect. But in fact, it applies it around each letter in the label! Not what I expected, but pretty handy in hindsight.
Anyway, give this a try. If it doesn't work, and it is in fact a "border" around each letter that you want, you may have to use a thin shadow with a very contrasting color (from your text color) to achieve the highlight you desire.
Do use any of these layer effects, you need to
Good luck! Report back. :-)
EDIT: If you simply want a rectangular border around your label, you will have to embed your label in a UIView, and apply a border to that using the view's layer.borderXXX properties (as above).
或者,您可以创建 UILable 的子类并在 drawRect 方法中绘制您想要的任何内容,
您可以使用以下几行来查找文本宽度和高度
Or else you can make a subclass of UILable and draw whatever you want in drawRect method
you can use following lines to find text width and height