Multiline UIButton and autolayout

I had the same problem where I wanted my button to grow along with its title. I had to sublcass the UIButton and its intrinsicContentSize so that it returns the intrinsic size of the label. – (CGSize)intrinsicContentSize { return self.titleLabel.intrinsicContentSize; } Since the UILabel is multiline, its intrinsicContentSize is unknown and you have to set …

Read more

UIButton title changes to default

You need to use setTitle:forState: method instead of setting the titleLabel.text property: [startButton setTitle:@”Start” forState:UIControlStateNormal]; // Normal and highlighted titles do not need to be the same [startButton setTitle:@”Start!” forState:UIControlStateHighlighted]; What happens now is that you set the title in the label that represents the view of the current state, but once the state changes …

Read more

Adjust font size of text to fit in UIButton

self.mybutton.titleLabel.minimumScaleFactor = 0.5f; self.mybutton.titleLabel.numberOfLines = 0; <– Or to desired number of lines self.mybutton.titleLabel.adjustsFontSizeToFitWidth = YES; … did the trick, after layoutIfNeeded in viewDidLoad As it turns out, all those must be set to actually adjust the font-size, not just making it fit into the frame. Update for Swift 3: mybutton.titleLabel?.minimumScaleFactor = 0.5 mybutton.titleLabel?.numberOfLines = …

Read more

How to change the highlighted color of a UIButton? [duplicate]

Try to Override the UIButton with the following Method.. and just change the backgroud color of button when its in highlighted state. – (void)setHighlighted:(BOOL)highlighted { [super setHighlighted:highlighted]; if (highlighted) { self.backgroundColor = [UIColor Your Customcolor]; } else{ self.backgroundColor = [UIColor Your DefaultColor]; } } Try it..hope it helps

UIButton that resizes to fit its titleLabel

Swift 4.x version of Kubba’s answer: Need to Update Line Break as Clip/WordWrap/ in Interface builder to corresponding buttons. class ResizableButton: UIButton { override var intrinsicContentSize: CGSize { let labelSize = titleLabel?.sizeThatFits(CGSize(width: frame.width, height: .greatestFiniteMagnitude)) ?? .zero let desiredButtonSize = CGSize(width: labelSize.width + titleEdgeInsets.left + titleEdgeInsets.right, height: labelSize.height + titleEdgeInsets.top + titleEdgeInsets.bottom) return desiredButtonSize } …

Read more