Skip to content

Commit 92ee9d0

Browse files
committed
下载页面布局 + RFMarkdownTextView
1 parent b5b94e3 commit 92ee9d0

9 files changed

Lines changed: 646 additions & 20 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// RFKeyboardToolbar.h
3+
//
4+
// Created by Rudd Fawcett on 12/3/13.
5+
// Copyright (c) 2013 Rudd Fawcett. All rights reserved.
6+
//
7+
8+
#import <UIKit/UIKit.h>
9+
#include <AvailabilityMacros.h>
10+
11+
#import "RFToolbarButton.h"
12+
13+
@interface RFKeyboardToolbar : UIView
14+
15+
/**
16+
* The buttons of the toolbar.
17+
*/
18+
@property (nonatomic, strong) NSArray *buttons;
19+
20+
/**
21+
* Creates a new toolbar.
22+
*
23+
* @param buttons The buttons to draw in the view.
24+
*
25+
* @return A RFKeyboardToolbar.
26+
*/
27+
+ (instancetype)toolbarWithButtons:(NSArray *)buttons;
28+
29+
/**
30+
* Creates a new toolbar.
31+
*
32+
* @param buttons The buttons to draw in the view.
33+
*
34+
* @return A RFKeyboardToolbar.
35+
*/
36+
+ (instancetype)toolbarViewWithButtons:(NSArray *)buttons DEPRECATED_MSG_ATTRIBUTE("This will still work, but there's a shorter method available, toolbarWithButtons:");
37+
38+
/**
39+
*
40+
*
41+
* @param buttons Sets the buttons for the toolbar.
42+
* @param animated Whether or not it should be animated.
43+
*/
44+
- (void)setButtons:(NSArray *)buttons animated:(BOOL)animated;
45+
46+
@end
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
//
2+
// RFKeyboardToolbar.m
3+
//
4+
// Created by Rudd Fawcett on 12/3/13.
5+
// Copyright (c) 2013 Rudd Fawcett. All rights reserved.
6+
//
7+
8+
#import "RFKeyboardToolbar.h"
9+
10+
@interface RFKeyboardToolbar ()
11+
12+
/**
13+
* The toolbar view.
14+
*/
15+
@property (nonatomic,strong) UIView *toolbarView;
16+
/**
17+
* The scroll view that's faked to look like a toolbar.
18+
*/
19+
@property (nonatomic,strong) UIScrollView *scrollView;
20+
/**
21+
* The fake top border to replicate the toolbar.
22+
*/
23+
@property (nonatomic,strong) CALayer *topBorder;
24+
25+
@end
26+
27+
@implementation RFKeyboardToolbar
28+
29+
+ (instancetype)toolbarWithButtons:(NSArray *)buttons {
30+
return [[RFKeyboardToolbar alloc] initWithButtons:buttons];
31+
}
32+
33+
+ (instancetype)toolbarViewWithButtons:(NSArray *)buttons {
34+
return [[RFKeyboardToolbar alloc] initWithButtons:buttons];
35+
}
36+
37+
- (id)initWithButtons:(NSArray *)buttons {
38+
self = [super initWithFrame:CGRectMake(0, 0, self.window.rootViewController.view.bounds.size.width, 40)];
39+
if (self) {
40+
_buttons = [buttons copy];
41+
42+
self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
43+
[self addSubview:[self inputAccessoryView]];
44+
}
45+
return self;
46+
}
47+
48+
- (void)layoutSubviews {
49+
CGRect frame = _toolbarView.bounds;
50+
frame.size.height = 0.5f;
51+
52+
_topBorder.frame = frame;
53+
}
54+
55+
- (UIView *)inputAccessoryView {
56+
_toolbarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40)];
57+
_toolbarView.backgroundColor = [UIColor colorWithWhite:0.973 alpha:1.0];
58+
_toolbarView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
59+
60+
_topBorder = [CALayer layer];
61+
_topBorder.frame = CGRectMake(0.0f, 0.0f, self.bounds.size.width, 0.5f);
62+
_topBorder.backgroundColor = [UIColor colorWithWhite:0.678 alpha:1.0].CGColor;
63+
64+
[_toolbarView.layer addSublayer:_topBorder];
65+
[_toolbarView addSubview:[self fakeToolbar]];
66+
67+
return _toolbarView;
68+
}
69+
70+
- (UIScrollView *)fakeToolbar {
71+
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40)];
72+
_scrollView.backgroundColor = [UIColor clearColor];
73+
_scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
74+
_scrollView.showsHorizontalScrollIndicator = NO;
75+
_scrollView.contentInset = UIEdgeInsetsMake(6.0f, 0.0f, 8.0f, 6.0f);
76+
77+
[self addButtons];
78+
79+
return _scrollView;
80+
}
81+
82+
- (void)addButtons {
83+
NSUInteger index = 0;
84+
NSUInteger originX = 8;
85+
86+
CGRect originFrame;
87+
88+
for (RFToolbarButton *eachButton in _buttons) {
89+
originFrame = CGRectMake(originX, 0, eachButton.frame.size.width, eachButton.frame.size.height);
90+
eachButton.frame = originFrame;
91+
92+
[_scrollView addSubview:eachButton];
93+
94+
originX = originX + eachButton.bounds.size.width + 8;
95+
index++;
96+
}
97+
98+
CGSize contentSize = _scrollView.contentSize;
99+
contentSize.width = originX - 8;
100+
_scrollView.contentSize = contentSize;
101+
}
102+
103+
- (void)setButtons:(NSArray *)buttons {
104+
[_buttons makeObjectsPerformSelector:@selector(removeFromSuperview)];
105+
_buttons = [buttons copy];
106+
[self addButtons];
107+
}
108+
109+
- (void)setButtons:(NSArray *)buttons animated:(BOOL)animated {
110+
if (!animated) {
111+
self.buttons = buttons;
112+
return;
113+
}
114+
115+
NSMutableSet *removeButtons = [NSMutableSet setWithArray:_buttons];
116+
[removeButtons minusSet:[NSSet setWithArray:buttons]];
117+
NSMutableSet *addButtons = [NSMutableSet setWithArray:buttons];
118+
[addButtons minusSet:[NSSet setWithArray:_buttons]];
119+
_buttons = [buttons copy];
120+
121+
// calculate end frames
122+
NSUInteger originX = 8;
123+
NSUInteger index = 0;
124+
NSMutableArray *buttonFrames = [NSMutableArray arrayWithCapacity:_buttons.count];
125+
126+
for (RFToolbarButton *button in _buttons) {
127+
CGRect frame = CGRectMake(originX, 0, button.frame.size.width, button.frame.size.height);
128+
[buttonFrames addObject:[NSValue valueWithCGRect:frame]];
129+
130+
originX += button.bounds.size.width + 8;
131+
index++;
132+
}
133+
134+
CGSize contentSize = _scrollView.contentSize;
135+
contentSize.width = originX - 8;
136+
if (contentSize.width > _scrollView.contentSize.width) {
137+
_scrollView.contentSize = contentSize;
138+
}
139+
140+
// make added buttons appear from the right
141+
[addButtons enumerateObjectsUsingBlock:^(RFToolbarButton *button, BOOL *stop) {
142+
button.frame = CGRectMake(originX, 0, button.frame.size.width, button.frame.size.height);
143+
[_scrollView addSubview:button];
144+
}];
145+
146+
// animate
147+
[UIView animateWithDuration:0.2 animations:^{
148+
[removeButtons enumerateObjectsUsingBlock:^(RFToolbarButton *button, BOOL *stop) {
149+
button.alpha = 0;
150+
}];
151+
152+
[_buttons enumerateObjectsUsingBlock:^(RFToolbarButton *button, NSUInteger idx, BOOL *stop) {
153+
button.frame = [buttonFrames[idx] CGRectValue];
154+
}];
155+
156+
_scrollView.contentSize = contentSize;
157+
} completion:^(BOOL finished) {
158+
[removeButtons makeObjectsPerformSelector:@selector(removeFromSuperview)];
159+
}];
160+
}
161+
162+
@end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// RFToolbarButton.h
3+
//
4+
// Created by Rudd Fawcett on 12/3/13.
5+
// Copyright (c) 2013 Rudd Fawcett. All rights reserved.
6+
//
7+
8+
#import <UIKit/UIKit.h>
9+
10+
/**
11+
* The block used for each button.
12+
*/
13+
typedef void (^eventHandlerBlock)();
14+
15+
@interface RFToolbarButton : UIButton
16+
17+
/**
18+
* Creates a new RFToolbarButton.
19+
*
20+
* @param title The string to show on the button.
21+
*
22+
* @return A new button.
23+
*/
24+
+ (instancetype)buttonWithTitle:(NSString *)title;
25+
26+
/**
27+
* Creates a new RFToolbarButton.
28+
*
29+
* @param title The string to show on the button.
30+
* @param eventHandler The event handler block.
31+
* @param controlEvent The type of event.
32+
*
33+
* @return A new button.
34+
*/
35+
+ (instancetype)buttonWithTitle:(NSString *)title andEventHandler:(eventHandlerBlock)eventHandler forControlEvents:(UIControlEvents)controlEvent;
36+
37+
/**
38+
* Adds the event handler for the button.
39+
*
40+
* @param eventHandler The event handler block.
41+
* @param controlEvent The type of event.
42+
*/
43+
- (void)addEventHandler:(eventHandlerBlock)eventHandler forControlEvents:(UIControlEvents)controlEvent;
44+
45+
@end
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// RFToolbarButton.m
3+
//
4+
// Created by Rudd Fawcett on 12/3/13.
5+
// Copyright (c) 2013 Rudd Fawcett. All rights reserved.
6+
//
7+
8+
#import "RFToolbarButton.h"
9+
10+
@interface RFToolbarButton ()
11+
12+
/**
13+
* The button's title.
14+
*/
15+
@property (nonatomic, strong) NSString *title;
16+
17+
/**
18+
* The button's event block.
19+
*/
20+
@property (nonatomic, copy) eventHandlerBlock buttonPressBlock;
21+
22+
@end
23+
24+
@implementation RFToolbarButton
25+
26+
+ (instancetype)buttonWithTitle:(NSString *)title {
27+
return [[self alloc] initWithTitle:title];
28+
}
29+
30+
+ (instancetype)buttonWithTitle:(NSString *)title andEventHandler:(eventHandlerBlock)eventHandler forControlEvents:(UIControlEvents)controlEvent {
31+
RFToolbarButton *newButton = [RFToolbarButton buttonWithTitle:title];
32+
[newButton addEventHandler:eventHandler forControlEvents:controlEvent];
33+
34+
return newButton;
35+
}
36+
37+
- (id)initWithTitle:(NSString *)title {
38+
_title = title;
39+
return [self init];
40+
}
41+
42+
- (id)init {
43+
CGSize sizeOfText = [self.title sizeWithAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14.f]}];
44+
45+
if (self = [super initWithFrame:CGRectMake(0, 0, sizeOfText.width + 18.104, sizeOfText.height + 10.298)]) {
46+
self.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.0];
47+
48+
self.layer.cornerRadius = 3.0f;
49+
self.layer.borderWidth = 1.0f;
50+
self.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:1.0].CGColor;
51+
52+
[self setTitle:self.title forState:UIControlStateNormal];
53+
[self setTitleColor:[UIColor colorWithWhite:0.500 alpha:1.0] forState:UIControlStateNormal];
54+
55+
self.titleLabel.font = [UIFont boldSystemFontOfSize:14.f];
56+
self.titleLabel.textColor = [UIColor colorWithWhite:0.500 alpha:1.0];
57+
}
58+
return self;
59+
}
60+
61+
- (void)addEventHandler:(eventHandlerBlock)eventHandler forControlEvents:(UIControlEvents)controlEvent {
62+
self.buttonPressBlock = eventHandler;
63+
[self addTarget:self action:@selector(buttonPressed) forControlEvents:controlEvent];
64+
}
65+
66+
- (void)buttonPressed {
67+
self.buttonPressBlock();
68+
}
69+
70+
@end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// RFMarkdownSyntaxStorage.h
3+
// RFMarkdownTextViewDemo
4+
//
5+
// Created by Rudd Fawcett on 12/6/13.
6+
// Copyright (c) 2013 Rudd Fawcett. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface RFMarkdownSyntaxStorage : NSTextStorage
12+
13+
- (void)update;
14+
15+
@end

0 commit comments

Comments
 (0)