Skip to content

Commit a753fc2

Browse files
m1gahansemannn
andauthored
feat: support for AttributedString objects (#14402)
* feat: support for AttributedString objects * ios part * docs * Update iphone/Classes/TiUIButton.m Co-authored-by: Hans Knöchel <hansemannn@users.noreply.github.com> * Update iphone/Classes/TiUIiOSButtonConfigurationProxy.m Co-authored-by: Hans Knöchel <hansemannn@users.noreply.github.com> * updates --------- Co-authored-by: Hans Knöchel <hansemannn@users.noreply.github.com>
1 parent 5198d50 commit a753fc2

File tree

16 files changed

+161
-59
lines changed

16 files changed

+161
-59
lines changed

android/modules/ui/src/java/ti/modules/titanium/ui/AttributedStringProxy.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ public AttributedStringProxy()
106106
{
107107
}
108108

109+
public static AttributedStringProxy createFromProperties(Object properties)
110+
{
111+
if (properties instanceof AttributedStringProxy) {
112+
return (AttributedStringProxy) properties;
113+
}
114+
if (properties instanceof HashMap<?, ?>) {
115+
AttributedStringProxy proxy = new AttributedStringProxy();
116+
KrollDict d = (properties instanceof KrollDict)
117+
? (KrollDict) properties : new KrollDict((HashMap<String, Object>) properties);
118+
proxy.handleCreationDict(d);
119+
return proxy;
120+
}
121+
return null;
122+
}
123+
109124
@Kroll.method
110125
public void addAttribute(Object attr)
111126
{

android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIButton.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,10 @@ public void processProperties(KrollDict d)
166166
btn.setText(d.getString(TiC.PROPERTY_TITLE));
167167
}
168168
if (d.containsKey(TiC.PROPERTY_ATTRIBUTED_STRING)) {
169-
Object attributedString = d.get(TiC.PROPERTY_ATTRIBUTED_STRING);
170-
if (attributedString instanceof AttributedStringProxy) {
171-
setAttributedStringText((AttributedStringProxy) attributedString);
169+
Object attr = d.get(TiC.PROPERTY_ATTRIBUTED_STRING);
170+
AttributedStringProxy proxy = AttributedStringProxy.createFromProperties(attr);
171+
if (proxy != null) {
172+
setAttributedStringText(proxy);
172173
}
173174
}
174175
if (d.containsKey(TiC.PROPERTY_COLOR) || d.containsKey(TiC.PROPERTY_TINT_COLOR)) {
@@ -223,8 +224,11 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
223224
AppCompatButton btn = (AppCompatButton) getNativeView();
224225
if (key.equals(TiC.PROPERTY_TITLE)) {
225226
btn.setText((String) newValue);
226-
} else if (key.equals(TiC.PROPERTY_ATTRIBUTED_STRING) && newValue instanceof AttributedStringProxy) {
227-
setAttributedStringText((AttributedStringProxy) newValue);
227+
} else if (key.equals(TiC.PROPERTY_ATTRIBUTED_STRING)) {
228+
AttributedStringProxy asProxy = AttributedStringProxy.createFromProperties(newValue);
229+
if (asProxy != null) {
230+
setAttributedStringText(asProxy);
231+
}
228232
} else if (key.equals(TiC.PROPERTY_COLOR)) {
229233
String colorString = TiConvert.toString(newValue);
230234
btn.setTextColor((colorString != null) ? TiConvert.toColor(colorString, activity) : this.defaultColor);

android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUILabel.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,10 @@ public void processProperties(KrollDict d)
343343
boolean hasProperty = false;
344344
if (d.containsKey(TiC.PROPERTY_ATTRIBUTED_STRING)) {
345345
hasProperty = true;
346-
Object attributedString = d.get(TiC.PROPERTY_ATTRIBUTED_STRING);
347-
if (attributedString instanceof AttributedStringProxy) {
348-
newText = AttributedStringProxy.toSpannable(((AttributedStringProxy) attributedString),
349-
TiApplication.getAppCurrentActivity());
346+
Object attr = d.get(TiC.PROPERTY_ATTRIBUTED_STRING);
347+
AttributedStringProxy proxy = AttributedStringProxy.createFromProperties(attr);
348+
if (proxy != null) {
349+
newText = AttributedStringProxy.toSpannable(proxy, TiApplication.getAppCurrentActivity());
350350
}
351351
}
352352
if ((newText == null) && d.containsKey(TiC.PROPERTY_HTML)) {
@@ -515,9 +515,9 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
515515
|| key.equals(TiC.PROPERTY_TITLE)) {
516516
CharSequence newText = null;
517517
if (key.equals(TiC.PROPERTY_ATTRIBUTED_STRING)) {
518-
if (newValue instanceof AttributedStringProxy) {
519-
newText = AttributedStringProxy.toSpannable((AttributedStringProxy) newValue,
520-
TiApplication.getAppCurrentActivity());
518+
AttributedStringProxy asProxy = AttributedStringProxy.createFromProperties(newValue);
519+
if (asProxy != null) {
520+
newText = AttributedStringProxy.toSpannable(asProxy, TiApplication.getAppCurrentActivity());
521521
}
522522
if (newText == null) {
523523
newText = "";

android/modules/ui/src/java/ti/modules/titanium/ui/widget/TiUIText.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,10 @@ public void processProperties(KrollDict d)
271271
}
272272

273273
if (d.containsKey(TiC.PROPERTY_ATTRIBUTED_STRING)) {
274-
Object attributedString = d.get(TiC.PROPERTY_ATTRIBUTED_STRING);
275-
if (attributedString instanceof AttributedStringProxy) {
276-
setAttributedStringText((AttributedStringProxy) attributedString);
274+
Object attr = d.get(TiC.PROPERTY_ATTRIBUTED_STRING);
275+
AttributedStringProxy proxy = AttributedStringProxy.createFromProperties(attr);
276+
if (proxy != null) {
277+
setAttributedStringText(proxy);
277278
}
278279
}
279280

@@ -473,8 +474,11 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
473474
TiUIHelper.linkifyIfEnabled(tv, newValue);
474475
} else if (key.equals(TiC.PROPERTY_ATTRIBUTED_HINT_TEXT) && newValue instanceof AttributedStringProxy) {
475476
setAttributedStringHint((AttributedStringProxy) newValue);
476-
} else if (key.equals(TiC.PROPERTY_ATTRIBUTED_STRING) && newValue instanceof AttributedStringProxy) {
477-
setAttributedStringText((AttributedStringProxy) newValue);
477+
} else if (key.equals(TiC.PROPERTY_ATTRIBUTED_STRING)) {
478+
AttributedStringProxy asProxy = AttributedStringProxy.createFromProperties(newValue);
479+
if (asProxy != null) {
480+
setAttributedStringText(asProxy);
481+
}
478482
} else if (key.equals(TiC.PROPERTY_PADDING)) {
479483
setTextPadding((HashMap) newValue);
480484
} else if (key.equals(TiC.PROPERTY_FULLSCREEN)) {

apidoc/Titanium/UI/AttributedString.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ examples:
6767
{
6868
type: Titanium.UI.ATTRIBUTE_BACKGROUND_COLOR,
6969
value: "red",
70-
range: [text.indexOf('Appcelerator'), ('Appcelerator').length]
70+
range: [text.indexOf('Titanium'), ('Titanium').length]
7171
},
7272
{
7373
type: Titanium.UI.ATTRIBUTE_BACKGROUND_COLOR,

apidoc/Titanium/UI/Label.yml

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ properties:
2020
The underlying attributed string drawn by the label. If set, avoid setting common attributes
2121
in the label, such as `color` and `font`, as unexpected behaviors may result.
2222
23-
Prior to Release 3.6.0, assign this property an object from the
24-
<Titanium.UI.iOS.AttributedString> class.
25-
26-
Since Appcelerator CLI 4.1.0 (Alloy 1.7.0), for Alloy, you can use an `<AttributedString>`
23+
For Alloy, you can use an `<AttributedString>`
2724
element inside a `<Label>` element and set the text property as node text:
2825
2926
``` xml
@@ -55,6 +52,27 @@ properties:
5552
}
5653
]
5754
}
55+
56+
Since 13.2.0 you can also use the AttributedString object without `Titanium.UI.createAttributedString()`:
57+
``` js
58+
var text = 'Bacon ipsum dolor Titanium SDK rocks! sit amet fatback leberkas salami sausage tongue strip steak.';
59+
var label = Titanium.UI.createLabel({
60+
attributedString: {
61+
text: text,
62+
attributes: [
63+
{
64+
type: Titanium.UI.ATTRIBUTE_UNDERLINES_STYLE,
65+
value: Ti.UI.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
66+
range: [text.indexOf('Titanium'), ('Titanium').length]
67+
},
68+
{
69+
type: Titanium.UI.ATTRIBUTE_BACKGROUND_COLOR,
70+
value: "red",
71+
range: [text.indexOf('Titanium'), ('Titanium').length]
72+
}
73+
]
74+
}
75+
});
5876
```
5977
type: Titanium.UI.AttributedString
6078
platforms: [android, iphone, ipad, macos]

apidoc/Titanium/UI/TextArea.yml

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ properties:
6868
6969
This has no effect when used with `hintType` Ti.UI.HINT_TYPE_ANIMATED.
7070
71-
Prior to Release 3.6.0, assign this property an object from the
72-
<Titanium.UI.iOS.AttributedString> class.
73-
74-
Since Appcelerator CLI 4.1.0 (Alloy 1.7.0), for Alloy, you can use an `<AttributedHintText>`
71+
For Alloy, you can use an `<AttributedHintText>`
7572
element inside a `<TextField>` element and set the text property as node text:
7673
7774
``` xml
@@ -103,6 +100,27 @@ properties:
103100
}
104101
]
105102
}
103+
104+
Since 13.2.0 you can also use the AttributedString object without `Titanium.UI.createAttributedString()`:
105+
``` js
106+
var text = 'Bacon ipsum dolor Titanium SDK rocks! sit amet fatback leberkas salami sausage tongue strip steak.';
107+
var label = Titanium.UI.createLabel({
108+
attributedString: {
109+
text: text,
110+
attributes: [
111+
{
112+
type: Titanium.UI.ATTRIBUTE_UNDERLINES_STYLE,
113+
value: Ti.UI.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
114+
range: [text.indexOf('Titanium'), ('Titanium').length]
115+
},
116+
{
117+
type: Titanium.UI.ATTRIBUTE_BACKGROUND_COLOR,
118+
value: "red",
119+
range: [text.indexOf('Titanium'), ('Titanium').length]
120+
}
121+
]
122+
}
123+
});
106124
```
107125
type: Titanium.UI.AttributedString
108126
platforms: [android, iphone, ipad, macos]
@@ -117,10 +135,7 @@ properties:
117135
The underlying attributed string drawn by the textArea. If set, avoid setting common attributes
118136
in textArea, such as `value`, `color` and `font`, as unexpected behaviors may result.
119137
120-
Prior to Release 3.6.0, assign this property an object from the
121-
<Titanium.UI.iOS.AttributedString> class.
122-
123-
Since Appcelerator CLI 4.1.0 (Alloy 1.7.0), for Alloy, you can use an `<AttributedString>`
138+
For Alloy, you can use an `<AttributedString>`
124139
element inside a `<TextArea>` element and set the text property as node text:
125140
126141
``` xml

apidoc/Titanium/UI/TextField.yml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,28 @@ properties:
9797
]
9898
}
9999
```
100+
101+
Since 13.2.0 you can also use the AttributedString object without `Titanium.UI.createAttributedString()`:
102+
``` js
103+
const text = 'Bacon ipsum dolor Titanium SDK rocks! sit amet fatback leberkas salami sausage tongue strip steak.';
104+
const label = Titanium.UI.createLabel({
105+
attributedString: {
106+
text: text,
107+
attributes: [
108+
{
109+
type: Titanium.UI.ATTRIBUTE_UNDERLINES_STYLE,
110+
value: Ti.UI.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
111+
range: [text.indexOf('Titanium'), ('Titanium').length]
112+
},
113+
{
114+
type: Titanium.UI.ATTRIBUTE_BACKGROUND_COLOR,
115+
value: "red",
116+
range: [text.indexOf('Titanium'), ('Titanium').length]
117+
}
118+
]
119+
}
120+
});
121+
```
100122
type: Titanium.UI.AttributedString
101123
platforms: [android, iphone, ipad, macos]
102124
since:
@@ -112,10 +134,7 @@ properties:
112134
113135
This has no effect when used with `hintType` Ti.UI.HINT_TYPE_ANIMATED.
114136
115-
Prior to Release 3.6.0, assign this property an object from the
116-
<Titanium.UI.iOS.AttributedString> class.
117-
118-
Since Appcelerator CLI 4.1.0 (Alloy 1.7.0), for Alloy, you can use an `<AttributedHintText>`
137+
For Alloy, you can use an `<AttributedHintText>`
119138
element inside a `<TextField>` element and set the text property as node text:
120139
121140
``` xml

iphone/Classes/TiUIAttributedStringProxy.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
- (NSString *)getLink:(NSUInteger)arg;
1717

18+
+ (TiUIAttributedStringProxy *)fromProperties:(id)arg;
19+
1820
typedef enum {
1921
AttributeNameFont,
2022
AttributeNameParagraphStyle,

iphone/Classes/TiUIAttributedStringProxy.m

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111

1212
@implementation TiUIAttributedStringProxy
1313

14+
+ (TiUIAttributedStringProxy *)fromProperties:(id)arg
15+
{
16+
if ([arg isKindOfClass:[TiUIAttributedStringProxy class]]) {
17+
return arg;
18+
}
19+
if ([arg isKindOfClass:[NSDictionary class]]) {
20+
TiUIAttributedStringProxy *asProxy = [[TiUIAttributedStringProxy alloc] init];
21+
[asProxy _initWithProperties:arg];
22+
return [asProxy autorelease];
23+
}
24+
return nil;
25+
}
26+
1427
- (void)_destroy
1528
{
1629
RELEASE_TO_NIL(_attributedString)

0 commit comments

Comments
 (0)