有没有人尝试在UIButton中集成自定义字体?

时间:2022-10-14 23:22:28

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.

试图将FontLabel(http://github.com/zynga/FontLabel)放入UIButton。起初我想设置标题标签。但它限制了一个setter,所以我不能只为它设置标签。

Just wondering if anyone has an idea in doing this without using images to set my custom button.

只是想知道是否有人有这样做的想法,而不使用图像来设置我的自定义按钮。

2 个解决方案

#1


0  

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).

您可以通过类别扩展UILabel类以添加一些功能。问题是UIButton创建了自己的UILabel,你不能(安全地)覆盖它。因此,如果您选择使用UIButton,那么从标准UILabel更改标签行为的唯一选择是实际更改UILabel类(通过类别)。

Alternatively, you could write your own UIControl and mimic the functionality of UIButton. Then you can do whatever you want.

或者,您可以编写自己的UIControl并模仿UIButton的功能。然后你可以做任何你想做的事。

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.

编辑 - 如果FontLabel类从UILabel扩展,则可以扩展UIButton并使用方法swizzling强制它将FontLabel实例化为其titleLabel。这是非常极端的,我认为你编写自己的模仿按钮行为的UIControl要好得多。

#2


0  

This is a bit old but you can just import the font files (.ttf) in your project and call them with:

这有点旧,但您可以在项目中导入字体文件(.ttf)并使用以下命令调用它们:

-[UIFont fontWithName:size:]

#1


0  

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).

您可以通过类别扩展UILabel类以添加一些功能。问题是UIButton创建了自己的UILabel,你不能(安全地)覆盖它。因此,如果您选择使用UIButton,那么从标准UILabel更改标签行为的唯一选择是实际更改UILabel类(通过类别)。

Alternatively, you could write your own UIControl and mimic the functionality of UIButton. Then you can do whatever you want.

或者,您可以编写自己的UIControl并模仿UIButton的功能。然后你可以做任何你想做的事。

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.

编辑 - 如果FontLabel类从UILabel扩展,则可以扩展UIButton并使用方法swizzling强制它将FontLabel实例化为其titleLabel。这是非常极端的,我认为你编写自己的模仿按钮行为的UIControl要好得多。

#2


0  

This is a bit old but you can just import the font files (.ttf) in your project and call them with:

这有点旧,但您可以在项目中导入字体文件(.ttf)并使用以下命令调用它们:

-[UIFont fontWithName:size:]