-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
BootstrapText.java
127 lines (109 loc) · 4.54 KB
/
BootstrapText.java
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
package com.beardedhen.androidbootstrap;
import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import com.beardedhen.androidbootstrap.font.AwesomeTypefaceSpan;
import com.beardedhen.androidbootstrap.font.FontAwesome;
import com.beardedhen.androidbootstrap.font.IconSet;
import com.beardedhen.androidbootstrap.font.MaterialIcons;
import com.beardedhen.androidbootstrap.font.Typicon;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* Bootstrap Text provides a Builder class, which allows convenient construction of SpannableStrings.
* Currently regular text, FontAwesome icons, and Typicons can be added.
*/
public class BootstrapText extends SpannableString implements Serializable {
private BootstrapText(CharSequence source) {
super(source);
}
/**
* This class should be used to construct BootstrapText instances. Text is appended to itself
* in the order in which it was added.
*/
public static class Builder {
private final StringBuilder sb;
private final Context context;
private final boolean editMode;
private final Map<Integer, IconSet> fontIndicesMap;
public Builder(Context context) {
fontIndicesMap = new HashMap<>();
sb = new StringBuilder();
this.context = context.getApplicationContext();
this.editMode = false;
}
public Builder(Context context, boolean editMode) {
fontIndicesMap = new HashMap<>();
sb = new StringBuilder();
this.context = context.getApplicationContext();
this.editMode = editMode;
}
/**
* Appends a regular piece of text to the BootstrapText under construction.
*
* @param text a regular piece of text
* @return the updated builder instance
*/
public Builder addText(CharSequence text) {
sb.append(text);
return this;
}
/**
* Appends a FontAwesomeIcon to the BootstrapText under construction
*
* @return the updated builder instance
*/
public Builder addFontAwesomeIcon(@FontAwesome.Icon CharSequence iconCode) {
IconSet iconSet = TypefaceProvider.retrieveRegisteredIconSet(FontAwesome.FONT_PATH, editMode);
sb.append(iconSet.unicodeForKey(iconCode.toString().replaceAll("\\-", "_")));
fontIndicesMap.put(sb.length(), iconSet);
return this;
}
/**
* Appends a Typicon to the BootstrapText under construction
*
* @return the updated builder instance
*/
public Builder addTypicon(@Typicon.Icon CharSequence iconCode) {
IconSet iconSet = TypefaceProvider.retrieveRegisteredIconSet(Typicon.FONT_PATH, editMode);
sb.append(iconSet.unicodeForKey(iconCode.toString().replaceAll("\\-", "_")));
fontIndicesMap.put(sb.length(), iconSet);
return this;
}
/**
* Appends a Typicon to the BootstrapText under construction
*
* @return the updated builder instance
*/
public Builder addMaterialIcon( CharSequence iconCode) {
IconSet iconSet = TypefaceProvider.retrieveRegisteredIconSet(MaterialIcons.FONT_PATH, editMode);
sb.append(iconSet.unicodeForKey(iconCode.toString().replaceAll("\\-", "_")));
fontIndicesMap.put(sb.length(), iconSet);
return this;
}
/**
* Appends a font icon to the BootstrapText under construction
*
* @param iconSet a font icon
* @return the updated builder instance
*/
public Builder addIcon(CharSequence iconCode, IconSet iconSet) {
sb.append(iconSet.unicodeForKey(iconCode.toString().replaceAll("\\-", "_")));
fontIndicesMap.put(sb.length(), iconSet);
return this;
}
/**
* @return a new instance of BootstrapText, constructed according to Builder arguments.
*/
public BootstrapText build() {
BootstrapText bootstrapText = new BootstrapText(sb.toString());
for (Map.Entry<Integer, IconSet> entry : fontIndicesMap.entrySet()) {
int index = entry.getKey();
AwesomeTypefaceSpan span = new AwesomeTypefaceSpan(context, entry.getValue());
bootstrapText.setSpan(span, index - 1, index, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
return bootstrapText;
}
}
}