UIBarButtonItem+RQExtension.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // UIBarButtonItem+RQExtension.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/16.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "UIBarButtonItem+RQExtension.h"
  9. @implementation UIBarButtonItem (RQExtension)
  10. + (UIBarButtonItem *)rq_systemItemWithTitle:(NSString *)title
  11. titleColor:(UIColor *)titleColor
  12. imageName:(NSString *)imageName
  13. target:(id)target
  14. selector:(SEL)selector
  15. textType:(BOOL)textType {
  16. UIBarButtonItem *item = textType ?
  17. ({
  18. /// 设置普通状态的文字属性
  19. item = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:target action:selector];
  20. titleColor = titleColor?titleColor:[UIColor whiteColor];
  21. NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
  22. textAttrs[NSForegroundColorAttributeName] = titleColor;
  23. textAttrs[NSFontAttributeName] = RQRegularFont(16.0f);
  24. NSShadow *shadow = [[NSShadow alloc] init];
  25. shadow.shadowOffset = CGSizeZero;
  26. textAttrs[NSShadowAttributeName] = shadow;
  27. [item setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
  28. // 设置高亮状态的文字属性
  29. NSMutableDictionary *highTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs];
  30. highTextAttrs[NSForegroundColorAttributeName] = [titleColor colorWithAlphaComponent:.5f];
  31. [item setTitleTextAttributes:highTextAttrs forState:UIControlStateHighlighted];
  32. // 设置不可用状态(disable)的文字属性
  33. NSMutableDictionary *disableTextAttrs = [NSMutableDictionary dictionaryWithDictionary:textAttrs];
  34. disableTextAttrs[NSForegroundColorAttributeName] = [titleColor colorWithAlphaComponent:.5f];
  35. [item setTitleTextAttributes:disableTextAttrs forState:UIControlStateDisabled];
  36. item;
  37. }) : ({
  38. item = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:target action:selector];
  39. item;
  40. });
  41. return item;
  42. }
  43. + (UIBarButtonItem *)rq_customItemWithTitle:(NSString *)title
  44. font:(UIFont *)font
  45. titleColor:(UIColor *)titleColor
  46. imageName:(NSString *)imageName
  47. target:(id)target
  48. selector:(SEL)selector
  49. contentHorizontalAlignment:(UIControlContentHorizontalAlignment)contentHorizontalAlignment
  50. EdgeInsetsStyle:(RQButtonEdgeInsetsStyle)edgeInsetsStyle
  51. space:(CGFloat)space {
  52. UIButton *item = [[UIButton alloc] init];
  53. titleColor = titleColor?titleColor:[UIColor whiteColor];
  54. [item setTitle:title forState:UIControlStateNormal];
  55. [item.titleLabel setFont:font];
  56. [item setTitleColor:titleColor forState:UIControlStateNormal];
  57. [item setTitleColor:[titleColor colorWithAlphaComponent:.5f] forState:UIControlStateHighlighted];
  58. [item setTitleColor:[titleColor colorWithAlphaComponent:.5f] forState:UIControlStateDisabled];
  59. [item sizeToFit];
  60. item.height = 44;
  61. if (item.width < 40) {
  62. item.width = 48;
  63. }
  64. item.contentHorizontalAlignment = contentHorizontalAlignment;
  65. [item addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
  66. @weakify(item)
  67. BOOL isURL = [imageName hasPrefix:@"http://"] || [imageName hasPrefix:@"https://"];
  68. if (isURL) {
  69. [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) {
  70. @strongify(item)
  71. if(image) {
  72. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
  73. image = [image scaledToSize:CGSizeMake(30, 30)];
  74. image = [image qmui_imageWithClippedCornerRadius:15];
  75. imageView.image = image;
  76. [item setImage:imageView.image forState:UIControlStateNormal];
  77. [item layoutButtonWithEdgeInsetsStyle:edgeInsetsStyle imageTitleSpace:space];
  78. }
  79. }];
  80. } else {
  81. [item setImage:[UIImage rq_imageAlwaysShowOriginalImageWithImageName:imageName] forState:UIControlStateNormal];
  82. [item layoutButtonWithEdgeInsetsStyle:edgeInsetsStyle imageTitleSpace:space];
  83. }
  84. return [[UIBarButtonItem alloc] initWithCustomView:item];
  85. }
  86. /// 返回按钮 带箭头的
  87. + (UIBarButtonItem *)rq_backItemWithTitle:(NSString *)title
  88. imageName:(NSString *)imageName
  89. target:(id)target
  90. action:(SEL)action
  91. {
  92. return [self rq_customItemWithTitle:title font:RQRegularFont_13 titleColor:nil imageName:imageName target:target selector:action contentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft EdgeInsetsStyle:RQButtonEdgeInsetsStyleLeft space:0];
  93. }
  94. @end