//
//  UIBarButtonItem+Addition.m
//  weibo
//
//  Created by mj on 13-3-4.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "UIBarButtonItem+Addition.h"
#import "CustomButton.h"
/*

 UIButton *moreOperationBtn = [UIButton  buttonWithType:UIButtonTypeCustom];
 
 [moreOperationBtn addTarget:self action:@selector(moreOperate) forControlEvents:UIControlEventTouchUpInside];
 
 moreOperationBtn.frame = CGRectMake(0, 0, 22, 4);
 [moreOperationBtn setImage:[UIImage imageNamed:@"moreOperation"] forState:UIControlStateNormal];
 
 UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc]initWithCustomView:moreOperationBtn];
 */

@implementation UIBarButtonItem (Addition)
+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action icon:(NSString *)icon highlightedIcon:(NSString *)highlightedIcon title:(NSString *)title iconSize:(CGSize)iconSize fontSize:(CGFloat)fontSize titleColor:(UIColor *)titleColor{
    CustomButton *btn = [CustomButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    UIImage *bg =nil;
    UIImage *highlightedBg = nil;
    if (icon) {
        bg = [UIImage imageNamed:icon];
        highlightedBg = [UIImage imageNamed:highlightedIcon];
        [btn setImage:bg forState:UIControlStateNormal];
//        [btn setBackgroundImage:bg forState:UIControlStateNormal];
//        [btn setBackgroundImage:highlightedBg forState:UIControlStateHighlighted];
        
    }
    
    if (icon) {
        
        btn.frame = (CGRect){CGPointZero, iconSize};
    }else{
        if (!fontSize) {
            fontSize = 14;
        }
        [btn setFont:[UIFont systemFontOfSize:fontSize]];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:titleColor?titleColor:[UIColor blackColor] forState:UIControlStateNormal];

        
//        CGSize size = [title stringgetSizebyMaxWidth:100 Font:[UIFont systemFontOfSize:fontSize]];
        
        
//        CGSize size = [UIBarButtonItem labelAutoCalculateRectWith:title FontSize:fontSize MaxSize:CGSizeMake(100, 20)];
//        
//        btn.frame = CGRectMake(0, 0, 1.5*size.width,size.height);
        
    }
   
    return [[UIBarButtonItem alloc] initWithCustomView:btn];
}

+ (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) + 20;
    
    return labelSize;
    
}

@end
