我可以用 FXLabel *替换* UIButton 上的 UILabel 吗?
因此,我尝试将 UIButton
上的 UILabel
更改为 FXLabel
的实例。查看有关 FXLabel
的更多信息 (https://github.com/nicklockwood/ FXLabel)。我知道通过使用类扩展(子类 UIButton 添加属性),我总是可以添加另一个属性,在我的例子中,基本上只需添加一个子视图即可添加一个 FXLabel
。不过,我喜欢已经存在的 titleLabel
属性的便利性和功能。
是否有某种方法可以按子类或类别“切换”UILabel
为 FXLabel
?
我确实知道我可以做到这一点,但这只是一种黑客行为,而且不能保证未来的发展。我不知道如何更改类类型(从 UIButton 中获取 UILabel)。
UILabel *label;
for (NSObject *view in button.subviews) {
if ([view isKindOfClass:[UILabel class]]) {
label = (UILabel *)view;
break;
}
}
想法??谢谢。
So, I'm trying to change the UILabel
on a UIButton
to an instance of an FXLabel
instead. Check out the link for more on FXLabel
(https://github.com/nicklockwood/FXLabel). I know by using class extensions (Subclass UIButton to add a property), I could always add another property, in my case, an FXLabel
by just adding a subview basically. However, I like the convenience and features of the titleLabel
property already present.
Is there some method to "switch" the UILabel
for an FXLabel
by subclass or category?
I do know I could do this, but it's such a hack and isn't future proof. And I don't know how to then change the class type (Get UILabel out of UIButton).
UILabel *label;
for (NSObject *view in button.subviews) {
if ([view isKindOfClass:[UILabel class]]) {
label = (UILabel *)view;
break;
}
}
Thoughts?? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您至少可以访问该标签的另一种方式。创建
UIButton
的子类,并在该子类中重写layoutSubviews
。从那里您可以直接访问标签:现在我们仍然遇到将该标签更改为子类的问题。我想您可以尝试将
UILabel
的类与您的FXLabel
子类进行动态交换(详细信息这里),但说实话,这是相当危险的。我想说,您最好创建自己的
UIControl
,您可以根据自己的喜好进行自定义。没有风险,只有纯粹的善良。模仿一个简单的UIButton
应该不会太难。祝你好运!Here's another way you could at least access the label. Create a subclass of
UIButton
, and in that subclass overridelayoutSubviews
. From there you can access the label directly:Now we're still stuck with the problem of changing that label to be a subclass. I guess you could try to dynamically swap the
UILabel
's class with yourFXLabel
subclass (as detailed here), but to be honest that's pretty risky.I'd say you're better off just creating your own
UIControl
that you can customize to your hearts content. No risk, just pure goodness. It shouldn't be too hard to imitate a simpleUIButton
. Good luck!很抱歉这么说,但我认为你不能以任何可靠的方式做到这一点。即使您继承了
UIButton
并覆盖了titleLabel
方法,您也必须处理状态设置。而且你永远不知道内部代码何时直接引用标签的私有 ivar。我为我的应用程序创建了一个 UIButton 的子类来执行类似的操作(自定义按钮布局和子视图),抱歉我现在无法共享它,它由客户拥有。为您自己的子视图创建您自己的状态处理代码并不太困难。
Sorry to say it but I don't think you can do this in any reliable way. Even if you subclassed
UIButton
and overrode thetitleLabel
methods you'd also have to handle the state settings. And you never know when the internal code refers directly to a private ivar for the label.I created a subclass of UIButton for my apps to do something similar (custom button layouts & subviews), sorry I can't share it right now, it's owned by a client. It's not too difficult to create your own state handling code for your own subviews.