Coder Social home page Coder Social logo

ctbuttoninsets's Introduction

CTButtonInsets

Give up the property imageEdgeInsets and titleEdgeInsets when you want to set a UIButton's titltLabel and imageView . It is difficult and you will feel that you are really fucking a dog (Chinese slang) when you do this in this way because there will be some problems you cannot control .So just abandon it ,this category can sovle this problem perfectly.

How does it work

When I was looking for the solution about this issue,I noticed that there declare two methods in the UIButton.h

  • -(CGRect)titleRectForContentRect:(CGRect)contentRect;
  • -(CGRect)imageRectForContentRect:(CGRect)contentRect;

The two methods description goes :

Returns the rectangle in which the receiver draws its title.

Returns the rectangle in which the receiver draws its image.

Then I add two properties titleRect and imageRect, and rewrite the two methods in the category ,it do work.

But there still have a problem.You must have many buttons in your project, those two methods you rewrite has covered the system methods. So if the other buttons do not set the two properties you added in the category,it will still call those two methods you rewrite.These button's titleLabel and imageView will show noting because its RECT is empty.

So I use method swizzling to solve this problem.To distinguish wether I set the titleRect and imageRect,if I set ,return it;if I don't ,call the system methods.

- (CGRect)swizzle_imageRectForContent:(CGRect)contentRect{
    if (!CGRectEqualToRect(self.imageRect, CGRectZero)) {
        return self.imageRect;
    }
    
    return [self swizzle_imageRectForContent:contentRect];
}

- (CGRect)swizzle_titleRectForContent:(CGRect)contentRect{
    if (!CGRectEqualToRect(self.titleRect, CGRectZero)) {
        return self.titleRect;
    }
    
    return [self swizzle_titleRectForContent:contentRect];
}

See more details ,download and star please.

#^_^#

ctbuttoninsets's People

Contributors

greatwallt avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.