Background image for UIButton

I have been using UIButton to create buttons, including Plain text buttons, setting background images, setting selected state image buttons... Especially when our provided image is smaller than the size of the button itself, the button will not be completely filled with the image, when When the image is larger than the button, the image will exceed the button's frame (unless button.layer.maskToBounds = YES is set to prevent it from exceeding the button's frame). In fact, UIButton has provided an interface for us to customize buttons freely, but many people have not noticed its existence. As long as we inherit the UIButton class, and then rewrite one or more of the following methods in its subclass, we can freely control the position of the image, label, etc. in the UIButton.

// these return the rectangle for the background (assumes bounds), the content (image + title) and for the image and title separately. the content rect is calculated based
// on the title and image size and padding and then adjusted based on the control content alignment. there are no draw methods since the contents
// are rendered in separate subviews (UIImageView, UILabel)

- (CGRect)backgroundRectForBounds:(CGRect)bounds;
- (CGRect)contentRectForBounds:(CGRect)bounds;
- (CGRect)titleRectForContentRect:(CGRect)contentRect;
- (CGRect)imageRectForContentRect:(CGRect)contentRect;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325179127&siteId=291194637