UIAlertController+RQExtension.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // UIAlertController+RQExtension.m
  3. // RQCommon
  4. //
  5. // Created by 张嵘 on 2018/11/23.
  6. // Copyright © 2018 张嵘. All rights reserved.
  7. //
  8. #import "UIAlertController+RQExtension.h"
  9. #import <objc/runtime.h>
  10. @implementation UIAlertController (RQExtension)
  11. - (BOOL)shouldAutorotate {
  12. return NO;
  13. }
  14. - (NSUInteger)supportedInterfaceOrientations {
  15. return UIInterfaceOrientationMaskPortrait;
  16. }
  17. - (void)viewWillLayoutSubviews {
  18. [super viewWillLayoutSubviews];
  19. //按钮统一颜色
  20. if (self.tintColor) {
  21. for (UIAlertAction *action in self.actions) {
  22. if (!action.textColor || action.style != UIAlertActionStyleDestructive) {
  23. action.textColor = self.tintColor;
  24. }
  25. }
  26. }
  27. }
  28. - (UIColor *)tintColor {
  29. return objc_getAssociatedObject(self, @selector(tintColor));
  30. }
  31. - (void)setTintColor:(UIColor *)tintColor {
  32. objc_setAssociatedObject(self, @selector(tintColor), tintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  33. }
  34. - (UIColor *)titleColor {
  35. return objc_getAssociatedObject(self, @selector(titleColor));
  36. }
  37. - (void)setTitleColor:(UIColor *)titleColor {
  38. unsigned int count = 0;
  39. Ivar *ivars = class_copyIvarList([UIAlertController class], &count);
  40. for(int i = 0;i < count;i ++){
  41. Ivar ivar = ivars[i];
  42. NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
  43. //标题颜色
  44. if ([ivarName isEqualToString:@"_attributedTitle"] && self.title && titleColor) {
  45. NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:self.title attributes:@{NSForegroundColorAttributeName:titleColor,NSFontAttributeName:[UIFont systemFontOfSize:16.0]}];
  46. [self setValue:attr forKey:@"attributedTitle"];
  47. }
  48. }
  49. free(ivars);
  50. objc_setAssociatedObject(self, @selector(titleColor), titleColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  51. }
  52. - (UIColor *)messageColor {
  53. return objc_getAssociatedObject(self, @selector(messageColor));
  54. }
  55. - (void)setMessageColor:(UIColor *)messageColor {
  56. unsigned int count = 0;
  57. Ivar *ivars = class_copyIvarList([UIAlertController class], &count);
  58. for(int i = 0;i < count;i ++){
  59. Ivar ivar = ivars[i];
  60. NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
  61. //描述颜色
  62. if ([ivarName isEqualToString:@"_attributedMessage"] && self.message && messageColor) {
  63. NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:self.message attributes:@{NSForegroundColorAttributeName:messageColor,NSFontAttributeName:[UIFont systemFontOfSize:12.0]}];
  64. [self setValue:attr forKey:@"attributedMessage"];
  65. }
  66. }
  67. free(ivars);
  68. objc_setAssociatedObject(self, @selector(messageColor), messageColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  69. }
  70. @end
  71. @implementation UIAlertAction (RQExtension)
  72. - (UIColor *)textColor {
  73. return objc_getAssociatedObject(self, @selector(textColor));
  74. }
  75. //按钮标题的字体颜色
  76. - (void)setTextColor:(UIColor *)textColor {
  77. if (self.style == UIAlertActionStyleDestructive) {
  78. return;
  79. }
  80. unsigned int count = 0;
  81. Ivar *ivars = class_copyIvarList([UIAlertAction class], &count);
  82. for(int i =0; i<count; i++){
  83. Ivar ivar = ivars[i];
  84. NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSUTF8StringEncoding];
  85. if ([ivarName isEqualToString:@"_titleTextColor"]) {
  86. [self setValue:textColor forKey:@"titleTextColor"];
  87. }
  88. }
  89. free(ivars);
  90. objc_setAssociatedObject(self, @selector(textColor), textColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  91. }
  92. @end