设置UIButton的titleLabel.font属性无效。

26

我正在使用iOS 7,尝试增加我的UIButton的titleLabel的字体大小。我是这样做的:

[[_centerButton titleLabel] setFont:[UIFont boldSystemFontOfSize:28.0]];

然而,这并没有起任何作用。每次我以不同的大小编译时,字体大小始终保持不变。即使我完全删除了那行代码,字体大小仍然保持不变。显然它仍然使用默认的大小。

我如何增加UIButton标题的字体大小?

8个回答

72

使用 Xcode 13 ,我不得不在 Interface Builder 中将 Style 属性从 Plain 更改为 Default

enter image description here

然后设置标题后,我最终可以在程序中轻松更改字体:

button.setTitle("The title", for: .normal)
button.titleLabel?.font = UIFont(name: "Montserrat-SemiBold", size: 15)

1
谢谢!这正是我的情况,对于那些拥有设计系统或类似系统并且不想自定义storyboards的人,你可以使用button.configuration = nil。关于iOS 15更多信息,请查看此处 - rgkobashi
1
是的,这真的帮了我大忙。谢谢你。不确定为什么苹果觉得有必要在这里搞些小动作。 - Brian Sachetta
那么样式是如何工作的呢?如果我们将样式设置为普通,使用的字体是什么?它的粗细是多少?有人能解释一下吗? - hasan
这对我有用,虽然我不确定发生了什么。 - chengsam
你真是个英雄...这个帮我省了太多时间去琢磨苹果的方法。谢谢你。 - undefined

14

1
作者似乎意识到方法是正确的,但显然他们想知道为什么在他们的情况下它不起作用。如果您没有足够的信息来回答问题,可以提出澄清问题。说“应该可以工作”,而实际上并不能工作,这不是一个答案。 - lazarevzubov

3

我曾经遇到过同样的问题,你使用了Interface Builder吗?当我将标题属性设置为Plain时,它就可以正常工作了,非常奇怪。


2
如果您使用Storyboard,那么当您点击一个UIButton时,您可以在属性检查器中看到一个名为Font的属性。默认情况下,它显示为“Custom”(空白)。点击T符号并选择System。然后,您可以选择字体大小。
希望它有所帮助。
(图片略)

1
有点晚了……在调用setTitle()后,titleLabel 的某些属性会被重置。链接

0

如果您正在尝试使用 iOS 15 中的新 UIButton.configuration 进行此操作,则更改 titlelabel?.font 不起作用,因为由于现在需要设置 configuration?.title = "some title",所以 configuration 字段将使用系统样式覆盖它。

您需要创建一个 AttributedString 并设置 configuration?.attributedTitle = yourAttributedString

我的整个应用程序都是编程方式创建的,因此这里是我创建的自定义按钮“红色药丸”样式按钮的示例,它使用方便的 init 动态设置按钮标题。

class APButton: UIButton {

    override init(frame: CGRect) {
        super.init(frame: frame)
        configure()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    convenience init(title: String) {
        self.init(frame: .zero)
        setDynamicTitle(title: title)
    }
    
    private func configure() {
        configuration = .filled()
        configuration?.cornerStyle = .capsule
        configuration?.baseBackgroundColor = .red
        configuration?.baseForegroundColor = .white

        translatesAutoresizingMaskIntoConstraints = false
    }
    
    private func setDynamicTitle(title: String) {
        let font = UIFont.systemFont(ofSize: 20)
        let container = AttributeContainer([NSAttributedString.Key.font: font])
        let attribString = AttributedString(title, attributes: container)
        
        configuration?.attributedTitle = attribString
    }

}

0
以下扩展应该可以正常工作。请尝试一下。
extension UIButton {
    func setFont(_ font: UIFont) {
        if #available(iOS 15.0, *) {
            self.configuration?.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
                var outgoing = incoming
                outgoing.font = font
                return outgoing
            }
        }
        else {
            self.titleLabel?.font = font
        }
    }
}

-2

在您的情况下,问题是您没有设置应该获取特定fontSizeUIControlStateNormal

我通常创建一个类型为UIButton的类,并在awakeFromNib中设置fontSize看看这篇文章。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接