1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/autofill/form_suggestion_label.h"
#include <cmath>
#import <QuartzCore/QuartzCore.h>
#include "base/strings/sys_string_conversions.h"
#include "components/autofill/core/browser/credit_card.h"
#import "components/autofill/ios/browser/form_suggestion.h"
#import "ios/chrome/browser/autofill/form_suggestion_view_client.h"
#import "ios/chrome/browser/ui/uikit_ui_util.h"
#include "ios/chrome/browser/ui/ui_util.h"
namespace {
// The button corner radius.
const CGFloat kCornerRadius = 2.0f;
// Font size of button titles.
const CGFloat kIpadFontSize = 15.0f;
const CGFloat kIphoneFontSize = 14.0f;
// The alpha values of the suggestion's main and description labels.
const CGFloat kMainLabelAlpha = 0.87f;
const CGFloat kDescriptionLabelAlpha = 0.55f;
// The horizontal space between the edge of the background and the text.
const CGFloat kBorderWidth = 8.0f;
// The space between items in the label.
const CGFloat kSpacing = 4.0f;
// RGB button color when the button is not pressed.
const int kBackgroundNormalColor = 0xeceff1;
// RGB button color when the button is pressed.
const int kBackgroundPressedColor = 0xc4cbcf;
// Structure that record the image for each icon.
struct IconImageMap {
const char* const icon_name;
NSString* image_name;
};
const IconImageMap kCreditCardIconImageMap[] = {
{autofill::kAmericanExpressCard, @"autofill_card_american_express"},
{autofill::kDiscoverCard, @"autofill_card_discover"},
{autofill::kMasterCard, @"autofill_card_mastercard"},
{autofill::kVisaCard, @"autofill_card_visa"},
{autofill::kDinersCard, @"autofill_card_diners"},
{autofill::kGenericCard, @"autofill_card_generic"},
{autofill::kJCBCard, @"autofill_card_jcb"},
{autofill::kUnionPay, @"autofill_card_unionpay"},
};
// Creates a label with the given |text| and |alpha| suitable for use in a
// suggestion button in the keyboard accessory view.
UILabel* TextLabel(NSString* text, CGFloat alpha, BOOL bold) {
base::scoped_nsobject<UILabel> label([[UILabel alloc] init]);
[label setText:text];
CGFloat fontSize = IsIPadIdiom() ? kIpadFontSize : kIphoneFontSize;
UIFont* font = bold ? [UIFont boldSystemFontOfSize:fontSize]
: [UIFont systemFontOfSize:fontSize];
[label setFont:font];
[label setTextColor:[UIColor colorWithWhite:0.0f alpha:alpha]];
[label setBackgroundColor:[UIColor clearColor]];
[label sizeToFit];
return label.autorelease();
}
} // namespace
@interface FormSuggestionLabel ()
// Returns the name of the image for credit card icon.
+ (NSString*)imageNameForCreditCardIcon:(NSString*)icon;
@end
@implementation FormSuggestionLabel {
// Client of this view.
base::WeakNSProtocol<id<FormSuggestionViewClient>> client_;
base::scoped_nsobject<FormSuggestion> suggestion_;
}
- (id)initWithSuggestion:(FormSuggestion*)suggestion
proposedFrame:(CGRect)proposedFrame
client:(id<FormSuggestionViewClient>)client {
// TODO(jimblackler): implement sizeThatFits: and layoutSubviews, and perform
// layout in those methods instead of in the designated initializer.
self = [super initWithFrame:CGRectZero];
if (self) {
suggestion_.reset([suggestion retain]);
client_.reset(client);
const CGFloat frameHeight = CGRectGetHeight(proposedFrame);
CGFloat currentX = kBorderWidth;
// [UIImage imageNamed:] writes error message if nil is passed. Prevent
// console spam by checking the name first.
NSString* iconImageName =
[FormSuggestionLabel imageNameForCreditCardIcon:suggestion.icon];
UIImage* iconImage = nil;
if (iconImageName)
iconImage = [UIImage imageNamed:iconImageName];
if (iconImage) {
UIImageView* iconView =
[[[UIImageView alloc] initWithImage:iconImage] autorelease];
const CGFloat iconY =
std::floor((frameHeight - iconImage.size.height) / 2.0f);
iconView.frame = CGRectMake(currentX, iconY, iconImage.size.width,
iconImage.size.height);
[self addSubview:iconView];
currentX += CGRectGetWidth(iconView.frame) + kSpacing;
}
UILabel* label = TextLabel(suggestion.value, kMainLabelAlpha, YES);
const CGFloat labelY =
std::floor(frameHeight / 2.0f - CGRectGetMidY(label.frame));
label.frame = CGRectMake(currentX, labelY, CGRectGetWidth(label.frame),
CGRectGetHeight(label.frame));
[self addSubview:label];
currentX += CGRectGetWidth(label.frame);
if ([suggestion.displayDescription length] > 0) {
currentX += kSpacing;
UILabel* description =
TextLabel(suggestion.displayDescription, kDescriptionLabelAlpha, NO);
const CGFloat descriptionY =
std::floor(frameHeight / 2.0f - CGRectGetMidY(description.frame));
description.frame =
CGRectMake(currentX, descriptionY, CGRectGetWidth(description.frame),
CGRectGetHeight(description.frame));
[self addSubview:description];
currentX += CGRectGetWidth(description.frame);
}
currentX += kBorderWidth;
self.frame = CGRectMake(proposedFrame.origin.x, proposedFrame.origin.y,
currentX, proposedFrame.size.height);
[self setBackgroundColor:UIColorFromRGB(kBackgroundNormalColor)];
[[self layer] setCornerRadius:kCornerRadius];
[self setClipsToBounds:YES];
[self setIsAccessibilityElement:YES];
[self setAccessibilityLabel:suggestion.value];
[self setUserInteractionEnabled:YES];
}
return self;
}
- (id)initWithFrame:(CGRect)frame {
NOTREACHED();
return nil;
}
#pragma mark -
#pragma mark UIResponder
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
[self setBackgroundColor:UIColorFromRGB(kBackgroundPressedColor)];
}
- (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event {
[self setBackgroundColor:UIColorFromRGB(kBackgroundNormalColor)];
}
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event {
[self setBackgroundColor:UIColorFromRGB(kBackgroundNormalColor)];
[client_ didSelectSuggestion:suggestion_];
}
#pragma mark -
#pragma mark Private
+ (NSString*)imageNameForCreditCardIcon:(NSString*)icon {
if (!icon || [icon length] == 0) {
return nil;
}
std::string iconName(base::SysNSStringToUTF8(icon));
for (size_t i = 0; i < arraysize(kCreditCardIconImageMap); ++i) {
if (iconName.compare(kCreditCardIconImageMap[i].icon_name) == 0) {
return kCreditCardIconImageMap[i].image_name;
}
}
return nil;
}
@end
|