BTUIKCardListLabel.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #import "BTUIKCardListLabel.h"
  2. #import "BTUIKPaymentOptionCardView.h"
  3. #import "BTUIKViewUtil.h"
  4. #import "BTUIKAppearance.h"
  5. #import <QuartzCore/QuartzCore.h>
  6. @interface BTUIKCardListLabel ()
  7. @property (nonatomic, strong) NSArray *availablePaymentOptionAttachments;
  8. @property (nonatomic) BTUIKPaymentOptionType emphasisedPaymentOption;
  9. @end
  10. @implementation BTUIKCardListLabel
  11. - (instancetype)initWithFrame:(CGRect)frame {
  12. self = [super initWithFrame:frame];
  13. if (self) {
  14. self.numberOfLines = 0;
  15. self.textAlignment = NSTextAlignmentCenter;
  16. self.emphasisedPaymentOption = BTUIKPaymentOptionTypeUnknown;
  17. self.availablePaymentOptionAttachments = @[];
  18. self.availablePaymentOptions = @[];
  19. }
  20. return self;
  21. }
  22. - (UIImage *) imageWithView:(UIView *)view {
  23. UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
  24. [view.layer renderInContext:UIGraphicsGetCurrentContext()];
  25. UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
  26. UIGraphicsEndImageContext();
  27. return img;
  28. }
  29. - (void)setAvailablePaymentOptions:(NSArray *)availablePaymentOptions {
  30. _availablePaymentOptions = [NSOrderedSet orderedSetWithArray:availablePaymentOptions].array;
  31. if ([BTUIKViewUtil isLanguageLayoutDirectionRightToLeft]) {
  32. _availablePaymentOptions = [[_availablePaymentOptions reverseObjectEnumerator] allObjects];
  33. }
  34. [self updateAppearance];
  35. [self emphasizePaymentOption:self.emphasisedPaymentOption];
  36. }
  37. - (void)updateAppearance {
  38. NSMutableAttributedString *at = [[NSMutableAttributedString alloc] initWithString:@""];
  39. NSMutableArray *attachments = [NSMutableArray new];
  40. BTUIKPaymentOptionCardView *hint = [BTUIKPaymentOptionCardView new];
  41. hint.frame = CGRectMake(0, 0, [BTUIKAppearance smallIconWidth], [BTUIKAppearance smallIconHeight]);
  42. for (NSUInteger i = 0; i < self.availablePaymentOptions.count; i++) {
  43. NSTextAttachment *composeAttachment = [NSTextAttachment new];
  44. BTUIKPaymentOptionType paymentOption = ((NSNumber*)self.availablePaymentOptions[i]).intValue;
  45. hint.paymentOptionType = paymentOption;
  46. [hint setNeedsLayout];
  47. [hint layoutIfNeeded];
  48. UIImage *composeImage = [self imageWithView:hint];
  49. composeImage.accessibilityLabel = [BTUIKViewUtil nameForPaymentMethodType:paymentOption];
  50. [attachments addObject:composeAttachment];
  51. composeAttachment.image = composeImage;
  52. [at appendAttributedString:[NSAttributedString attributedStringWithAttachment:composeAttachment]];
  53. [at appendAttributedString:[[NSMutableAttributedString alloc]
  54. initWithString: i < self.availablePaymentOptions.count - 1? @" " : @""]];
  55. }
  56. self.attributedText = at;
  57. self.availablePaymentOptionAttachments = attachments;
  58. }
  59. - (void)emphasizePaymentOption:(BTUIKPaymentOptionType)paymentOption {
  60. if (paymentOption == self.emphasisedPaymentOption) {
  61. return;
  62. }
  63. [self updateAppearance];
  64. for (NSUInteger i = 0; i < self.availablePaymentOptions.count; i++) {
  65. BTUIKPaymentOptionType option = ((NSNumber*)self.availablePaymentOptions[i]).intValue;
  66. float newAlpha = (paymentOption == option || paymentOption == BTUIKPaymentOptionTypeUnknown) ? 1.0 : 0.25;
  67. NSTextAttachment *attachment = self.availablePaymentOptionAttachments[i];
  68. UIGraphicsBeginImageContextWithOptions(attachment.image.size, NO, attachment.image.scale);
  69. [attachment.image drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:newAlpha];
  70. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  71. UIGraphicsEndImageContext();
  72. attachment.image = image;
  73. // Update accessibility label for highlighted cards
  74. if (paymentOption == option) {
  75. image.accessibilityLabel = [BTUIKViewUtil nameForPaymentMethodType:paymentOption];
  76. } else if (paymentOption == BTUIKPaymentOptionTypeUnknown) {
  77. image.accessibilityLabel = [BTUIKViewUtil nameForPaymentMethodType:((NSNumber*)self.availablePaymentOptions[i]).intValue];
  78. }
  79. }
  80. self.emphasisedPaymentOption = paymentOption;
  81. [self setNeedsDisplay];
  82. }
  83. @end