我可以用 FXLabel *替换* UIButton 上的 UILabel 吗?

发布于 2024-12-22 14:58:10 字数 919 浏览 2 评论 0原文

因此,我尝试将 UIButton 上的 UILabel 更改为 FXLabel 的实例。查看有关 FXLabel 的更多信息 (https://github.com/nicklockwood/ FXLabel)。我知道通过使用类扩展(子类 UIButton 添加属性),我总是可以添加另一个属性,在我的例子中,基本上只需添加一个子视图即可添加一个 FXLabel 。不过,我喜欢已经存在的 titleLabel 属性的便利性和功能。

是否有某种方法可以按子类或类别“切换”UILabelFXLabel

我确实知道我可以做到这一点,但这只是一种黑客行为,而且不能保证未来的发展。我不知道如何更改类类型(从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷弦 2024-12-29 14:58:10

这是您至少可以访问该标签的另一种方式。创建 UIButton 的子类,并在该子类中重写 layoutSubviews。从那里您可以直接访问标签:

- (void)layoutSubviews {
    [super layoutSubviews];

    UILabel *titleLabel = [self titleLabel];
}

现在我们仍然遇到将该标签更改为子类的问题。我想您可以尝试将 UILabel 的类与您的 FXLabel 子类进行动态交换(详细信息这里),但说实话,这是相当危险的。

我想说,您最好创建自己的 UIControl ,您可以根据自己的喜好进行自定义。没有风险,只有纯粹的善良。模仿一个简单的 UIButton 应该不会太难。祝你好运!

Here's another way you could at least access the label. Create a subclass of UIButton, and in that subclass override layoutSubviews. From there you can access the label directly:

- (void)layoutSubviews {
    [super layoutSubviews];

    UILabel *titleLabel = [self titleLabel];
}

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 your FXLabel 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 simple UIButton. Good luck!

烟柳画桥 2024-12-29 14:58:10

很抱歉这么说,但我认为你不能以任何可靠的方式做到这一点。即使您继承了 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 the titleLabel 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文