This repository was archived by the owner on Jul 2, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathStringElement.cs
More file actions
303 lines (260 loc) · 9.87 KB
/
Copy pathStringElement.cs
File metadata and controls
303 lines (260 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using UIKit;
using Foundation;
using SDWebImage;
using ReactiveUI;
namespace CodeHub.iOS.DialogElements
{
public class StringElement : Element
{
private static NSString Key = new NSString("StringElement");
public UITextAlignment Alignment = UITextAlignment.Left;
public string _value;
public event Action Tapped;
public string Value
{
get { return _value; }
set
{
_value = value;
var cell = GetActiveCell();
if (cell != null)
cell.DetailTextLabel.Text = value ?? string.Empty;
}
}
public StringElement (string caption)
: this(caption, null)
{
this.Caption = caption;
}
public StringElement (string caption, string value, IReactiveCommand tapped)
: this(caption, value, () => tapped.ExecuteIfCan())
{
}
public StringElement (string caption, string value, Action tapped = null)
{
this.Caption = caption;
this.Value = value;
if (tapped != null)
Tapped += tapped;
}
public override UITableViewCell GetCell (UITableView tv)
{
var cell = tv.DequeueReusableCell (Key);
if (cell == null)
{
cell = new UITableViewCell (Value == null ? UITableViewCellStyle.Default : UITableViewCellStyle.Value1, Key);
cell.SelectionStyle = (Tapped != null) ? UITableViewCellSelectionStyle.Blue : UITableViewCellSelectionStyle.None;
}
cell.Accessory = UITableViewCellAccessory.None;
cell.TextLabel.Text = Caption;
cell.TextLabel.TextAlignment = Alignment;
if (cell.DetailTextLabel != null)
cell.DetailTextLabel.Text = Value ?? "";
return cell;
}
public override void Selected (UITableView tableView, NSIndexPath indexPath)
{
base.Selected(tableView, indexPath);
if (Tapped != null)
Tapped ();
}
public override bool Matches (string text)
{
return (Value != null && Value.IndexOf(text, StringComparison.CurrentCultureIgnoreCase) != -1) || base.Matches (text);
}
}
/// <summary>
/// A version of the StringElement that can be styled with a number of formatting
/// options and can render images or background images either from UIImage parameters
/// or by downloading them from the net.
/// </summary>
public class StyledStringElement : StringElement
{
public static UIFont DefaultTitleFont = UIFont.PreferredBody;
public static UIFont DefaultDetailFont = UIFont.PreferredBody;
public static UIColor DefaultTitleColor = UIColor.FromRGB(41, 41, 41);
public static UIColor DefaultDetailColor = UIColor.FromRGB(80, 80, 80);
public static UIColor BgColor = UIColor.White;
static NSString [] skey = { new NSString (".1"), new NSString (".2"), new NSString (".3"), new NSString (".4") };
public StyledStringElement (string caption) : base (caption)
{
Init();
}
public StyledStringElement (string caption, Action tapped) : base (caption, null, tapped)
{
Accessory = UITableViewCellAccessory.DisclosureIndicator;
Init();
}
public StyledStringElement (string caption, Action tapped, UIImage image) : base (caption, null, tapped)
{
Accessory = UITableViewCellAccessory.DisclosureIndicator;
Init();
Image = image;
}
public StyledStringElement (string caption, string value) : base (caption, value)
{
style = UITableViewCellStyle.Value1;
Init();
}
public StyledStringElement (string caption, string value, UITableViewCellStyle style) : base (caption, value)
{
this.style = style;
Init();
}
protected UITableViewCellStyle style;
public event Action AccessoryTapped;
public UIFont Font;
public UIFont SubtitleFont;
public UIColor TextColor;
public UILineBreakMode LineBreakMode = UILineBreakMode.WordWrap;
public int Lines = 1;
public UITableViewCellAccessory Accessory = UITableViewCellAccessory.None;
private void Init()
{
Font = DefaultTitleFont.WithSize(DefaultTitleFont.PointSize);
SubtitleFont = DefaultDetailFont.WithSize(DefaultDetailFont.PointSize);
BackgroundColor = BgColor;
TextColor = DefaultTitleColor;
DetailColor = DefaultDetailColor;
LineBreakMode = UILineBreakMode.TailTruncation;
}
// To keep the size down for a StyleStringElement, we put all the image information
// on a separate structure, and create this on demand.
ExtraInfo extraInfo;
class ExtraInfo {
public UIImage Image; // Maybe add BackgroundImage?
public UIColor BackgroundColor, DetailColor;
public Uri Uri;
}
ExtraInfo OnImageInfo ()
{
if (extraInfo == null)
extraInfo = new ExtraInfo ();
return extraInfo;
}
// Uses the specified image (use this or ImageUri)
public UIImage Image {
get {
return extraInfo == null ? null : extraInfo.Image;
}
set {
OnImageInfo ().Image = value;
extraInfo.Uri = null;
}
}
// Loads the image from the specified uri (use this or Image)
public Uri ImageUri {
get {
return extraInfo == null ? null : extraInfo.Uri;
}
set {
OnImageInfo ().Uri = value;
extraInfo.Image = null;
}
}
// Background color for the cell (alternative: BackgroundUri)
public UIColor BackgroundColor {
get {
return extraInfo == null ? null : extraInfo.BackgroundColor;
}
set {
OnImageInfo ().BackgroundColor = value;
}
}
public UIColor DetailColor {
get {
return extraInfo == null ? null : extraInfo.DetailColor;
}
set {
OnImageInfo ().DetailColor = value;
}
}
protected virtual string GetKey (int style)
{
return skey [style];
}
protected virtual void OnCellCreated(UITableViewCell cell)
{
}
protected virtual UITableViewCell CreateTableViewCell(UITableViewCellStyle style, string key)
{
return new UITableViewCell (style, key);
}
public override UITableViewCell GetCell (UITableView tv)
{
var key = GetKey ((int) style);
var cell = tv.DequeueReusableCell (key);
if (cell == null){
cell = CreateTableViewCell(style, key);
OnCellCreated(cell);
cell.SelectionStyle = UITableViewCellSelectionStyle.Blue;
}
PrepareCell (cell);
ClearBackground(cell);
if (extraInfo != null)
{
if (extraInfo.BackgroundColor != null)
{
cell.BackgroundColor = extraInfo.BackgroundColor;
cell.TextLabel.BackgroundColor = UIColor.Clear;
}
}
return cell;
}
protected void PrepareCell (UITableViewCell cell)
{
cell.Accessory = Accessory;
var tl = cell.TextLabel;
tl.Text = Caption;
tl.TextAlignment = Alignment;
tl.TextColor = TextColor ?? UIColor.Black;
tl.Font = Font ?? UIFont.BoldSystemFontOfSize (17);
tl.LineBreakMode = LineBreakMode;
tl.Lines = Lines;
// The check is needed because the cell might have been recycled.
if (cell.DetailTextLabel != null)
cell.DetailTextLabel.Text = Value == null ? "" : Value;
if (extraInfo == null){
ClearBackground (cell);
} else {
var imgView = cell.ImageView;
if (imgView != null) {
if (extraInfo.Uri != null)
{
imgView.SetImage(new NSUrl(extraInfo.Uri.AbsoluteUri));
}
else if (extraInfo.Image != null)
{
imgView.Image = extraInfo.Image;
}
else
{
imgView.Image = null;
}
}
if (cell.DetailTextLabel != null)
cell.DetailTextLabel.TextColor = extraInfo.DetailColor ?? UIColor.Gray;
}
if (cell.DetailTextLabel != null){
cell.DetailTextLabel.Lines = Lines;
cell.DetailTextLabel.LineBreakMode = LineBreakMode;
cell.DetailTextLabel.Font = SubtitleFont ?? UIFont.SystemFontOfSize (14);
cell.DetailTextLabel.TextColor = (extraInfo == null || extraInfo.DetailColor == null) ? UIColor.Gray : extraInfo.DetailColor;
}
}
void ClearBackground (UITableViewCell cell)
{
cell.BackgroundColor = UIColor.White;
cell.TextLabel.BackgroundColor = UIColor.Clear;
if (cell.DetailTextLabel != null)
cell.DetailTextLabel.BackgroundColor = UIColor.Clear;
}
internal void AccessoryTap ()
{
var tapped = AccessoryTapped;
if (tapped != null)
tapped ();
}
}
}

