美文网首页
简单几行代码实现UIButton 上图下文

简单几行代码实现UIButton 上图下文

作者: CocoaJason | 来源:发表于2017-03-24 10:16 被阅读70次
    #define ScreenHeight  [UIScreen mainScreen].bounds.size.height
    #define ScreenWidth  [UIScreen mainScreen].bounds.size.width
    
    #define HeightIn667(Height) (Height * ScreenHeight / 667)
    #define WidthIn375(Width) (Width * ScreenWidth / 375)
    
    @interface CustomBtn : UIButton
    
    -(void)setTitle:(NSString *)title
              image:(UIImage *)image;
    
    @end
    
    - (CGRect)titleRectForContentRect:(CGRect)contentRect
    {
        CGRect titleRect = CGRectMake(0, CGRectGetHeight(self.bounds)/3 * 2, CGRectGetWidth(self.bounds),CGRectGetHeight(self.bounds)/3);
        return titleRect;
    }
    
    - (CGRect)imageRectForContentRect:(CGRect)contentRect
    {
        CGFloat width = CGRectGetWidth(self.bounds);
        CGRect imageRect = CGRectMake(0, 0, width, CGRectGetHeight(self.bounds)/3 * 2);
        
        return imageRect;
    }
    
    -(void)setTitle:(NSString *)title
              image:(UIImage *)image;
    {
        [self setTitle:title forState:UIControlStateNormal];
        [self setImage:image forState:UIControlStateNormal];
    }
    
    CustomBtn *btn = [[CustomBtn alloc]initWithFrame:CGRectMake(20, 100, 80, 120)];
        [self.view addSubview:btn];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitle:@"测试一下" image:[UIImage imageNamed:@"placeholderHeadIcon"]];
    

    相关文章

      网友评论

          本文标题:简单几行代码实现UIButton 上图下文

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