简体   繁体   English

如何将FontSizeToFitWidth调整为UIButton标签? ios

[英]How to adjustsFontSizeToFitWidth to UIButton label ? ios

when i set adjustsFontSizeToFitWidth to the button UILabel, the text go out side of the button frame, I don't know why ? 当我将AdjustsFontSizeToFitWidth设置为按钮UILabel时,文本超出了按钮框架的一侧,我不知道为什么?

here is my code: 这是我的代码:

            shareBtn = UIButton()

            shareBtn.setTitle(IconsConstants.share, forState: UIControlState.Normal)
            shareBtn.titleLabel?.font = UIFont.iconmoonFont(100)
            shareBtn.titleLabel?.adjustsFontSizeToFitWidth = true;
            shareBtn.contentVerticalAlignment = UIControlContentVerticalAlignment.Center
            shareBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Center


            //EdgeInsets

            shareBtn.contentEdgeInsets = UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10)

and here is the result: 结果如下:

在此处输入图片说明

for single line set 用于单行设置

shareBtn.titleLabel?.adjustsFontSizeToFitWidth = YES;

instead of this 代替这个

factLabel.adjustsFontSizeToFitWidth = true;

for multiple line us use 对于多行我们使用

actLabel.numberOfLines = 0;
factLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize maximumLabelSize = CGSizeMake(factLabel.frame.size.width, CGFLOAT_MAX);
CGSize expectSize = [factLabel sizeThatFits:maximumLabelSize];
factLabel.frame = CGRectMake(factLabel.frame.origin.x, factLabel.frame.origin.y, expectSize.width, expectSize.height);

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

相关问题 如果标签以两行显示,则adjustsFontSizeToFitWidth在iOS 5中不起作用 - adjustsFontSizeToFitWidth is not working in ios 5 if the label is displayed in 2 lines 如何在葫芦iOS中按标题标签查找UIButton? - How to find UIButton by title label in calabash ios? UIButton的属性文本不尊重adjustsFontSizeToFitWidth - UIButton with attributed text not respecting adjustsFontSizeToFitWidth iOS 6和iOS 7 UIButton标签垂直对齐 - iOS 6 and iOS 7 UIButton label vertical align iOS:UIButton没有增加高度以适应标题标签(没有子类化UIButton) - iOS: UIButton is not increasing height to fit Title Label (without subclassing UIButton) iOS多行标签浮动在UIButton周围 - IOS Multiple line Label floating around a UIButton 如何以编程方式更改 UIButton 标签 - How to change UIButton label programmatically 如何在 Swift 中将 label 添加到 uibutton? - How to Add label to uibutton in Swift? 在 iOS 7 中,adjustsFontSizeToFitWidth 为 YES 时,如何从 UILabel 获取调整后的字体大小? - How to obtain adjusted font size from UILabel when adjustsFontSizeToFitWidth is YES in iOS 7? adjustsFontSizeToFitWidth 在 iOS 14 上与 NSMutableAttributedString 不能正常工作 - adjustsFontSizeToFitWidth does not work properly with NSMutableAttributedString on iOS 14
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM