3

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))  
patreu22
  • 2,988
  • 4
  • 22
  • 31

2 Answers2

5

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. 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. Now you can adapte the constraint for that you need.

This work in you code?

Cristiano Alves
  • 233
  • 1
  • 13
  • as you can see in my code example that doesn't work for my Buttons[!] – patreu22 Sep 21 '15 at 14:19
  • For this code I use Label. In your application you can try remove the buttons and use the labels. – Cristiano Alves Sep 21 '15 at 14:29
  • I refer the instruction to "btn.titleLabel" so it's also working with a label. But as you see on the screenshot above it doesn't work... :( – patreu22 Sep 21 '15 at 14:33
  • The problem is the on button. Your `btn.titleLabel` really adjust to the text but the button not adjust to the `titleLabel`. You understand? – Cristiano Alves Sep 21 '15 at 14:36
  • And there is no way to make the label fit the button? – patreu22 Sep 21 '15 at 14:49
  • First: Thank you very much for taking your time! Second: No it doesn't work really right. I updated my question with the new version. But now it shows 3 (fitting) lines but deletes the rest of the title... – patreu22 Sep 21 '15 at 15:21
  • Try see my [code](https://www.dropbox.com/s/xlrexaijapyvnu8/tabbar.zip?dl=0) I hope it can help you. If don't work try use labels – Cristiano Alves Sep 21 '15 at 15:31
  • I appreciate your help, but it doesn't work for me because i have lots of other constraints and your solution destroys them. But for other people it will work fine! – patreu22 Sep 21 '15 at 15:54
0

Assuming the cells are to be of same height with same font, you can restrict the text by setting the "numberOfLines" to a value other than 0 (like 3 or 4).

Anand V
  • 423
  • 3
  • 13