Commit 3924c582 authored by Kurt Horimoto's avatar Kurt Horimoto Committed by Commit Bot

[iOS] Add NSAttributedString option for empty table view messages.

This CL updates TableViewEmptyView to use NSAttributedStrings rather
than using the UILabel equivalents.  This allows ChromeTableView
subclasses to define custom text styling if necessary.

Bug: 851962
Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: Ib41dedfcb659ddfbe974db7d88b8867d6d9d5c20
Reviewed-on: https://chromium-review.googlesource.com/1135760Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Reviewed-by: default avatarRohit Rao <rohitrao@chromium.org>
Commit-Queue: Kurt Horimoto <kkhorimoto@chromium.org>
Cr-Commit-Position: refs/heads/master@{#575776}
parent 0933be71
......@@ -59,10 +59,17 @@ typedef NS_ENUM(NSInteger, ChromeTableViewControllerStyle) {
- (void)stopLoadingIndicatorWithCompletion:(ProceduralBlock)completion;
// Adds an empty table view in the center of the ChromeTableViewController which
// displays |message| with |image| on top. This will remove any existing table
// view background views.
// displays |message| with |image| on top. |message| will be rendered using
// default styling. This will remove any existing table view background views.
- (void)addEmptyTableViewWithMessage:(NSString*)message image:(UIImage*)image;
// Adds an empty table view in the center of the ChromeTableViewController which
// displays |attributedMessage| with |image| on top. This will remove any
// existing table view background views.
- (void)addEmptyTableViewWithAttributedMessage:
(NSAttributedString*)attributedMessage
image:(UIImage*)image;
// Removes the empty table view, if one is present.
- (void)removeEmptyTableView;
......
......@@ -38,8 +38,6 @@ const CGFloat kTableViewSeparatorColor = 0xC8C7CC;
@synthesize styler = _styler;
@synthesize tableViewModel = _tableViewModel;
#pragma mark - Public Interface
- (instancetype)initWithTableViewStyle:(UITableViewStyle)style
appBarStyle:
(ChromeTableViewControllerStyle)appBarStyle {
......@@ -59,11 +57,24 @@ const CGFloat kTableViewSeparatorColor = 0xC8C7CC;
appBarStyle:ChromeTableViewControllerStyleNoAppBar];
}
#pragma mark - Accessors
- (void)setStyler:(ChromeTableViewStyler*)styler {
DCHECK(![self isViewLoaded]);
_styler = styler;
}
- (void)setEmptyView:(TableViewEmptyView*)emptyView {
if (_emptyView == emptyView)
return;
_emptyView = emptyView;
self.tableView.backgroundView = _emptyView;
// Since this would replace any loadingView, set it to nil.
self.loadingView = nil;
}
#pragma mark - Public
- (void)loadModel {
_tableViewModel = [[TableViewModel alloc] init];
}
......@@ -125,14 +136,17 @@ const CGFloat kTableViewSeparatorColor = 0xC8C7CC;
}
- (void)addEmptyTableViewWithMessage:(NSString*)message image:(UIImage*)image {
if (!self.emptyView) {
self.emptyView = [[TableViewEmptyView alloc] initWithFrame:self.view.bounds
message:message
image:image];
self.tableView.backgroundView = self.emptyView;
// Since this would replace any loadingView, set it to nil.
self.loadingView = nil;
}
self.emptyView = [[TableViewEmptyView alloc] initWithFrame:self.view.bounds
message:message
image:image];
}
- (void)addEmptyTableViewWithAttributedMessage:
(NSAttributedString*)attributedMessage
image:(UIImage*)image {
self.emptyView = [[TableViewEmptyView alloc] initWithFrame:self.view.bounds
attributedMessage:attributedMessage
image:image];
}
- (void)removeEmptyTableView {
......
......@@ -10,10 +10,17 @@
// Displays an UIImage on top of a message over a clearBackground.
@interface TableViewEmptyView : UIView
// Designated initializer.
// Designated initializer for a view that displays |message| with default
// styling and |image| above the message.
- (instancetype)initWithFrame:(CGRect)frame
message:(NSString*)message
image:(UIImage*)image NS_DESIGNATED_INITIALIZER;
// Designated initializer for a view that displays an attributed |message| and
// |image| above the message.
- (instancetype)initWithFrame:(CGRect)frame
attributedMessage:(NSAttributedString*)message
image:(UIImage*)image NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithCoder:(NSCoder*)aDecoder NS_UNAVAILABLE;
- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;
......
......@@ -13,11 +13,26 @@ namespace {
const float kStackViewVerticalSpacing = 23.0;
// The StackView width.
const float kStackViewWidth = 227.0;
// Returns |message| as an attributed string with default styling.
NSAttributedString* GetAttributedMessage(NSString* message) {
NSMutableParagraphStyle* paragraph_style =
[[NSMutableParagraphStyle alloc] init];
paragraph_style.lineBreakMode = NSLineBreakByWordWrapping;
paragraph_style.alignment = NSTextAlignmentCenter;
NSDictionary* default_attributes = @{
NSFontAttributeName :
[UIFont preferredFontForTextStyle:UIFontTextStyleBody],
NSForegroundColorAttributeName : [UIColor grayColor],
NSParagraphStyleAttributeName : paragraph_style
};
return [[NSAttributedString alloc] initWithString:message
attributes:default_attributes];
}
}
@interface TableViewEmptyView ()
// The message that will be displayed.
@property(nonatomic, copy) NSString* message;
@property(nonatomic, copy) NSAttributedString* message;
// The image that will be displayed.
@property(nonatomic, strong) UIImage* image;
@end
......@@ -29,10 +44,19 @@ const float kStackViewWidth = 227.0;
- (instancetype)initWithFrame:(CGRect)frame
message:(NSString*)message
image:(UIImage*)image {
self = [super initWithFrame:frame];
if (self) {
self.message = message;
self.image = image;
if (self = [super initWithFrame:frame]) {
_message = GetAttributedMessage(message);
_image = image;
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
attributedMessage:(NSAttributedString*)message
image:(UIImage*)image {
if (self = [super initWithFrame:frame]) {
_message = message;
_image = image;
}
return self;
}
......@@ -47,12 +71,8 @@ const float kStackViewWidth = 227.0;
imageView.clipsToBounds = YES;
UILabel* messageLabel = [[UILabel alloc] init];
messageLabel.text = self.message;
messageLabel.numberOfLines = 0;
messageLabel.lineBreakMode = NSLineBreakByWordWrapping;
messageLabel.textAlignment = NSTextAlignmentCenter;
messageLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
messageLabel.textColor = [UIColor grayColor];
messageLabel.attributedText = self.message;
// Vertical stack view that holds the image and message.
UIStackView* verticalStack = [[UIStackView alloc]
......
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