隐藏 UIButton 标题

发布于 2024-12-02 18:40:56 字数 186 浏览 3 评论 0原文

我在滚动视图中有几个 UIButtons,我用它们来传递某些信息。该信息保存在每个 uibutton 的标题中,单击按钮时,会将其标题传递给函数。

我想做的就是隐藏按钮的标题,这样您就看不到该按钮。我将它们覆盖在用于显示按钮的图像上。我将文本设置为透明,但单击时它仍然变成白色。

如果您在解释中包含代码,请解释它应该放在哪里。

I have several UIButtons in a scrollview which I use in order to pass certain information. The information is saved in the title of each uibutton and when the button is clicked, it passes its title into the function.

All I want to do is hide the title of the button so you can not see the button. I have them overlaid over images which I use to show buttons. I have the text set to transparent but it still turns white when it is being clicked.

If you include code in your explanation, please explain where it should go.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(13

吲‖鸣 2024-12-09 18:40:56

在IOS7之后,如果您只想隐藏按钮的titleLabel上的标题,您可以执行以下操作。这样标题仍然存在,只是使其不可见。如果您执行 NSLog("%@",button.currentTitle) 您将在终端中看到标题。希望这有帮助。

[button setTitle:@"Button Title" forState:UIControlStateNormal];
button.titleLabel.layer.opacity = 0.0f;

After IOS7, If you want to just hide the title on titleLabel of a button you can do as follow. This way the title is still there it just makes it invisible. if you do NSLog("%@",button.currentTitle) you will see the title in terminal. Hope this helps.

[button setTitle:@"Button Title" forState:UIControlStateNormal];
button.titleLabel.layer.opacity = 0.0f;
影子是时光的心 2024-12-09 18:40:56

我发现只有一种正确的工作方式:

//hide
yourButton.setTitleColor(UIColor.clearColor(), forState: .Normal)

//show (put your color)
yourButton.setTitleColor(UIColor.blackColor(), forState: .Normal)

I found only one correct working way:

//hide
yourButton.setTitleColor(UIColor.clearColor(), forState: .Normal)

//show (put your color)
yourButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
流心雨 2024-12-09 18:40:56

使用 button.titleLabel.hidden = YES 将不起作用(至少在 iOS 7 上)。

我最终使用了:

// remove the button since hiding it doesn't work
[button.titleLabel removeFromSuperview];
// put back when you're done
[button addSubview:button.titleLabel];

using button.titleLabel.hidden = YES will not work (at least on on iOS 7).

I ended up using:

// remove the button since hiding it doesn't work
[button.titleLabel removeFromSuperview];
// put back when you're done
[button addSubview:button.titleLabel];
白况 2024-12-09 18:40:56

您可以将标签隐藏在按钮内:

button.titleLabel.hidden=YES;

或者将按钮的标题设置为 @"" 并在您想要检索该值时将其保存在其他位置。

You can hide the label inside the button:

button.titleLabel.hidden=YES;

or set the button's title to @"" and save the value somewhere else when you want to retrieve it.

熊抱啵儿 2024-12-09 18:40:56

我创建了 UIButton 的子类并重写 layoutSubviews 方法。在 layoutSubviews 方法中隐藏 titleLabel 是有效的。

public class LoadingButton: UIButton {
    public var isTitleHidden: Bool = false {
        didSet {
           titleLabel?.isHidden = isTitleHidden
        }
    }
    public override func layoutSubviews() {
        super.layoutSubviews()
        titleLabel?.isHidden = isTitleHidden
    }
}

如果想隐藏 titleLabel,只需设置 isTitleHidden = true

I create a subclass of UIButton and override layoutSubviews method. Hiding titleLabel in layoutSubviews method works.

public class LoadingButton: UIButton {
    public var isTitleHidden: Bool = false {
        didSet {
           titleLabel?.isHidden = isTitleHidden
        }
    }
    public override func layoutSubviews() {
        super.layoutSubviews()
        titleLabel?.isHidden = isTitleHidden
    }
}

if wanna hide titleLabel, just set isTitleHidden = true

夏日浅笑〃 2024-12-09 18:40:56

我无法从 titleLabel 或整个视图中删除标题,因为我需要它作为约束。

我最终使用

isEnabled = false
titleLabel?.layer.opacity = 0
setTitleColor(.clear, for: .disabled)

隐藏标题并

isEnabled = true
titleLabel?.layer.opacity = 1
setTitleColor(titleColor(for: .normal), for: .disabled)

再次显示它

I could not remove the title from the titleLabel nor the whole view as I needed it for constraints.

I ended up using

isEnabled = false
titleLabel?.layer.opacity = 0
setTitleColor(.clear, for: .disabled)

to hide the title and

isEnabled = true
titleLabel?.layer.opacity = 1
setTitleColor(titleColor(for: .normal), for: .disabled)

to show it again

三生一梦 2024-12-09 18:40:56

要临时隐藏标题,只需将Title设置为空字符串,

setTitle("", for: .normal)

按钮标题标签将被隐藏,但标题仍将位于titleLabel中,您可以使用以下命令将其返回

setTitle(titleLabel?.text, for: .normal)

To hide a title temporary just setTitle to empty string

setTitle("", for: .normal)

the button title label will be hidden but the title will still in the titleLabel, you can return it back using

setTitle(titleLabel?.text, for: .normal)
冬天旳寂寞 2024-12-09 18:40:56

如果您想在禁用按钮时临时隐藏标题,请使用:

setTitle("Title", for: .normal)
setTitle("", for: .disabled)

然后,当您想隐藏标题时,button.isEnabled = false

If you want to temporary hide the title, while you disabling the button, use:

setTitle("Title", for: .normal)
setTitle("", for: .disabled)

Then, button.isEnabled = false, when you want to hide the title.

温折酒 2024-12-09 18:40:56

我遇到了标题问题,因为使用了属性标题,但上面没有任何帮助。然后我找到了一个解决方法:

button.titleEdgeInsets = .init(top: 0, left: shouldHide ? 1000 : 0, bottom: 0, right: 0)

但是它有一些缺点,但适合我的需求。

I got a problem with title, because used an attributed title and nothing above helped. Then i found a workaround:

button.titleEdgeInsets = .init(top: 0, left: shouldHide ? 1000 : 0, bottom: 0, right: 0)

However it has some disadvantages, but fit my needs.

独自←快乐 2024-12-09 18:40:56

我提出了这个解决方案,它允许您设置标题标签文本并将其与按钮图像一起使用,而不显示它,也不将按钮图像移动到左侧。

- (void)hideButtonLabel:(UIButton*)buttonInp {

    buttonInp.titleLabel.layer.opacity = 0.0f;
    uttonInp.titleLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:0.0];

}

I've come up with this solution, which allows you to set title label text and use it with button image without showing it and not moving button image to the left.

- (void)hideButtonLabel:(UIButton*)buttonInp {

    buttonInp.titleLabel.layer.opacity = 0.0f;
    uttonInp.titleLabel.font = [UIFont fontWithName:@"Helvetica-Light" size:0.0];

}
以歌曲疗慰 2024-12-09 18:40:56

您无法使用 .hidden 属性隐藏 UIButton titleLabel。相反,你可以这样做。

隐藏:

[self.yourButton setTitle:nil forState:UIControlStateNormal];

显示:

[self.yourButton setTitle:@"Your Text" forState:UIControlStateNormal];

You cannot hide UIButton titleLabel using .hidden property. Instead you can do this.

To Hide:

[self.yourButton setTitle:nil forState:UIControlStateNormal];

To Show:

[self.yourButton setTitle:@"Your Text" forState:UIControlStateNormal];
离笑几人歌 2024-12-09 18:40:56

在 Swift 中,

您不需要隐藏也不需要将不透明度设置为 0.0。 Swift 给了你一个更简单的方法。
只需将标题设置为零即可。事实上,我是从文档中得到这个想法的。

命令单击 setTitle(_:for:) 方法,您将看到 -

open func setTitle(_ title: String?, for state: UIControl.State) // default is nil. title is assumed to be single line 

所以,我只是将其设置为 nil。

setTitle(nil, for: .normal)

In Swift-

You don't need to hide nor need to make the opacity to 0.0. Swift gave you a simpler way.
Just set the title as nil. In fact, I got the idea from the documentation.

Command click on the setTitle(_:for:) method and you will see-

open func setTitle(_ title: String?, for state: UIControl.State) // default is nil. title is assumed to be single line 

So, I just set it to nil.

setTitle(nil, for: .normal)
铁轨上的流浪者 2024-12-09 18:40:56

Swift 5隐藏按钮标签:

myButton.titleLabel?.isHidden = true

这里myButton是按钮的@IBOutlet。

Swift 5 to hide button label:

myButton.titleLabel?.isHidden = true

Here myButton is a @IBOutlet of the button.

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