commit | author | age
|
655e66
|
1 |
/* |
H |
2 |
* Copyright (C) 2017 Twitter, Inc. |
|
3 |
* |
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
* you may not use this file except in compliance with the License. |
|
6 |
* You may obtain a copy of the License at |
|
7 |
* |
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
* |
|
10 |
* Unless required by applicable law or agreed to in writing, software |
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
* See the License for the specific language governing permissions and |
|
14 |
* limitations under the License. |
|
15 |
* |
|
16 |
*/ |
a0a843
|
17 |
|
H |
18 |
#import <UIKit/UIKit.h> |
|
19 |
#import "TWTRTweetViewDelegate.h" |
|
20 |
|
|
21 |
@class TWTRTweet; |
|
22 |
|
|
23 |
NS_ASSUME_NONNULL_BEGIN |
|
24 |
|
|
25 |
/** |
|
26 |
* The style for Tweet views. |
|
27 |
*/ |
|
28 |
typedef NS_ENUM(NSUInteger, TWTRTweetViewStyle) { |
|
29 |
|
|
30 |
/** |
|
31 |
* A full-size Tweet view. Displays images if present. |
|
32 |
*/ |
|
33 |
TWTRTweetViewStyleRegular, |
|
34 |
|
|
35 |
/** |
|
36 |
* A small Tweet view, primarily designed to be used in table views. |
|
37 |
*/ |
|
38 |
TWTRTweetViewStyleCompact |
|
39 |
}; |
|
40 |
|
|
41 |
/** |
|
42 |
* A default combination of colors for Tweet views. |
|
43 |
*/ |
|
44 |
typedef NS_ENUM(NSUInteger, TWTRTweetViewTheme) { |
|
45 |
|
|
46 |
/** |
|
47 |
* Official light theme. |
|
48 |
*/ |
|
49 |
TWTRTweetViewThemeLight, |
|
50 |
|
|
51 |
/** |
|
52 |
* Official dark theme. |
|
53 |
*/ |
|
54 |
TWTRTweetViewThemeDark, |
|
55 |
}; |
|
56 |
|
|
57 |
/** |
|
58 |
`TWTRTweetView` displays a single Tweet to the user. It handles background taps and other actions displayed to the user. |
|
59 |
|
|
60 |
TWTRAPIClient *APIClient = [[TWTRAPIClient alloc] init]; |
|
61 |
[[APIClient loadTweetWithID:@"20" completion:^(TWTRTweet *tweet, NSError *error) { |
|
62 |
if (tweet) { |
|
63 |
TWTRTweetView *tweetView = [[TWTRTweetView alloc] initWithTweet:tweet]; |
|
64 |
[self.view addSubview:tweetView]; |
|
65 |
} else { |
|
66 |
NSLog(@"Error loading Tweet: %@", [error localizedDescription]); |
|
67 |
} |
|
68 |
}]; |
|
69 |
|
|
70 |
## Interaction |
|
71 |
|
|
72 |
The `TWTRTweetViewDelegate` is notified: |
|
73 |
|
|
74 |
- When the background is tapped. |
|
75 |
- When a link is selected. |
|
76 |
- When the share button is tapped. |
|
77 |
- When the share action completes. |
|
78 |
- When the favorite action completes. |
655e66
|
79 |
- When the video (if available) is paused or started to play. |
a0a843
|
80 |
|
H |
81 |
## Usage in UITableView |
|
82 |
|
|
83 |
To allow for usage in a `UITableView`, the `configureWithTweet:` method allows configuration of an existing `TWTRTweetView` without having to create a new instance. |
|
84 |
|
|
85 |
## Sizing |
|
86 |
|
|
87 |
When using Auto Layout, feel free to set a width or margin on the Tweet view. The height will be calculated automatically. For old-fashioned frame based layout you may use the standard `sizeThatFits:` method to calculate the appropriate height for a given width: |
|
88 |
|
|
89 |
// Find the height for a given width (20pts on either side) |
|
90 |
CGFloat desiredHeight = [tweetView sizeThatFits:CGSizeMake(self.view.frame.size.width - 40, CGFLOAT_MAX)].height; |
|
91 |
|
|
92 |
## UIAppearance |
|
93 |
|
|
94 |
You may use UIAppearance proxy objects to style certain aspects of Tweet views before those views are added to the view hierarchy. |
|
95 |
|
|
96 |
// Using UIAppearance Proxy |
|
97 |
[TWTRTweetView appearance].theme = TWTRTweetViewThemeDark; |
|
98 |
|
|
99 |
// Setting colors directly |
|
100 |
[TWTRTweetView appearance].primaryTextColor = [UIColor yellowColor]; |
|
101 |
[TWTRTweetView appearance].backgroundColor = [UIColor blueColor]; |
|
102 |
|
|
103 |
// Setting action button visibility |
|
104 |
[TWTRTweetView appearance].showActionButtons = NO; |
|
105 |
|
|
106 |
_Note:_ You can't change the theme through an appearance proxy after the view has already been added to the view hierarchy. Direct `theme` property access will work though. |
|
107 |
*/ |
|
108 |
@interface TWTRTweetView : UIView <UIAppearanceContainer> |
|
109 |
|
|
110 |
/** |
|
111 |
* The Tweet being displayed. |
|
112 |
*/ |
|
113 |
@property (nonatomic, readonly) TWTRTweet *tweet; |
|
114 |
|
|
115 |
/** |
|
116 |
* Background color of the Tweet view and all text labels (fullname, username, Tweet text, timestamp). |
|
117 |
*/ |
|
118 |
@property (nonatomic) UIColor *backgroundColor UI_APPEARANCE_SELECTOR; |
|
119 |
|
|
120 |
/** |
|
121 |
* Color of Tweet text and full name. |
|
122 |
*/ |
|
123 |
@property (nonatomic) UIColor *primaryTextColor UI_APPEARANCE_SELECTOR; |
|
124 |
|
|
125 |
/** |
|
126 |
* Color of links in Tweet text. |
|
127 |
*/ |
|
128 |
@property (nonatomic) UIColor *linkTextColor UI_APPEARANCE_SELECTOR; |
|
129 |
|
|
130 |
/** |
|
131 |
* Set whether the border should be shown. |
|
132 |
* Defaults to YES. |
|
133 |
*/ |
|
134 |
@property (nonatomic) BOOL showBorder UI_APPEARANCE_SELECTOR; |
655e66
|
135 |
|
H |
136 |
/** |
|
137 |
* Set whether or not videos playing inline should be muted. |
|
138 |
* Defaults to NO. |
|
139 |
*/ |
|
140 |
@property (nonatomic) BOOL shouldPlayVideoMuted; |
a0a843
|
141 |
|
H |
142 |
/** |
|
143 |
* Set whether the action buttons (Favorite, Share) should be shown. When toggled, |
|
144 |
* both the visibility of the action buttons and the internal constraints are |
|
145 |
* updated immediately. The layout will be updated the next layout pass that occurs. |
|
146 |
* |
|
147 |
* Defaults to NO. |
|
148 |
*/ |
|
149 |
@property (nonatomic) BOOL showActionButtons; |
|
150 |
|
|
151 |
/** |
|
152 |
* Setting the theme of the Tweet view will change the color properties accordingly. |
|
153 |
* |
|
154 |
* Set to `TWTRTweetViewThemeLight` by default. |
|
155 |
*/ |
|
156 |
@property (nonatomic) TWTRTweetViewTheme theme; |
|
157 |
|
|
158 |
/** |
|
159 |
* The style of the Tweet. i.e. `TWTRTweetViewStyleRegular` or `TWTRTweetViewStyleCompact`. |
|
160 |
*/ |
|
161 |
@property (nonatomic, readonly) TWTRTweetViewStyle style; |
|
162 |
|
|
163 |
/** |
|
164 |
* Optional delegate to receive notifications when certain actions happen |
|
165 |
*/ |
|
166 |
@property (nonatomic, weak) IBOutlet id<TWTRTweetViewDelegate> delegate; |
|
167 |
|
|
168 |
/** |
|
169 |
* Optional property to set a UIViewController from which to present various new UI |
|
170 |
* e.g. when presenting a Share sheet, presenting a login view controller for actions, etc |
|
171 |
*/ |
|
172 |
@property (nonatomic, weak) UIViewController *presenterViewController; |
|
173 |
|
|
174 |
/** |
|
175 |
* Convenience initializer to configure a compact style Tweet view. |
|
176 |
* |
|
177 |
* @param tweet The Tweet to display. |
|
178 |
* |
|
179 |
* @return The fully-configured Tweet view. |
|
180 |
*/ |
|
181 |
- (instancetype)initWithTweet:(nullable TWTRTweet *)tweet; |
|
182 |
|
|
183 |
/** |
|
184 |
* Designated initializer. Initializes view with both Tweet and style. |
|
185 |
* |
|
186 |
* @param tweet The Tweet to display. |
|
187 |
* @param style The style of the Tweet view (regular or compact). |
|
188 |
* |
|
189 |
* @return The fully configured Tweet view. |
|
190 |
*/ |
|
191 |
- (instancetype)initWithTweet:(nullable TWTRTweet *)tweet style:(TWTRTweetViewStyle)style; |
|
192 |
|
|
193 |
/** |
|
194 |
* Initialization with a frame parameter is not supported. |
|
195 |
*/ |
|
196 |
- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE; |
|
197 |
|
|
198 |
/** |
|
199 |
Find the size that fits into a desired space. This is a system method on UIView but implemented on `TWTRTweetView` |
|
200 |
|
|
201 |
// Calculate the desired height at 280 points wide |
|
202 |
CGSize desiredSize = [tweetView sizeThatFits:CGSizeMake(280, CGFLOAT_MAX)]; |
|
203 |
|
|
204 |
|
|
205 |
@param size The space available. Should generally leave one orientation unconstrained, and the minimum width supported is 200pts. |
|
206 |
|
|
207 |
@return The size that will fit into the space available. |
|
208 |
*/ |
|
209 |
- (CGSize)sizeThatFits:(CGSize)size; |
|
210 |
|
|
211 |
/** |
|
212 |
* Update all images and label text to fully represent the given Tweet. |
|
213 |
* |
|
214 |
* @param tweet The Tweet to display. |
|
215 |
*/ |
|
216 |
- (void)configureWithTweet:(nullable TWTRTweet *)tweet; |
|
217 |
|
655e66
|
218 |
/** |
H |
219 |
* If the tweet contains playable media, calling this function will play the media. The media will also play if |
|
220 |
* the user taps on the play button for the media. |
|
221 |
*/ |
|
222 |
- (void)playVideo; |
|
223 |
|
|
224 |
/** |
|
225 |
* If the tweet contains media that is currently playing, this function will pause the current video. |
|
226 |
* |
|
227 |
* If a TWTRTweetVideo is being added to a UICollectionView, implement the delegate collectionView:didEndDisplayingCell:forItemAtIndexPath: |
|
228 |
* and call pauseVideo here so videos stop playing when the user scrolls off the screen. |
|
229 |
*/ |
|
230 |
- (void)pauseVideo; |
|
231 |
|
a0a843
|
232 |
@end |
H |
233 |
|
|
234 |
NS_ASSUME_NONNULL_END |