UIButton 中的轮廓文本/字体
我正在开发一个项目,其中 UIButtons 可以根据用户设置动态设置其字体颜色。为了避免浅色背景上的浅色字体或深色背景上的深色字体的问题,我想用细的黑色轮廓勾勒出我的字体。我说的是直接围绕字母的轮廓,而不是围绕标签或按钮本身的轮廓。类似于典型的 LOLcat Impact 字体轮廓: http://mintyferret.com/ wp-content/uploads/2007/07/lolcat7.gif
有人有这样的例子吗?如果您有更简单的东西,我愿意接受建议,但我并不是真的想为我的按钮创建自定义图像。它们可以有动态文本和动态颜色,所以我需要它非常灵活。
I'm working on a project where UIButtons can have their font color dynamically set depending on user settings. To avoid the issue of a light font on a light background or a dark font on a dark background I'd like to outline my font with a thin, black outline. I'm talking about an outline directly around the letters, not around the label or button itself. Something like the typical LOLcat Impact font outline: http://mintyferret.com/wp-content/uploads/2007/07/lolcat7.gif
Does anyone have an example of this in action? I'm open to suggestions if you have something easier, but I'm not really in a place where I want to create custom images for my buttons. They can have dynamic text and dynamic colors, so I need it to be pretty flexible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UIButton 在 UILabel 中包含其标题,并且 UILabel 没有选项在字符周围添加边框。 UIButton 的 titleLabel 属性是只读的,因此您不能将其替换为您自己的 UILabel 子类。因此,唯一的选择似乎是在按钮的 drawRect 函数中绘制文本。
这是有关如何在文本周围绘制边框的主题:
如何让 UILabel 显示轮廓文本?
非常确定您必须使用自己的变量覆盖 setTitle:forState: 和 currentTitle 函数,否则按钮将在您绘制的文本后面或前面绘制文本。
UIButton contains its title in a UILabel and UILabel has no option to add borders around your characters. The titleLabel property of UIButton is read-only, so you can't replace it with your own UILabel subclass. Therefore, the only option appears to be to draw the text in the button's drawRect function.
Here's a topic on how to draw borders around text:
How do I make UILabel display outlined text?
I'm pretty sure you'll have to override the setTitle:forState: and currentTitle functions using your own variables, or the button will draw text behind or in front of the text you draw.