有人尝试过在 UIButton 中集成自定义字体吗?
尝试将 FontLabel (http://github.com/zynga/FontLabel) 放入 UIButton 中。起初我想设置titlelabel。但它限制了它的设置器,所以我不能只为其设置标签。
只是想知道是否有人有想法在不使用图像来设置我的自定义按钮的情况下执行此操作。
Trying to put in FontLabel (http://github.com/zynga/FontLabel) into a UIButton. At first I wanted to set titlelabel. But it's restricting a setter to it so i can't just set the label to it.
Just wondering if anyone has an idea in doing this without using images to set my custom button.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过类别扩展 UILabel 类以添加一些功能。问题是 UIButton 创建了自己的 UILabel,并且您无法(安全地)覆盖它。因此,如果您选择使用 UIButton,则更改标准 UILabel 标签行为的唯一选择是实际更改 UILabel 类(通过类别)。
或者,您可以编写自己的 UIControl 并模仿 UIButton 的功能。然后你就可以做任何你想做的事了。
编辑 - 如果 FontLabel 类从 UILabel 扩展,则可以扩展 UIButton 并使用方法调整来强制它实例化 FontLabel 作为其 titleLabel。但这是相当极端的,我认为你最好编写自己的模仿按钮行为的 UIControl。
You could extend the UILabel class through a category to add some functionality. The problem is that UIButton creates its own UILabel, and you can't override that (safely). So if you choose to use a UIButton, your only option for changing the label's behavior from that of a standard UILabel is to actually change the UILabel class (via categories).
Alternatively, you could write your own UIControl and mimic the functionality of UIButton. Then you can do whatever you want.
EDIT - If that FontLabel class extends from UILabel, it may be possible to extend UIButton and use method swizzling to force it to instantiate a FontLabel as its titleLabel. This is pretty extreme though, and I think you're much better off writing your own UIControl that mimics button behavior.
这有点旧,但您可以在项目中导入字体文件(.ttf)并使用以下命令调用它们:
This is a bit old but you can just import the font files (.ttf) in your project and call them with: