Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Commit

Permalink
show info on navigation bar according to NS_OPTIONS(DZNWebInfoOnNavig…
Browse files Browse the repository at this point in the history
…ationBar) instead of using showPageTitleAndURL
  • Loading branch information
hesyifei committed Jul 7, 2016
1 parent b31a8b6 commit 5e75217
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
14 changes: 12 additions & 2 deletions Source/Classes/DZNWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ typedef NS_OPTIONS(NSUInteger, DZNsupportedWebActions) {
DZNWebActionOpenDolphin = (1 << 6),
};

/**
Types of information to be shown on navigation bar
*/
typedef NS_OPTIONS(NSUInteger, DZNWebInfoOnNavigationBar) {
DZNWebInfoOnNavigationBarAll = -1,
DZNWebInfoOnNavigationBarNone = 0,
DZNWebInfoOnNavigationBarTitle = (1 << 0),
DZNWebInfoOnNavigationBarURL = (1 << 1),
};

/**
A very simple web browser with useful navigation and tooling features.
*/
Expand All @@ -52,14 +62,14 @@ typedef NS_OPTIONS(NSUInteger, DZNsupportedWebActions) {
@property (nonatomic, readwrite) DZNWebNavigationTools supportedWebNavigationTools;
/** The supported actions like sharing and copy link, add to reading list, open in Safari, etc. Default is All. */
@property (nonatomic, readwrite) DZNsupportedWebActions supportedWebActions;
/** The information to be shown on navigation bar. */
@property (nonatomic, readwrite) DZNWebInfoOnNavigationBar infoOnNavigationBar;
/** Yes if a progress bar indicates the . Default is YES. */
@property (nonatomic) BOOL showLoadingProgress;
/** YES if long pressing the backward and forward buttons the navigation history is displayed. Default is YES. */
@property (nonatomic) BOOL allowHistory;
/** YES if both, the navigation and tool bars should hide when panning vertically. Default is YES. */
@property (nonatomic) BOOL hideBarsWithGestures;
/** YES if should set the title automatically based on the page title and URL. Default is YES. */
@property (nonatomic) BOOL showPageTitleAndURL;

///------------------------------------------------
/// @name Initialization
Expand Down
32 changes: 24 additions & 8 deletions Source/Classes/DZNWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ - (void)commonInit
{
self.supportedWebNavigationTools = DZNWebNavigationToolAll;
self.supportedWebActions = DZNWebActionAll;
self.infoOnNavigationBar = DZNWebInfoOnNavigationBarAll;
self.showLoadingProgress = YES;
self.hideBarsWithGestures = YES;
self.allowHistory = YES;
self.showPageTitleAndURL = YES;

self.webView = [[DZNWebView alloc] initWithFrame:self.view.bounds configuration:[WKWebViewConfiguration new]];
self.webView.backgroundColor = [UIColor whiteColor];
Expand Down Expand Up @@ -362,7 +362,7 @@ - (void)setURL:(NSURL *)URL

- (void)setTitle:(NSString *)title
{
if (!self.showPageTitleAndURL) {
if (self.infoOnNavigationBar == 0) {

This comment has been minimized.

Copy link
@dzenbot

dzenbot Jul 7, 2016

Owner

DZNWebInfoOnNavigationBarNone perhaps?

[super setTitle:title];
return;
}
Expand All @@ -388,17 +388,28 @@ - (void)setTitle:(NSString *)title
UIFont *urlFont = [UIFont fontWithName:titleFont.fontName size:titleFont.pointSize-2.0];
UIColor *textColor = self.navigationBar.titleTextAttributes[NSForegroundColorAttributeName] ?: [UIColor blackColor];

NSMutableString *text = [NSMutableString stringWithString:title];
NSMutableString *text = [NSMutableString stringWithString: @""];
if ((_infoOnNavigationBar & DZNWebInfoOnNavigationBarTitle) > 0 || self.showAllInfoOnNavigationBar) {
[text appendFormat:@"%@", title];
}

if (self.showAllInfoOnNavigationBar) {
[text appendFormat:@"\n"];
}

if (url.length > 0) {
[text appendFormat:@"\n%@", url];
if ((_infoOnNavigationBar & DZNWebInfoOnNavigationBarURL) > 0 || self.showAllInfoOnNavigationBar) {
if (url.length > 0) {
[text appendFormat:@"%@", url];

This comment has been minimized.

Copy link
@dzenbot

dzenbot Jul 7, 2016

Owner

You could probably add the line break here instead: @"\n%@"

}
}

NSDictionary *attributes = @{NSFontAttributeName: titleFont, NSForegroundColorAttributeName: textColor};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text attributes:attributes];

if (url.length > 0) {
[attributedString addAttribute:NSFontAttributeName value:urlFont range:[text rangeOfString:url]];
if ((_infoOnNavigationBar & DZNWebInfoOnNavigationBarURL) > 0 || self.showAllInfoOnNavigationBar) {
if (url.length > 0) {
[attributedString addAttribute:NSFontAttributeName value:urlFont range:[text rangeOfString:url]];
}
}

label.attributedText = attributedString;
Expand All @@ -423,6 +434,11 @@ - (void)setLoadingError:(NSError *)error
[alert show];
}

- (BOOL)showAllInfoOnNavigationBar
{
return (_infoOnNavigationBar == DZNWebInfoOnNavigationBarAll) ? YES : NO;
}


#pragma mark - DZNWebViewController methods

Expand Down Expand Up @@ -673,7 +689,7 @@ - (void)webView:(DZNWebView *)webView didUpdateProgress:(CGFloat)progress

- (void)webView:(DZNWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
if (self.showPageTitleAndURL) {
if (self.infoOnNavigationBar > 0) {

This comment has been minimized.

Copy link
@dzenbot

dzenbot Jul 7, 2016

Owner

Same here. > DZNWebInfoOnNavigationBarNone?

self.title = self.webView.title;
}
}
Expand Down

0 comments on commit 5e75217

Please sign in to comment.