美文网首页
UIButton的自定义

UIButton的自定义

作者: 奶茶007 | 来源:发表于2017-03-08 11:11 被阅读117次

    UIButton默认显示图片和标题,如图:

    2E7371BD-9436-4835-8F05-D74148E9E97C.png

    此时,imageEdgeInsets(top,left,bottom,right) 和 titleEdgeInsets(top,left,bottom,right)默认是UIEdgeInsetsZero

    可以进行2种方式的自定义:

    • 1 ,图片在左标题在右
    CGSize  buttonTitleSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
    CGSize  buttonImageSize = [self imageForState:UIControlStateSelected].size;
    self.imageEdgeInsets = UIEdgeInsetsMake(0, buttonTitleSize.width+self.border,0, -buttonTitleSize.width-self.border);
    self.titleEdgeInsets = UIEdgeInsetsMake(0, -buttonImageSize.width,0, buttonImageSize.width);
    //self.border为自定义的图片和标题的间隔
    
    45016748-9DFD-47E9-8B02-8342026A8378.png

    2,图片在上标题在下

    self.imageEdgeInsets=UIEdgeInsetsMake(-buttonTitleSize.height/2,buttonTitleSize.width/2, buttonTitleSize.height/2, -buttonTitleSize.width/2);
    self.titleEdgeInsets=UIEdgeInsetsMake(buttonImageSize.height/2+self.border, -buttonImageSize.width/2, -buttonImageSize.height/2-self.border, buttonImageSize.width/2);
    
    FDCD377B-5E25-442E-8FFA-17AE3C2D25A7.png

    demo地址 https://github.com/liuyn007/CustomerButton

    相关文章

      网友评论

          本文标题:UIButton的自定义

          本文链接:https://www.haomeiwen.com/subject/uwfbgttx.html