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) { ...@@ -59,10 +59,17 @@ typedef NS_ENUM(NSInteger, ChromeTableViewControllerStyle) {
- (void)stopLoadingIndicatorWithCompletion:(ProceduralBlock)completion; - (void)stopLoadingIndicatorWithCompletion:(ProceduralBlock)completion;
// Adds an empty table view in the center of the ChromeTableViewController which // Adds an empty table view in the center of the ChromeTableViewController which
// displays |message| with |image| on top. This will remove any existing table // displays |message| with |image| on top. |message| will be rendered using
// view background views. // default styling. This will remove any existing table view background views.
- (void)addEmptyTableViewWithMessage:(NSString*)message image:(UIImage*)image; - (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. // Removes the empty table view, if one is present.
- (void)removeEmptyTableView; - (void)removeEmptyTableView;
......
...@@ -38,8 +38,6 @@ const CGFloat kTableViewSeparatorColor = 0xC8C7CC; ...@@ -38,8 +38,6 @@ const CGFloat kTableViewSeparatorColor = 0xC8C7CC;
@synthesize styler = _styler; @synthesize styler = _styler;
@synthesize tableViewModel = _tableViewModel; @synthesize tableViewModel = _tableViewModel;
#pragma mark - Public Interface
- (instancetype)initWithTableViewStyle:(UITableViewStyle)style - (instancetype)initWithTableViewStyle:(UITableViewStyle)style
appBarStyle: appBarStyle:
(ChromeTableViewControllerStyle)appBarStyle { (ChromeTableViewControllerStyle)appBarStyle {
...@@ -59,11 +57,24 @@ const CGFloat kTableViewSeparatorColor = 0xC8C7CC; ...@@ -59,11 +57,24 @@ const CGFloat kTableViewSeparatorColor = 0xC8C7CC;
appBarStyle:ChromeTableViewControllerStyleNoAppBar]; appBarStyle:ChromeTableViewControllerStyleNoAppBar];
} }
#pragma mark - Accessors
- (void)setStyler:(ChromeTableViewStyler*)styler { - (void)setStyler:(ChromeTableViewStyler*)styler {
DCHECK(![self isViewLoaded]); DCHECK(![self isViewLoaded]);
_styler = styler; _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 { - (void)loadModel {
_tableViewModel = [[TableViewModel alloc] init]; _tableViewModel = [[TableViewModel alloc] init];
} }
...@@ -125,14 +136,17 @@ const CGFloat kTableViewSeparatorColor = 0xC8C7CC; ...@@ -125,14 +136,17 @@ const CGFloat kTableViewSeparatorColor = 0xC8C7CC;
} }
- (void)addEmptyTableViewWithMessage:(NSString*)message image:(UIImage*)image { - (void)addEmptyTableViewWithMessage:(NSString*)message image:(UIImage*)image {
if (!self.emptyView) { self.emptyView = [[TableViewEmptyView alloc] initWithFrame:self.view.bounds
self.emptyView = [[TableViewEmptyView alloc] initWithFrame:self.view.bounds message:message
message:message image:image];
image:image]; }
self.tableView.backgroundView = self.emptyView;
// Since this would replace any loadingView, set it to nil. - (void)addEmptyTableViewWithAttributedMessage:
self.loadingView = nil; (NSAttributedString*)attributedMessage
} image:(UIImage*)image {
self.emptyView = [[TableViewEmptyView alloc] initWithFrame:self.view.bounds
attributedMessage:attributedMessage
image:image];
} }
- (void)removeEmptyTableView { - (void)removeEmptyTableView {
......
...@@ -10,10 +10,17 @@ ...@@ -10,10 +10,17 @@
// Displays an UIImage on top of a message over a clearBackground. // Displays an UIImage on top of a message over a clearBackground.
@interface TableViewEmptyView : UIView @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 - (instancetype)initWithFrame:(CGRect)frame
message:(NSString*)message message:(NSString*)message
image:(UIImage*)image NS_DESIGNATED_INITIALIZER; 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)initWithCoder:(NSCoder*)aDecoder NS_UNAVAILABLE;
- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE; - (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;
......
...@@ -13,11 +13,26 @@ namespace { ...@@ -13,11 +13,26 @@ namespace {
const float kStackViewVerticalSpacing = 23.0; const float kStackViewVerticalSpacing = 23.0;
// The StackView width. // The StackView width.
const float kStackViewWidth = 227.0; 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 () @interface TableViewEmptyView ()
// The message that will be displayed. // The message that will be displayed.
@property(nonatomic, copy) NSString* message; @property(nonatomic, copy) NSAttributedString* message;
// The image that will be displayed. // The image that will be displayed.
@property(nonatomic, strong) UIImage* image; @property(nonatomic, strong) UIImage* image;
@end @end
...@@ -29,10 +44,19 @@ const float kStackViewWidth = 227.0; ...@@ -29,10 +44,19 @@ const float kStackViewWidth = 227.0;
- (instancetype)initWithFrame:(CGRect)frame - (instancetype)initWithFrame:(CGRect)frame
message:(NSString*)message message:(NSString*)message
image:(UIImage*)image { image:(UIImage*)image {
self = [super initWithFrame:frame]; if (self = [super initWithFrame:frame]) {
if (self) { _message = GetAttributedMessage(message);
self.message = message; _image = image;
self.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; return self;
} }
...@@ -47,12 +71,8 @@ const float kStackViewWidth = 227.0; ...@@ -47,12 +71,8 @@ const float kStackViewWidth = 227.0;
imageView.clipsToBounds = YES; imageView.clipsToBounds = YES;
UILabel* messageLabel = [[UILabel alloc] init]; UILabel* messageLabel = [[UILabel alloc] init];
messageLabel.text = self.message;
messageLabel.numberOfLines = 0; messageLabel.numberOfLines = 0;
messageLabel.lineBreakMode = NSLineBreakByWordWrapping; messageLabel.attributedText = self.message;
messageLabel.textAlignment = NSTextAlignmentCenter;
messageLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
messageLabel.textColor = [UIColor grayColor];
// Vertical stack view that holds the image and message. // Vertical stack view that holds the image and message.
UIStackView* verticalStack = [[UIStackView alloc] 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