Commit 0e87e869 authored by sczs's avatar sczs Committed by Commit Bot

[ios] Enhances TableViewTextButton to work with the Passwords Infobar Modal

Makes changes to TableViewTextButton so it can be used with the Password
Infobar Modal.
Adds these buttons to InfobarPasswordTableViewController and hooks them
to their actions.

Screenshot:
https://drive.google.com/open?id=1CAJc2tT_oyL9Xyrm6b6krtZo1fxcey7T

Bug: 945478
Change-Id: Iadfdec6081a5742ec786102e040cf7a38396c17b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1572962Reviewed-by: default avatarPeter Lee <pkl@chromium.org>
Reviewed-by: default avatarChris Lu <thegreenfrog@chromium.org>
Commit-Queue: Sergio Collazos <sczs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#652540}
parent d6198c6c
...@@ -93,9 +93,14 @@ ...@@ -93,9 +93,14 @@
withString:@"•" withString:@"•"
startingAtIndex:0]; startingAtIndex:0];
self.modalViewController.unmaskedPassword = password; self.modalViewController.unmaskedPassword = password;
self.modalViewController.detailsTextMessage =
self.passwordInfoBarDelegate->GetDetailsMessageText();
self.modalViewController.saveButtonText = self.modalViewController.saveButtonText =
base::SysUTF16ToNSString(self.passwordInfoBarDelegate->GetButtonLabel( base::SysUTF16ToNSString(self.passwordInfoBarDelegate->GetButtonLabel(
ConfirmInfoBarDelegate::BUTTON_OK)); ConfirmInfoBarDelegate::BUTTON_OK));
self.modalViewController.cancelButtonText =
base::SysUTF16ToNSString(self.passwordInfoBarDelegate->GetButtonLabel(
ConfirmInfoBarDelegate::BUTTON_CANCEL));
self.modalViewController.URL = self.passwordInfoBarDelegate->GetURLHostText(); self.modalViewController.URL = self.passwordInfoBarDelegate->GetURLHostText();
} }
...@@ -121,4 +126,9 @@ ...@@ -121,4 +126,9 @@
[self dismissInfobarModal:self completion:nil]; [self dismissInfobarModal:self completion:nil];
} }
- (void)neverSaveCredentialsForCurrentSite {
self.passwordInfoBarDelegate->Cancel();
[self dismissInfobarModal:self completion:nil];
}
@end @end
...@@ -8,7 +8,7 @@ source_set("modals") { ...@@ -8,7 +8,7 @@ source_set("modals") {
"infobar_modal_delegate.h", "infobar_modal_delegate.h",
"infobar_modal_view_controller.h", "infobar_modal_view_controller.h",
"infobar_modal_view_controller.mm", "infobar_modal_view_controller.mm",
"infobar_password_modal_delegate", "infobar_password_modal_delegate.h",
"infobar_password_table_view_controller.h", "infobar_password_table_view_controller.h",
"infobar_password_table_view_controller.mm", "infobar_password_table_view_controller.mm",
] ]
......
...@@ -17,6 +17,10 @@ ...@@ -17,6 +17,10 @@
- (void)updateCredentialsWithUsername:(NSString*)username - (void)updateCredentialsWithUsername:(NSString*)username
password:(NSString*)password; password:(NSString*)password;
// Blacklists the current site to never prompt the user to save its credentials
// again.
- (void)neverSaveCredentialsForCurrentSite;
@end @end
#endif // IOS_CHROME_BROWSER_UI_INFOBARS_MODALS_INFOBAR_PASSWORD_MODAL_DELEGATE_H_ #endif // IOS_CHROME_BROWSER_UI_INFOBARS_MODALS_INFOBAR_PASSWORD_MODAL_DELEGATE_H_
...@@ -22,10 +22,14 @@ ...@@ -22,10 +22,14 @@
@property(nonatomic, copy) NSString* maskedPassword; @property(nonatomic, copy) NSString* maskedPassword;
// The unmasked password for the InfobarModal. // The unmasked password for the InfobarModal.
@property(nonatomic, copy) NSString* unmaskedPassword; @property(nonatomic, copy) NSString* unmaskedPassword;
// The details text message being displayed in the InfobarModal.
@property(nonatomic, copy) NSString* detailsTextMessage;
// The URL being displayed in the InfobarModal. // The URL being displayed in the InfobarModal.
@property(nonatomic, copy) NSString* URL; @property(nonatomic, copy) NSString* URL;
// The text used for the save credentials button. // The text used for the save credentials button.
@property(nonatomic, copy) NSString* saveButtonText; @property(nonatomic, copy) NSString* saveButtonText;
// The text used for the cancel button.
@property(nonatomic, copy) NSString* cancelButtonText;
@end @end
......
...@@ -9,7 +9,6 @@ ...@@ -9,7 +9,6 @@
#import "ios/chrome/browser/ui/infobars/modals/infobar_modal_constants.h" #import "ios/chrome/browser/ui/infobars/modals/infobar_modal_constants.h"
#import "ios/chrome/browser/ui/infobars/modals/infobar_password_modal_delegate.h" #import "ios/chrome/browser/ui/infobars/modals/infobar_password_modal_delegate.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_cells_constants.h" #import "ios/chrome/browser/ui/table_view/cells/table_view_cells_constants.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_detail_icon_item.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_text_button_item.h" #import "ios/chrome/browser/ui/table_view/cells/table_view_text_button_item.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_text_edit_item.h" #import "ios/chrome/browser/ui/table_view/cells/table_view_text_edit_item.h"
#import "ios/chrome/browser/ui/table_view/chrome_table_view_styler.h" #import "ios/chrome/browser/ui/table_view/chrome_table_view_styler.h"
...@@ -29,6 +28,7 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -29,6 +28,7 @@ typedef NS_ENUM(NSInteger, ItemType) {
ItemTypeUsername, ItemTypeUsername,
ItemTypePassword, ItemTypePassword,
ItemTypeSaveCredentials, ItemTypeSaveCredentials,
ItemTypeNeverForThisSite,
}; };
@interface InfobarPasswordTableViewController () @interface InfobarPasswordTableViewController ()
...@@ -38,6 +38,8 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -38,6 +38,8 @@ typedef NS_ENUM(NSInteger, ItemType) {
@property(nonatomic, strong) TableViewTextEditItem* passwordItem; @property(nonatomic, strong) TableViewTextEditItem* passwordItem;
// Item that holds the SaveCredentials Button information. // Item that holds the SaveCredentials Button information.
@property(nonatomic, strong) TableViewTextButtonItem* saveCredentialsItem; @property(nonatomic, strong) TableViewTextButtonItem* saveCredentialsItem;
// Item that holds the Never Save for this site Button information.
@property(nonatomic, strong) TableViewTextButtonItem* neverForThisSiteItem;
@end @end
@implementation InfobarPasswordTableViewController @implementation InfobarPasswordTableViewController
...@@ -81,12 +83,12 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -81,12 +83,12 @@ typedef NS_ENUM(NSInteger, ItemType) {
TableViewModel* model = self.tableViewModel; TableViewModel* model = self.tableViewModel;
[model addSectionWithIdentifier:SectionIdentifierContent]; [model addSectionWithIdentifier:SectionIdentifierContent];
TableViewDetailIconItem* URLDetailItem = TableViewTextEditItem* URLItem =
[[TableViewDetailIconItem alloc] initWithType:ItemTypeURL]; [[TableViewTextEditItem alloc] initWithType:ItemTypeURL];
URLDetailItem.text = l10n_util::GetNSString(IDS_IOS_SHOW_PASSWORD_VIEW_SITE); URLItem.textFieldName =
URLDetailItem.detailText = self.URL; l10n_util::GetNSString(IDS_IOS_SHOW_PASSWORD_VIEW_SITE);
[model addItem:URLDetailItem URLItem.textFieldValue = self.URL;
toSectionWithIdentifier:SectionIdentifierContent]; [model addItem:URLItem toSectionWithIdentifier:SectionIdentifierContent];
self.usernameItem = self.usernameItem =
[[TableViewTextEditItem alloc] initWithType:ItemTypeUsername]; [[TableViewTextEditItem alloc] initWithType:ItemTypeUsername];
...@@ -103,16 +105,25 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -103,16 +105,25 @@ typedef NS_ENUM(NSInteger, ItemType) {
self.passwordItem.textFieldName = self.passwordItem.textFieldName =
l10n_util::GetNSString(IDS_IOS_SHOW_PASSWORD_VIEW_PASSWORD); l10n_util::GetNSString(IDS_IOS_SHOW_PASSWORD_VIEW_PASSWORD);
self.passwordItem.textFieldValue = self.maskedPassword; self.passwordItem.textFieldValue = self.maskedPassword;
self.passwordItem.textFieldEnabled = YES;
self.passwordItem.autoCapitalizationType = UITextAutocapitalizationTypeNone;
[model addItem:self.passwordItem [model addItem:self.passwordItem
toSectionWithIdentifier:SectionIdentifierContent]; toSectionWithIdentifier:SectionIdentifierContent];
self.saveCredentialsItem = self.saveCredentialsItem =
[[TableViewTextButtonItem alloc] initWithType:ItemTypeSaveCredentials]; [[TableViewTextButtonItem alloc] initWithType:ItemTypeSaveCredentials];
self.saveCredentialsItem.textAlignment = NSTextAlignmentNatural;
self.saveCredentialsItem.text = self.detailsTextMessage;
self.saveCredentialsItem.buttonText = self.saveButtonText; self.saveCredentialsItem.buttonText = self.saveButtonText;
self.saveCredentialsItem.disableButtonIntrinsicWidth = YES;
[model addItem:self.saveCredentialsItem [model addItem:self.saveCredentialsItem
toSectionWithIdentifier:SectionIdentifierContent]; toSectionWithIdentifier:SectionIdentifierContent];
self.neverForThisSiteItem =
[[TableViewTextButtonItem alloc] initWithType:ItemTypeNeverForThisSite];
self.neverForThisSiteItem.buttonText = self.cancelButtonText;
self.neverForThisSiteItem.buttonTextColor = [UIColor blueColor];
self.neverForThisSiteItem.buttonBackgroundColor = [UIColor clearColor];
[model addItem:self.neverForThisSiteItem
toSectionWithIdentifier:SectionIdentifierContent];
} }
#pragma mark - UITableViewDataSource #pragma mark - UITableViewDataSource
...@@ -134,6 +145,15 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -134,6 +145,15 @@ typedef NS_ENUM(NSInteger, ItemType) {
forControlEvents:UIControlEventTouchUpInside]; forControlEvents:UIControlEventTouchUpInside];
break; break;
} }
case ItemTypeNeverForThisSite: {
TableViewTextButtonCell* tableViewTextButtonCell =
base::mac::ObjCCastStrict<TableViewTextButtonCell>(cell);
[tableViewTextButtonCell.button
addTarget:self.infobarModalDelegate
action:@selector(neverSaveCredentialsForCurrentSite)
forControlEvents:UIControlEventTouchUpInside];
break;
}
case ItemTypeUsername: case ItemTypeUsername:
case ItemTypePassword: { case ItemTypePassword: {
TableViewTextEditCell* editCell = TableViewTextEditCell* editCell =
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
namespace { namespace {
// The presented view Height // The presented view Height
const CGFloat kPresentedViewHeight = 267.0; const CGFloat kPresentedViewHeight = 350.0;
// The presented view outer horizontal margins. // The presented view outer horizontal margins.
const CGFloat kPresentedViewHorizontalMargin = 10.0; const CGFloat kPresentedViewHorizontalMargin = 10.0;
// The presented view maximum width. // The presented view maximum width.
......
...@@ -156,11 +156,46 @@ typedef NS_ENUM(NSInteger, ItemType) { ...@@ -156,11 +156,46 @@ typedef NS_ENUM(NSInteger, ItemType) {
TableViewTextButtonItem* textActionButtonItem = TableViewTextButtonItem* textActionButtonItem =
[[TableViewTextButtonItem alloc] initWithType:ItemTypeTextButton]; [[TableViewTextButtonItem alloc] initWithType:ItemTypeTextButton];
textActionButtonItem.text = @"Hello, you should do something."; textActionButtonItem.text = @"Hello, you should do something. Also as you "
@"can see this text is centered.";
textActionButtonItem.buttonText = @"Do something"; textActionButtonItem.buttonText = @"Do something";
[model addItem:textActionButtonItem [model addItem:textActionButtonItem
toSectionWithIdentifier:SectionIdentifierText]; toSectionWithIdentifier:SectionIdentifierText];
TableViewTextButtonItem* textActionButtonAlignmentItem =
[[TableViewTextButtonItem alloc] initWithType:ItemTypeTextButton];
textActionButtonAlignmentItem.text =
@"Hello, you should do something. Also as you can see this text is using "
@"NSTextAlignmentNatural";
textActionButtonAlignmentItem.textAlignment = NSTextAlignmentNatural;
textActionButtonAlignmentItem.buttonText = @"Do something";
[model addItem:textActionButtonAlignmentItem
toSectionWithIdentifier:SectionIdentifierText];
TableViewTextButtonItem* textActionButtoExpandedItem =
[[TableViewTextButtonItem alloc] initWithType:ItemTypeTextButton];
textActionButtoExpandedItem.text = @"Hello, you should do something.";
textActionButtoExpandedItem.disableButtonIntrinsicWidth = YES;
textActionButtoExpandedItem.buttonText = @"Expanded Button";
[model addItem:textActionButtoExpandedItem
toSectionWithIdentifier:SectionIdentifierText];
TableViewTextButtonItem* textActionButtonNoTextItem =
[[TableViewTextButtonItem alloc] initWithType:ItemTypeTextButton];
textActionButtonNoTextItem.buttonText = @"Do something, no Top Text";
[model addItem:textActionButtonNoTextItem
toSectionWithIdentifier:SectionIdentifierText];
TableViewTextButtonItem* textActionButtonColorItem =
[[TableViewTextButtonItem alloc] initWithType:ItemTypeTextButton];
textActionButtonColorItem.text = @"Hello, you should do something.";
textActionButtonColorItem.disableButtonIntrinsicWidth = YES;
textActionButtonColorItem.buttonBackgroundColor = [UIColor lightGrayColor];
textActionButtonColorItem.buttonTextColor = [UIColor greenColor];
textActionButtonColorItem.buttonText = @"Do something, different Colors";
[model addItem:textActionButtonColorItem
toSectionWithIdentifier:SectionIdentifierText];
TableViewDetailTextItem* detailTextItem = TableViewDetailTextItem* detailTextItem =
[[TableViewDetailTextItem alloc] initWithType:ItemTypeDetailText]; [[TableViewDetailTextItem alloc] initWithType:ItemTypeDetailText];
detailTextItem.text = @"Item with two labels"; detailTextItem.text = @"Item with two labels";
......
...@@ -15,9 +15,15 @@ ...@@ -15,9 +15,15 @@
// Text being displayed above the button. // Text being displayed above the button.
@property(nonatomic, readwrite, strong) NSString* text; @property(nonatomic, readwrite, strong) NSString* text;
// Text being displayed above the button alignment.
@property(nonatomic, readwrite, assign) NSTextAlignment textAlignment;
// Text for cell button. // Text for cell button.
@property(nonatomic, readwrite, strong) NSString* buttonText; @property(nonatomic, readwrite, strong) NSString* buttonText;
// Button text color.
@property(nonatomic, strong) UIColor* buttonTextColor;
// Button background color. Default is custom blue color. // Button background color. Default is custom blue color.
@property(nonatomic, strong) UIColor* buttonBackgroundColor; @property(nonatomic, strong) UIColor* buttonBackgroundColor;
...@@ -28,6 +34,10 @@ ...@@ -28,6 +34,10 @@
// default. // default.
@property(nonatomic, assign, getter=isEnabled) BOOL enabled; @property(nonatomic, assign, getter=isEnabled) BOOL enabled;
// If YES the item's button width will expand to match the cell's. If NO the
// button will maintain its intrinsic size based on its title. NO by default.
@property(nonatomic, assign) BOOL disableButtonIntrinsicWidth;
@end @end
// TableViewTextButtonCell contains a textLabel and a UIbutton // TableViewTextButtonCell contains a textLabel and a UIbutton
...@@ -40,6 +50,15 @@ ...@@ -40,6 +50,15 @@
// Action button. Note: Set action method in the TableView datasource method. // Action button. Note: Set action method in the TableView datasource method.
@property(nonatomic, strong) UIButton* button; @property(nonatomic, strong) UIButton* button;
// Enables spacing between items. If there's only one item (e.g. just a button),
// disable the spacing or an extra top padding will be added.
- (void)enableItemSpacing:(BOOL)enable;
// If |disabled| is YES the button's width will expand to match the cell's
// container. If NO, the button will maintain its intrinsic size based on its
// title.
- (void)disableButtonIntrinsicWidth:(BOOL)disable;
@end @end
#endif // IOS_CHROME_BROWSER_UI_TABLE_VIEW_CELLS_TABLE_VIEW_TEXT_BUTTON_ITEM_H_ #endif // IOS_CHROME_BROWSER_UI_TABLE_VIEW_CELLS_TABLE_VIEW_TEXT_BUTTON_ITEM_H_
...@@ -13,25 +13,29 @@ ...@@ -13,25 +13,29 @@
namespace { namespace {
// Text label gray color. // Text label gray color.
const CGFloat grayHexColor = 0x6d6d72; const CGFloat kGrayHexColor = 0x6d6d72;
// Action button blue background color. // Action button blue background color.
const CGFloat blueHexColor = 0x1A73E8; const CGFloat kBlueHexColor = 0x1A73E8;
// Default Button title Color.
const CGFloat kDefaultButtonTitleColor = 0xFFFFFF;
// Alpha value for the disabled action button. // Alpha value for the disabled action button.
const CGFloat disabledButtonAlpha = 0.5; const CGFloat kDisabledButtonAlpha = 0.5;
// Vertical spacing between stackView and cell contentView. // Vertical spacing between stackView and cell contentView.
const CGFloat stackViewVerticalSpacing = 9.0; const CGFloat kStackViewVerticalSpacing = 9.0;
// Horizontal spacing between stackView and cell contentView. // Horizontal spacing between stackView and cell contentView.
const CGFloat stackViewHorizontalSpacing = 16.0; const CGFloat kStackViewHorizontalSpacing = 16.0;
// SubView spacing within stackView. // SubView spacing within stackView.
const CGFloat stackViewSubViewSpacing = 13.0; const CGFloat kStackViewSubViewSpacing = 13.0;
// Horizontal Inset between button contents and edge. // Horizontal Inset between button contents and edge.
const CGFloat buttonTitleHorizontalContentInset = 40.0; const CGFloat kButtonTitleHorizontalContentInset = 40.0;
// Vertical Inset between button contents and edge. // Vertical Inset between button contents and edge.
const CGFloat buttonTitleVerticalContentInset = 8.0; const CGFloat kButtonTitleVerticalContentInset = 8.0;
// Button corner radius. // Button corner radius.
const CGFloat buttonCornerRadius = 8; const CGFloat kButtonCornerRadius = 8;
// Font Size for Button Title Label. // Font Size for Button Title Label.
const CGFloat buttonTitleFontSize = 17.0; const CGFloat kButtonTitleFontSize = 17.0;
const NSTextAlignment kDefaultTextAlignment = NSTextAlignmentCenter;
} // namespace } // namespace
@implementation TableViewTextButtonItem @implementation TableViewTextButtonItem
...@@ -45,6 +49,7 @@ const CGFloat buttonTitleFontSize = 17.0; ...@@ -45,6 +49,7 @@ const CGFloat buttonTitleFontSize = 17.0;
if (self) { if (self) {
self.cellClass = [TableViewTextButtonCell class]; self.cellClass = [TableViewTextButtonCell class];
_enabled = YES; _enabled = YES;
_textAlignment = kDefaultTextAlignment;
} }
return self; return self;
} }
...@@ -55,21 +60,35 @@ const CGFloat buttonTitleFontSize = 17.0; ...@@ -55,21 +60,35 @@ const CGFloat buttonTitleFontSize = 17.0;
TableViewTextButtonCell* cell = TableViewTextButtonCell* cell =
base::mac::ObjCCastStrict<TableViewTextButtonCell>(tableCell); base::mac::ObjCCastStrict<TableViewTextButtonCell>(tableCell);
cell.textLabel.text = self.text; cell.textLabel.text = self.text;
[cell enableItemSpacing:[self.text length]];
[cell disableButtonIntrinsicWidth:self.disableButtonIntrinsicWidth];
cell.textLabel.textAlignment = self.textAlignment;
[cell.button setTitle:self.buttonText forState:UIControlStateNormal]; [cell.button setTitle:self.buttonText forState:UIControlStateNormal];
if (self.buttonTextColor) {
[cell.button setTitleColor:self.buttonTextColor
forState:UIControlStateNormal];
}
cell.button.accessibilityIdentifier = self.buttonAccessibilityIdentifier; cell.button.accessibilityIdentifier = self.buttonAccessibilityIdentifier;
cell.button.backgroundColor = self.buttonBackgroundColor cell.button.backgroundColor = self.buttonBackgroundColor
? self.buttonBackgroundColor ? self.buttonBackgroundColor
: UIColorFromRGB(blueHexColor); : UIColorFromRGB(kBlueHexColor);
[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.button.enabled = self.enabled; cell.button.enabled = self.enabled;
if (!self.enabled) { if (!self.enabled) {
cell.button.backgroundColor = [cell.button.backgroundColor cell.button.backgroundColor = [cell.button.backgroundColor
colorWithAlphaComponent:disabledButtonAlpha]; colorWithAlphaComponent:kDisabledButtonAlpha];
} }
} }
@end @end
@interface TableViewTextButtonCell ()
// StackView that contains the cell's Button and Label.
@property(nonatomic, strong) UIStackView* verticalStackView;
// Constraints used to match the Button width to the StackView.
@property(nonatomic, strong) NSArray* expandedButtonWidthConstraints;
@end
@implementation TableViewTextButtonCell @implementation TableViewTextButtonCell
@synthesize textLabel = _textLabel; @synthesize textLabel = _textLabel;
@synthesize button = _button; @synthesize button = _button;
...@@ -85,48 +104,80 @@ const CGFloat buttonTitleFontSize = 17.0; ...@@ -85,48 +104,80 @@ const CGFloat buttonTitleFontSize = 17.0;
self.textLabel.textAlignment = NSTextAlignmentCenter; self.textLabel.textAlignment = NSTextAlignmentCenter;
self.textLabel.font = self.textLabel.font =
[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]; [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
self.textLabel.textColor = UIColorFromRGB(grayHexColor); self.textLabel.textColor = UIColorFromRGB(kGrayHexColor);
// Create button. // Create button.
self.button = [[UIButton alloc] init]; self.button = [[UIButton alloc] init];
[self.button setTitleColor:[UIColor whiteColor] [self.button setTitleColor:UIColorFromRGB(kDefaultButtonTitleColor)
forState:UIControlStateNormal]; forState:UIControlStateNormal];
self.button.translatesAutoresizingMaskIntoConstraints = NO; self.button.translatesAutoresizingMaskIntoConstraints = NO;
[self.button.titleLabel [self.button.titleLabel
setFont:[UIFont boldSystemFontOfSize:buttonTitleFontSize]]; setFont:[UIFont boldSystemFontOfSize:kButtonTitleFontSize]];
self.button.layer.cornerRadius = buttonCornerRadius; self.button.layer.cornerRadius = kButtonCornerRadius;
self.button.clipsToBounds = YES; self.button.clipsToBounds = YES;
self.button.contentEdgeInsets = UIEdgeInsetsMake( self.button.contentEdgeInsets = UIEdgeInsetsMake(
buttonTitleVerticalContentInset, buttonTitleHorizontalContentInset, kButtonTitleVerticalContentInset, kButtonTitleHorizontalContentInset,
buttonTitleVerticalContentInset, buttonTitleHorizontalContentInset); kButtonTitleVerticalContentInset, kButtonTitleHorizontalContentInset);
// Vertical stackView to hold label and button. // Vertical stackView to hold label and button.
UIStackView* verticalStackView = [[UIStackView alloc] self.verticalStackView = [[UIStackView alloc]
initWithArrangedSubviews:@[ self.textLabel, self.button ]]; initWithArrangedSubviews:@[ self.textLabel, self.button ]];
verticalStackView.alignment = UIStackViewAlignmentCenter; self.verticalStackView.alignment = UIStackViewAlignmentCenter;
verticalStackView.axis = UILayoutConstraintAxisVertical; self.verticalStackView.axis = UILayoutConstraintAxisVertical;
verticalStackView.spacing = stackViewSubViewSpacing; self.verticalStackView.translatesAutoresizingMaskIntoConstraints = NO;
verticalStackView.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:verticalStackView]; [self.contentView addSubview:self.verticalStackView];
// Add constraints for stackView // Add constraints for stackView
[NSLayoutConstraint activateConstraints:@[ [NSLayoutConstraint activateConstraints:@[
[verticalStackView.leadingAnchor [self.verticalStackView.leadingAnchor
constraintEqualToAnchor:self.contentView.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor
constant:stackViewHorizontalSpacing], constant:kStackViewHorizontalSpacing],
[verticalStackView.trailingAnchor [self.verticalStackView.trailingAnchor
constraintEqualToAnchor:self.contentView.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor
constant:-stackViewHorizontalSpacing], constant:-kStackViewHorizontalSpacing],
[verticalStackView.topAnchor [self.verticalStackView.topAnchor
constraintEqualToAnchor:self.contentView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor
constant:stackViewVerticalSpacing], constant:kStackViewVerticalSpacing],
[verticalStackView.bottomAnchor [self.verticalStackView.bottomAnchor
constraintEqualToAnchor:self.contentView.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor
constant:-stackViewVerticalSpacing] constant:-kStackViewVerticalSpacing]
]]; ]];
self.expandedButtonWidthConstraints = @[
[self.button.leadingAnchor
constraintEqualToAnchor:self.verticalStackView.leadingAnchor],
[self.button.trailingAnchor
constraintEqualToAnchor:self.verticalStackView.trailingAnchor],
];
} }
return self; return self;
} }
#pragma mark - Public Methods
- (void)enableItemSpacing:(BOOL)enable {
self.verticalStackView.spacing = enable ? kStackViewSubViewSpacing : 0;
}
- (void)disableButtonIntrinsicWidth:(BOOL)disable {
if (disable) {
[NSLayoutConstraint
activateConstraints:self.expandedButtonWidthConstraints];
} else {
[NSLayoutConstraint
deactivateConstraints:self.expandedButtonWidthConstraints];
}
}
#pragma mark - UITableViewCell
- (void)prepareForReuse {
[super prepareForReuse];
[self.button setTitleColor:UIColorFromRGB(kDefaultButtonTitleColor)
forState:UIControlStateNormal];
self.textLabel.textAlignment = kDefaultTextAlignment;
[self disableButtonIntrinsicWidth:NO];
}
@end @end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment