IOS lets UIButton automatically calculate width and height based on text content

Source: Internet
Author: User

Xcode's own UIButton control is no way to calculate its own width and height according to the text content, the following demonstrates the problem,

I've created a UIButton in code, and I've set some properties, and here's a look at

Everything is so beautiful, as we want to Ah, BTN also follow the text content of their own wide-height changes Ah! Then I'll set a little more text.

With an excited mood, look again

Wow, how did it turn out that way. The text content is beyond the btn control, not the same as we imagined. How should the problem be solved?

Since the UIButton class from Xcode doesn't meet our needs. Then we can customize a UIButton class, just inherit UIButton.

I override the Init method in. m files

In fact, when the instance is instantiated, a uilable control is added to the button inside, and there is a constraint between the uilable control and the button. First you should understand that the system comes with a button control with two child controls, Uiimageview and Uilable,

Since the constraints between the system's uilable and the parent control UIButton do not meet our current requirements, I recreate a uilable, set the constraints myself, and display the text information on the uilable I created without the uilable of the system. The following override method

When I rewrite these three methods, I'll give btn the properties of the text, then call the above method, then, I rewrite is to put the text message is set in the UIButton bring the uilable above, I rewrite the text message will be set in my own creation of the lable above, lable will be based on the text content to change their size, because the lable and the button is also constrained, so the button will also follow the change.

Here's a look at the effect

It's the same as the effect we want.

Summary: Because the UIButton control has two child controls: Uiimageview (used to display the picture), uilable (used to display the text), but the constraints between the system-defined uilable and UIButton do not meet our current requirements, so I define a class myself, Inherit UIButton, at the time of initialization, add a custom uilable control to UIButton, and set the constraint between this uilable control and the button to the current requirement. When I set the text to the button again, I set it to the custom uilable control (that is, the overridden method), this time the custom uilable Gray automatically calculates its own width according to the content of the text, because there is a constraint between the uilable and the button, The width of the button itself will also change. So the button itself comes with a uilable that we don't use,

IOS lets UIButton automatically calculate width and height based on text content

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.