| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- #import "BTUIKCardListLabel.h"
- #import "BTUIKPaymentOptionCardView.h"
- #import "BTUIKViewUtil.h"
- #import "BTUIKAppearance.h"
- #import <QuartzCore/QuartzCore.h>
- @interface BTUIKCardListLabel ()
- @property (nonatomic, strong) NSArray *availablePaymentOptionAttachments;
- @property (nonatomic) BTUIKPaymentOptionType emphasisedPaymentOption;
- @end
- @implementation BTUIKCardListLabel
- - (instancetype)initWithFrame:(CGRect)frame {
- self = [super initWithFrame:frame];
- if (self) {
- self.numberOfLines = 0;
- self.textAlignment = NSTextAlignmentCenter;
- self.emphasisedPaymentOption = BTUIKPaymentOptionTypeUnknown;
- self.availablePaymentOptionAttachments = @[];
- self.availablePaymentOptions = @[];
- }
- return self;
- }
- - (UIImage *) imageWithView:(UIView *)view {
- UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
- [view.layer renderInContext:UIGraphicsGetCurrentContext()];
- UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return img;
- }
- - (void)setAvailablePaymentOptions:(NSArray *)availablePaymentOptions {
- _availablePaymentOptions = [NSOrderedSet orderedSetWithArray:availablePaymentOptions].array;
- if ([BTUIKViewUtil isLanguageLayoutDirectionRightToLeft]) {
- _availablePaymentOptions = [[_availablePaymentOptions reverseObjectEnumerator] allObjects];
- }
- [self updateAppearance];
- [self emphasizePaymentOption:self.emphasisedPaymentOption];
- }
- - (void)updateAppearance {
- NSMutableAttributedString *at = [[NSMutableAttributedString alloc] initWithString:@""];
- NSMutableArray *attachments = [NSMutableArray new];
- BTUIKPaymentOptionCardView *hint = [BTUIKPaymentOptionCardView new];
- hint.frame = CGRectMake(0, 0, [BTUIKAppearance smallIconWidth], [BTUIKAppearance smallIconHeight]);
- for (NSUInteger i = 0; i < self.availablePaymentOptions.count; i++) {
- NSTextAttachment *composeAttachment = [NSTextAttachment new];
- BTUIKPaymentOptionType paymentOption = ((NSNumber*)self.availablePaymentOptions[i]).intValue;
- hint.paymentOptionType = paymentOption;
- [hint setNeedsLayout];
- [hint layoutIfNeeded];
- UIImage *composeImage = [self imageWithView:hint];
- composeImage.accessibilityLabel = [BTUIKViewUtil nameForPaymentMethodType:paymentOption];
- [attachments addObject:composeAttachment];
- composeAttachment.image = composeImage;
- [at appendAttributedString:[NSAttributedString attributedStringWithAttachment:composeAttachment]];
- [at appendAttributedString:[[NSMutableAttributedString alloc]
- initWithString: i < self.availablePaymentOptions.count - 1? @" " : @""]];
- }
- self.attributedText = at;
- self.availablePaymentOptionAttachments = attachments;
- }
- - (void)emphasizePaymentOption:(BTUIKPaymentOptionType)paymentOption {
- if (paymentOption == self.emphasisedPaymentOption) {
- return;
- }
- [self updateAppearance];
- for (NSUInteger i = 0; i < self.availablePaymentOptions.count; i++) {
- BTUIKPaymentOptionType option = ((NSNumber*)self.availablePaymentOptions[i]).intValue;
- float newAlpha = (paymentOption == option || paymentOption == BTUIKPaymentOptionTypeUnknown) ? 1.0 : 0.25;
- NSTextAttachment *attachment = self.availablePaymentOptionAttachments[i];
- UIGraphicsBeginImageContextWithOptions(attachment.image.size, NO, attachment.image.scale);
- [attachment.image drawAtPoint:CGPointZero blendMode:kCGBlendModeNormal alpha:newAlpha];
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- attachment.image = image;
- // Update accessibility label for highlighted cards
- if (paymentOption == option) {
- image.accessibilityLabel = [BTUIKViewUtil nameForPaymentMethodType:paymentOption];
- } else if (paymentOption == BTUIKPaymentOptionTypeUnknown) {
- image.accessibilityLabel = [BTUIKViewUtil nameForPaymentMethodType:((NSNumber*)self.availablePaymentOptions[i]).intValue];
- }
- }
- self.emphasisedPaymentOption = paymentOption;
- [self setNeedsDisplay];
- }
- @end
|