Commit 20ff0e74 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

[iOS] Create the TableView link footer

This CL creates a TableView item for handling a footer which would
display a link in a TextView.
This is designed as a replacement for CollectionViewFooterItem.

Bug: 894791
Cq-Include-Trybots: luci.chromium.try:ios-simulator-cronet;luci.chromium.try:ios-simulator-full-configs
Change-Id: Id4b84e5b17920237016314447bed9a25140ab30c
Reviewed-on: https://chromium-review.googlesource.com/c/1286416
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#600717}
parent 57c4e7d9
......@@ -6,6 +6,7 @@
#import "ios/chrome/browser/ui/settings/cells/settings_detail_item.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_accessory_item.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_link_header_footer_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_header_footer_item.h"
#import "ios/chrome/browser/ui/table_view/cells/table_view_text_item.h"
......@@ -21,6 +22,7 @@ namespace {
typedef NS_ENUM(NSInteger, SectionIdentifier) {
SectionIdentifierText = kSectionIdentifierEnumZero,
SectionIdentifierSettings,
SectionIdentifierURL,
};
......@@ -37,6 +39,7 @@ typedef NS_ENUM(NSInteger, ItemType) {
ItemTypeURLWithSupplementalText,
ItemTypeURLWithBadgeImage,
ItemTypeTextSettingsDetail,
ItemTypeLinkFooter,
};
}
......@@ -66,6 +69,7 @@ typedef NS_ENUM(NSInteger, ItemType) {
TableViewModel* model = self.tableViewModel;
[model addSectionWithIdentifier:SectionIdentifierText];
[model addSectionWithIdentifier:SectionIdentifierSettings];
[model addSectionWithIdentifier:SectionIdentifierURL];
// SectionIdentifierText.
......@@ -113,19 +117,27 @@ typedef NS_ENUM(NSInteger, ItemType) {
[model addItem:textActionButtonItem
toSectionWithIdentifier:SectionIdentifierText];
// SectionIdentifierSettings.
SettingsDetailItem* settingsDetailItem =
[[SettingsDetailItem alloc] initWithType:ItemTypeTextSettingsDetail];
settingsDetailItem.text = @"Short text";
settingsDetailItem.text = @"Settings cells";
settingsDetailItem.detailText = @"Short";
[model addItem:settingsDetailItem
toSectionWithIdentifier:SectionIdentifierText];
toSectionWithIdentifier:SectionIdentifierSettings];
SettingsDetailItem* settingsDetailItemLong =
[[SettingsDetailItem alloc] initWithType:ItemTypeTextSettingsDetail];
settingsDetailItemLong.text = @"Very long text eating the other detail label";
settingsDetailItemLong.detailText = @"A bit less short";
[model addItem:settingsDetailItemLong
toSectionWithIdentifier:SectionIdentifierText];
toSectionWithIdentifier:SectionIdentifierSettings];
TableViewLinkHeaderFooterItem* linkFooter =
[[TableViewLinkHeaderFooterItem alloc] initWithType:ItemTypeLinkFooter];
linkFooter.text =
@"This is a footer text view with a BEGIN_LINKlinkEND_LINK in the middle";
[model setFooter:linkFooter
forSectionWithIdentifier:SectionIdentifierSettings];
// SectionIdentifierURL.
TableViewURLItem* item =
......
......@@ -16,6 +16,8 @@ source_set("cells") {
"table_view_header_footer_item.mm",
"table_view_item.h",
"table_view_item.mm",
"table_view_link_header_footer_item.h",
"table_view_link_header_footer_item.mm",
"table_view_signin_promo_item.h",
"table_view_signin_promo_item.mm",
"table_view_text_button_item.h",
......@@ -48,6 +50,7 @@ source_set("cells") {
"//ios/chrome/common/favicon",
"//ios/chrome/common/ui_util",
"//ios/third_party/material_components_ios:material_components_ios",
"//net",
"//ui/base",
"//url:url",
]
......
// Copyright 2018 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.
#ifndef IOS_CHROME_BROWSER_UI_TABLE_VIEW_CELLS_TABLE_VIEW_LINK_HEADER_FOOTER_ITEM_H_
#define IOS_CHROME_BROWSER_UI_TABLE_VIEW_CELLS_TABLE_VIEW_LINK_HEADER_FOOTER_ITEM_H_
#import <UIKit/UIKit.h>
#import "ios/chrome/browser/ui/table_view/cells/table_view_header_footer_item.h"
#import "url/gurl.h"
@class TableViewLinkHeaderFooterView;
@protocol TableViewLinkHeaderFooterItemDelegate<NSObject>
// Notifies the delegate that the link corresponding to |URL| was tapped in
// |view|.
- (void)view:(TableViewLinkHeaderFooterView*)view didTapLinkURL:(GURL)URL;
@end
// TableViewLinkHeaderFooterItem is the model class corresponding to
// TableViewLinkHeaderFooterView.
@interface TableViewLinkHeaderFooterItem : TableViewHeaderFooterItem
// The URL to open when the cell is tapped.
@property(nonatomic, assign) GURL linkURL;
// The main text string.
@property(nonatomic, copy) NSString* text;
@end
// UITableViewHeaderFooterView subclass containing a single UITextView. The text
// view is laid to fill the full width of the cell and it is wrapped as needed
// to fit in the cell. If it contains a link, the link is correctly displayed as
// link and the delegate is notified if it is tapped.
@interface TableViewLinkHeaderFooterView : UITableViewHeaderFooterView
// Delegate to notify when the link is tapped.
@property(nonatomic, weak) id<TableViewLinkHeaderFooterItemDelegate> delegate;
// The URL to open when the link is tapped.
@property(nonatomic, assign) GURL linkURL;
// Sets the |text| displayed by this cell. If |text| is containing a link, the
// link is appropriately colored.
- (void)setText:(NSString*)text;
@end
#endif // IOS_CHROME_BROWSER_UI_TABLE_VIEW_CELLS_TABLE_VIEW_LINK_HEADER_FOOTER_ITEM_H_
// Copyright 2018 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/ui/table_view/cells/table_view_link_header_footer_item.h"
#import "ios/chrome/browser/ui/uikit_ui_util.h"
#import "ios/chrome/common/string_util.h"
#import "net/base/mac/url_conversions.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
// Padding used on the leading and trailing edges of the cell.
const CGFloat kHorizontalPadding = 24;
// Padding used on the top and bottom edges of the cell.
const CGFloat kVerticalPadding = 16;
// Text color.
const int kTextColor = 0x8A8A8F;
} // namespace
@implementation TableViewLinkHeaderFooterItem
- (instancetype)initWithType:(NSInteger)type {
self = [super initWithType:type];
if (self) {
self.cellClass = [TableViewLinkHeaderFooterView class];
}
return self;
}
#pragma mark CollectionViewItem
- (void)configureHeaderFooterView:(TableViewLinkHeaderFooterView*)headerFooter
withStyler:(ChromeTableViewStyler*)styler {
[super configureHeaderFooterView:headerFooter withStyler:styler];
headerFooter.linkURL = self.linkURL;
[headerFooter setText:self.text];
}
@end
@interface TableViewLinkHeaderFooterView ()<UITextViewDelegate>
// UITextView corresponding to |text| from the item.
@property(nonatomic, readonly, strong) UITextView* textView;
@end
@implementation TableViewLinkHeaderFooterView
@synthesize textView = _textView;
- (instancetype)initWithReuseIdentifier:(NSString*)reuseIdentifier {
self = [super initWithReuseIdentifier:reuseIdentifier];
if (self) {
self.isAccessibilityElement = YES;
_textView = [[UITextView alloc] init];
_textView.scrollEnabled = NO;
_textView.editable = NO;
_textView.delegate = self;
_textView.backgroundColor = [UIColor clearColor];
_textView.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
_textView.adjustsFontForContentSizeCategory = YES;
_textView.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:_textView];
[NSLayoutConstraint activateConstraints:@[
[_textView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor
constant:kVerticalPadding],
[_textView.bottomAnchor
constraintEqualToAnchor:self.contentView.bottomAnchor
constant:-kVerticalPadding],
[_textView.trailingAnchor
constraintEqualToAnchor:self.contentView.trailingAnchor
constant:-kHorizontalPadding],
[_textView.leadingAnchor
constraintEqualToAnchor:self.contentView.leadingAnchor
constant:kHorizontalPadding],
]];
}
return self;
}
- (void)setText:(NSString*)text {
NSRange range;
NSString* strippedText = ParseStringWithLink(text, &range);
NSRange fullRange = NSMakeRange(0, strippedText.length);
NSMutableAttributedString* attributedText =
[[NSMutableAttributedString alloc] initWithString:strippedText];
[attributedText addAttribute:NSForegroundColorAttributeName
value:UIColorFromRGB(kTextColor)
range:fullRange];
[attributedText
addAttribute:NSFontAttributeName
value:[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]
range:fullRange];
if (range.location != NSNotFound && range.length != 0) {
NSURL* URL = net::NSURLWithGURL(self.linkURL);
id linkValue = URL ? URL : @"";
[attributedText addAttribute:NSLinkAttributeName
value:linkValue
range:range];
}
self.textView.attributedText = attributedText;
}
- (void)prepareForReuse {
[super prepareForReuse];
[self setText:nil];
}
#pragma mark - UITextViewDelegate
- (BOOL)textView:(UITextView*)textView
shouldInteractWithURL:(NSURL*)URL
inRange:(NSRange)characterRange
interaction:(UITextItemInteraction)interaction {
DCHECK(self.textView == textView);
GURL convertedURL = URL ? net::GURLWithNSURL(URL) : self.linkURL;
[self.delegate view:self didTapLinkURL:convertedURL];
// Returns NO as the app is handling the opening of the URL.
return NO;
}
@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