UIButton that resizes to fit its titleLabel

前端 未结 12 1402
既然无缘
既然无缘 2020-12-01 09:13

I have a UIButton that I add to my view controller\'s view in a storyboard. I add centering constraints to position it and leading space constraints to limit it

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-01 09:49

    What I would suggest is to calculate the width of the text, and calculate the frame by yourself. It's not complicated anyway, get the width of the text first:

    [NSString sizeWithFont:font];
    

    Do a mod operation and you'll easily find out the number of lines for the text.

    Note this method is for pre iOS7, for iOS 7 and after you might want to try

    [NSString sizeWithAttributes:aDictionary];
    

提交回复
热议问题