0

when i set adjustsFontSizeToFitWidth to the button UILabel, the text go out side of the button frame, I don't know why ?

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:

enter image description here

Wain
  • 118,658
  • 15
  • 128
  • 151
david
  • 3,310
  • 7
  • 36
  • 59
  • remove the inset, set an allowed font scale – Wain Jan 20 '16 at 10:01
  • what changed? is the text resizing? what is your intention with the inset? – Wain Jan 20 '16 at 10:48
  • @Wain , the share icon still go outside the button frame ! , I am using inset to set padding for the icon. please note that the white border is a button layer which describe the button frame border – david Jan 20 '16 at 10:54

1 Answers1

0

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);
viratpuar
  • 524
  • 1
  • 5
  • 22
  • thank you for your answer , do you mean that i should use UILable instead of UIButton ? – david Jan 20 '16 at 10:17