Skip to content

Commit

Permalink
docs: attributedString link example
Browse files Browse the repository at this point in the history
  • Loading branch information
m1ga committed Jun 6, 2024
1 parent 2f1212f commit c8610d6
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions apidoc/Titanium/UI/AttributedString.yml
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,63 @@ examples:
win.add(label);
```
- title: Links with underline color.
example: |
``` js
const win = Ti.UI.createWindow({
backgroundColor: 'gray',
layout: 'vertical'
});
const lbl_a = createLink();
const lbl_b = createLink();
colorLink(lbl_b);
win.add([lbl_a, lbl_b]);
win.open();
function createLink() {
const label = Ti.UI.createLabel({
top: 20,
attributedString: Ti.UI.createAttributedString({
text: 'Check out Titanium SDK',
attributes: [{
type: Ti.UI.ATTRIBUTE_LINK,
value: 'https://titaniumsdk.com',
range: [10, 12]
}]
})
});
label.addEventListener('link', e => {
Ti.Platform.openURL(e.url);
});
return label;
}
function colorLink(lbl) {
const attributedString = lbl.attributedString;
const textColor = 'purple';
const underlineColor = 'yellow';
for (const attribute of attributedString.attributes) {
if (attribute.type === Ti.UI.ATTRIBUTE_LINK) {
// Set new link color.
attributedString.addAttribute({
type: Ti.UI.ATTRIBUTE_FOREGROUND_COLOR,
value: textColor,
range: attribute.range
});
// Set new underline color.
attributedString.addAttribute({
type: Ti.UI.ATTRIBUTE_UNDERLINE_COLOR,
value: underlineColor,
range: attribute.range
});
}
}
}
```

0 comments on commit c8610d6

Please sign in to comment.