简体   繁体   English

UIButton自动调整按钮字体大小Swift

[英]UIButton auto-adjust Button font size Swift

I try to make out some quiz and have a problem with my answer buttons. 我尝试进行一些测验,并且回答按钮有问题。 Simple fact: The text is too long and I try to auto-adjust it for cells using different methods. 简单事实:文本太长,我尝试使用不同的方法针对单元格自动调整文本。 My current status: 我目前的状态:

        for btn in btnArr{
        btn.titleLabel!.minimumScaleFactor = 0.3
        btn.titleLabel!.numberOfLines = 0
        btn.titleLabel!.adjustsFontSizeToFitWidth = true
        btn.titleLabel?.baselineAdjustment = UIBaselineAdjustment.AlignCenters
        //btn.contentEdgeInsets = UIEdgeInsets(top: 3.0,left: 3.0,bottom: 3.0,right: 3.0)
    }

I hope somebody has another option for me to make that work :) 我希望有人对我有另一种选择:)

Regards, Patrick 问候,帕特里克

EDIT: 编辑:

 self.view.addConstraint(NSLayoutConstraint(item: btn.titleLabel!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: btn, attribute:NSLayoutAttribute.Height, multiplier: 0.99, constant: 0))  

With auto layout you can set the space between buttons, and the max and min size. 使用自动布局,您可以设置按钮之间的间距以及最大和最小尺寸。 In code for all labels use: 在所有标签的代码中使用:

self.button.titleLabel.numberOfLines = 0;
self.button.titleLabel.adjustsFontSizeToFitWidth = YES;

With this all labels adjust the text will size. 这样,所有标签都会调整文本大小。

For adjust button to titleLabel use auto layout constraint for titleLabel. 对于对titleLabel的调整按钮,请对titleLabel使用自动布局约束。 For examples: 举些例子:

  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.button.titleLabel
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeHeight
                                                         multiplier:0.5
                                                           constant:0]];

This constraint define the height of titleLabel to 50% of self.view height. 这个约束定义titleLabel的高度的50% self.view高度。 Now you can adapte the constraint for that you need. 现在,您可以根据需要调整约束。

This work in you code? 这项工作在您的代码中吗?

假设单元格的高度和字体相同,则可以通过将“ numberOfLines”设置为非0的值(例如3或4)来限制文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何快速自动移动/自动调整笔记应用程序的保存按钮? - How to auto-move/auto-adjust the save button for a note-taking app in swift? 使 UILabel 自动将字体大小调整为屏幕宽度,而不是文本长度 - Make UILabel auto-adjust font size to screen width, but not to text length 如何在UIButton中调整文本的字体大小以在Swift中以编程方式适应宽度? - How to adjust font size of text in a UIButton to fit the width programmatically in Swift? 自动布局-方向更改后,UILabel的宽度缩小,这是由另一个应该自动调整字体大小的UILabel引起的 - Autolayout - UILabel shrinks in width after orientation change, cause by another UILabel which is supposed to auto-adjust font size 将 UIButton 字体大小调整为宽度 - Adjust UIButton font size to width 自动更改字体大小以适应swift中的按钮 - Auto change the font size to fit the button in swift 调整UIButton标签字体,以适应屏幕尺寸 - Adjust UIButton label font ,to screen size 无法使自动布局和堆栈视图正常自动调整(Swift 3,Xcode 8) - Can't get Auto Layout and Stack Views to Auto-Adjust Properly (Swift 3, Xcode 8) 调整文本的字体大小以适合 UIButton - Adjust font size of text to fit in UIButton 是否可以在iPhone中自动调整图像亮度? - Is it possible to auto-adjust the image brightness in iPhone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM