123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- //
- // UIBarButtonItem+RQExtension.m
- // RQCommon
- //
- // Created by 张嵘 on 2018/11/16.
- // Copyright © 2018 张嵘. All rights reserved.
- //
- #import "UIBarButtonItem+RQExtension.h"
- @implementation UIBarButtonItem (RQExtension)
- + (UIBarButtonItem *)rq_systemItemWithTitle:(NSString *)title
- titleColor:(UIColor *)titleColor
- imageName:(NSString *)imageName
- target:(id)target
- selector:(SEL)selector
- textType:(BOOL)textType {
- UIBarButtonItem *item = textType ?
- ({
- /// 设置普通状态的文字属性
- item = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:target action:selector];
- titleColor = titleColor?titleColor:[UIColor whiteColor];
- NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
- textAttrs[NSForegroundColorAttributeName] = titleColor;
- textAttrs[NSFontAttributeName] = RQRegularFont(16.0f);
- NSShadow *shadow = [[NSShadow alloc] init];
- shadow.shadowOffset = CGSizeZero;
- textAttrs[NSShadowAttributeName] = shadow;
- [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
-
- // 设置高亮状态的文字属性
- NSMutableDictionary *highTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs];
- highTextAttrs[NSForegroundColorAttributeName] = [titleColor colorWithAlphaComponent:.5f];
- [item setTitleTextAttributes:highTextAttrs forState:UIControlStateHighlighted];
-
- // 设置不可用状态(disable)的文字属性
- NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs];
- disableTextAttrs[NSForegroundColorAttributeName] = [titleColor colorWithAlphaComponent:.5f];
- [item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
-
- item;
- }) : ({
- item = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:target action:selector];
- item;
- });
- return item;
- }
- + (UIBarButtonItem *)rq_customItemWithTitle:(NSString *)title
- font:(UIFont *)font
- titleColor:(UIColor *)titleColor
- imageName:(NSString *)imageName
- target:(id)target
- selector:(SEL)selector
- contentHorizontalAlignment:(UIControlContentHorizontalAlignment)contentHorizontalAlignment
- EdgeInsetsStyle:(RQButtonEdgeInsetsStyle)edgeInsetsStyle
- space:(CGFloat)space {
- UIButton *item = [[UIButton alloc] init];
- titleColor = titleColor?titleColor:[UIColor whiteColor];
- [item setTitle:title forState:UIControlStateNormal];
- [item.titleLabel setFont:font];
- [item setTitleColor:titleColor forState:UIControlStateNormal];
- [item setTitleColor:[titleColor colorWithAlphaComponent:.5f] forState:UIControlStateHighlighted];
- [item setTitleColor:[titleColor colorWithAlphaComponent:.5f] forState:UIControlStateDisabled];
- [item sizeToFit];
- item.height = 44;
- if (item.width < 40) {
- item.width = 48;
- }
- item.contentHorizontalAlignment = contentHorizontalAlignment;
- [item addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
- @weakify(item)
- BOOL isURL = [imageName hasPrefix:@"http://"] || [imageName hasPrefix:@"https://"];
- if (isURL) {
- [item yy_setImageWithURL:[NSURL URLWithString:imageName] forState:UIControlStateNormal placeholder:RQWebAvatarImagePlaceholder() options:RQWebImageOptionAutomatic completion:^(UIImage * _Nullable image, NSURL * _Nonnull url, YYWebImageFromType from, YYWebImageStage stage, NSError * _Nullable error) {
- @strongify(item)
- if(image) {
- UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
- image = [image scaledToSize:CGSizeMake(30, 30)];
- image = [image qmui_imageWithClippedCornerRadius:15];
- imageView.image = image;
-
- [item setImage:imageView.image forState:UIControlStateNormal];
- [item layoutButtonWithEdgeInsetsStyle:edgeInsetsStyle imageTitleSpace:space];
- }
- }];
- } else {
- [item setImage:[UIImage rq_imageAlwaysShowOriginalImageWithImageName:imageName] forState:UIControlStateNormal];
- [item layoutButtonWithEdgeInsetsStyle:edgeInsetsStyle imageTitleSpace:space];
- }
-
-
- return [[UIBarButtonItem alloc] initWithCustomView:item];
- }
- /// 返回按钮 带箭头的
- + (UIBarButtonItem *)rq_backItemWithTitle:(NSString *)title
- imageName:(NSString *)imageName
- target:(id)target
- action:(SEL)action
- {
- return [self rq_customItemWithTitle:title font:RQRegularFont_13 titleColor:nil imageName:imageName target:target selector:action contentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft EdgeInsetsStyle:RQButtonEdgeInsetsStyleLeft space:0];
- }
- @end
|