//
//  customButton.m
//  项目-iWeiBo
//
//  Created by student on 14-9-16.
//  Copyright (c) 2014年 student. All rights reserved.
//

#import "CustomButton.h"
#define imageToTextRatio 0.3



@implementation CustomButton

- (instancetype)init
{
    if (self = [super init]) {
        self.titleLabel.textColor = [UIColor blackColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return self;
}

- (id)initCustomBtnWithImageDirection:(ImagePostion)imagePosition imageViewSize:(CGSize)imageSize imageLabelMargin:(CGFloat)imageLabelMargin titleLabelFont:(UIFont *)titleLabelFont imageIcon:(NSString *)imageIcon selectIcon:(NSString *)selectIcon title:(NSString *)title btnWidth:(CGFloat)btnWidth
{
    
    
    if (self = [super init]) {

        self.titleLabel.numberOfLines = 0;
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
//        self.imageView.backgroundColor = [UIColor redColor];
        self.layoutType = LayoutTypeMargin;
        
        self.imagePostion = imagePosition;
        
        self.imageSize = imageSize;
        
        self.titleLabelFont = titleLabelFont;
        
        self.imageLabelMargin = imageLabelMargin;
    
        [self setImage:[UIImage imageNamed:imageIcon] forState:UIControlStateNormal];
        if (selectIcon) {
            [self setImage:[UIImage imageNamed:selectIcon] forState:UIControlStateSelected];
        }
//        [self setBackgroundImage:[UIImage imageNamed:imageIcon] forState:UIControlStateNormal];
        [self setTitle:title forState:UIControlStateNormal];
   
        [self setFont:titleLabelFont];
        
        //top 方向的有用.
        if (imagePosition == ImagePostionTop) {
            self.customBtnHeight = self.imageSize.height + self.imageLabelMargin + [title stringgetSizebyMaxWidth:btnWidth Font:titleLabelFont].height;
        }else if (imagePosition == ImagePostionLeft)
        {
            self.customBtnHeight = imageSize.height;
        }
        
        
    }
    
    return self;
}

- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state
{
    [super setTitle:title forState:state];
    //存文本的..
    if(self.imagePostion == ImagePostionNoImage){
        CGSize newSize = [self labelAutoCalculateRectWith:title FontSize:self.titleLabel.font.pointSize MaxSize:CGSizeMake(100, 20)];
        self.bounds = CGRectMake(0, 0, newSize.width , newSize.height);
    }else if (self.imagePostion == ImagePostionLeft)
    {
        CGSize newSize = [self labelAutoCalculateRectWith:title FontSize:self.titleLabel.font.pointSize MaxSize:CGSizeMake(SCREEN_WIDTH/2.0f, 40)];
        self.bounds = CGRectMake(0, 0, newSize.width + 2 * newSize.height , newSize.height);
    }
    
}


- (CGSize)labelAutoCalculateRectWith:(NSString*)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize

{
    
    NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    
    paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
    
    NSDictionary* attributes =@{NSFontAttributeName:[UIFont systemFontOfSize:fontSize],NSParagraphStyleAttributeName:paragraphStyle.copy};
    
    CGSize labelSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
    
    
    
    
    labelSize.height=ceil(labelSize.height);
    
    labelSize.width=ceil(labelSize.width);
    
    return labelSize;
    
}



//- (id)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//        // Initialization code
//        self.imageView.contentMode = UIViewContentModeScaleAspectFit;
//        self.titleLabel.textAlignment = NSTextAlignmentCenter;
//        self.titleLabel.font = TabbarButtonTextFont;
//        
//        [self setTitleColor: [UIColor colorWithRed:248/255.0 green:139/255.0 blue:0/255.0 alpha:1] forState:UIControlStateSelected];
//        
//        //添加unreadbutton
//    }
//    return self;
//}
//- (void)setHighlighted:(BOOL)highlighted
//{
//    
//}

- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    if (self.layoutType == LayoutTypeMargin) {
        
        if (self.imagePostion == ImagePostionTop){
        
            CGFloat imageX = self.frame.size.width/2.0f - self.imageSize.width/2.0f;
            CGFloat imageY = 0;
            
            return CGRectMake(imageX, imageY, self.imageSize.width, self.imageSize.height);
        }else if (self.imagePostion == ImagePostionLeft){
            
            CGFloat imageX = 0;
            CGFloat imageY = (self.frame.size.height-self.imageSize.height)/2.0f;
            
            return CGRectMake(imageX, imageY, self.imageSize.width, self.imageSize.height);
        }
        else
        
        return  [super imageRectForContentRect:contentRect];
        
    }else{
    
        if (self.imagePostion == ImagePostionLeft) {
            CGFloat imageW = self.frame.size.width  * imageToTextRatio;
            
            return CGRectMake(0, self.frame.size.height/2.0f -  imageW/2.0f, imageW, imageW);
        }else if (self.imagePostion == ImagePostionTop)
        {
            CGFloat imageX = self.frame.size.width/2.0f - self.imageSize.width/2.0f;
            CGFloat imageY = 0;

            CGFloat imageW = self.frame.size.width;
            CGFloat imageH = self.frame.size.height * (1 - (self.imageToTextRatioFloat?self.imageToTextRatioFloat:imageToTextRatio));
            
            return CGRectMake(imageX, imageY, imageW, imageH);
        }else if (self.imagePostion == ImagePostionCustom)
        {
            
            CGFloat imageX = self.imageFrame.origin.x;
            CGFloat imageY = self.frame.size.height/2.0f - self.imageFrame.size.height/2.0f;
            CGFloat imageW = self.imageFrame.size.width;
            CGFloat imageH = self.imageFrame.size.height;
            return CGRectMake(imageX, imageY, imageW, imageH);
        }
        else return [super imageRectForContentRect:contentRect];
    }
    
   
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    
    if (self.layoutType == LayoutTypeMargin) {
        
        if (self.imagePostion == ImagePostionTop) {
            CGFloat titleLabelX = 0;
            CGFloat titleLabelY = self.imageSize.height + self.imageLabelMargin;
            CGSize titleLabelSize = [self.titleLabel.text stringgetSizebySize:CGSizeMake(self.frame.size.width, self.frame.size.height - self.imageSize.height - self.imageLabelMargin) Font:self.titleLabelFont];
            return CGRectMake(titleLabelX, titleLabelY, self.frame.size.width, titleLabelSize.height? titleLabelSize.height:self.frame.size.height - self.imageSize.height - self.imageLabelMargin);//底部间距.
        }else if (self.imagePostion == ImagePostionLeft) {
            CGFloat titleLabelX = self.imageLabelMargin + self.imageSize.width;
            CGFloat titleLabelY = 0;
            CGSize titleLabelSize = [self.titleLabel.text stringgetSizebyMaxWidth:self.frame.size.width - titleLabelX Font:self.titleLabelFont];
            return CGRectMake(titleLabelX, titleLabelY, titleLabelSize.width, self.frame.size.height);
        }else return  [super titleRectForContentRect:contentRect];
    }else{
    
        if (self.imagePostion == ImagePostionLeft) {
            CGFloat margin  = 5;
            CGFloat titleX = self.frame.size.width * imageToTextRatio + margin  ;
            
            CGFloat titleW = self.frame.size.width * (1-imageToTextRatio) - margin  ;
            
            return CGRectMake(titleX, 0, titleW, self.frame.size.height);
        }else if (self.imagePostion == ImagePostionTop)
        {
            CGFloat titleY  = self.frame.size.height * (1 - (self.imageToTextRatioFloat?self.imageToTextRatioFloat:imageToTextRatio));
            CGFloat titleW = self.frame.size.width;
            CGFloat titleH = self.frame.size.height * (self.imageToTextRatioFloat?self.imageToTextRatioFloat:imageToTextRatio);
            return CGRectMake(0, titleY , titleW, titleH);
        }else if (self.imagePostion == ImagePostionCustom)
        {
            CGFloat titleX = CGRectGetMaxX(self.imageFrame);
            CGFloat titleY = 0;
            CGFloat titleW = self.frame.size.width - titleX;
            CGFloat titleH = self.frame.size.height;
            return CGRectMake(titleX, titleY , titleW, titleH);
        }
        else {
            
            CGRect rect = contentRect;
            rect.size.width = self.bounds.size.width;
            contentRect = rect;
            
            return  [super titleRectForContentRect:contentRect];
        }

    }
   
}

- (void)setImageFrame:(CGRect)imageFrame
{
    _imageFrame = imageFrame;
//    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    if (self.isClickCycle) {
        self.imageView.layer.masksToBounds = YES;
        self.imageView.layer.cornerRadius = self.imageView.frame.size.height/2.0f;
    }
    
   
    
//    self.titleLabel.backgroundColor =[UIColor redColor];
    
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
