ti.attributed
Version:
Easy Attributed string for Appcelerator Titanium
283 lines (251 loc) • 6.61 kB
JavaScript
function BOLD(_label, text, _word, _font) {
return {
type: Ti.UI.ATTRIBUTE_FONT,
value: {
fontSize: _font ? _font.fontSize : _label.font.fontSize,
fontFamily: _font ? _font.fontFamily : _label.font._fontFamily,
},
range: _word ? [text.indexOf(_word), _word.length] : [0, text.length]
}
}
function COLOR(text, _word, _color) {
return {
type: Ti.UI.ATTRIBUTE_FOREGROUND_COLOR,
value: _color,
range: _word ? [text.indexOf(_word), _word.length] : [0, text.length]
}
}
function BACKGROUNDCOLOR(text, _word, _color) {
return {
type: Ti.UI.ATTRIBUTE_BACKGROUND_COLOR,
value: _color,
range: _word ? [text.indexOf(_word), _word.length] : [0, text.length]
}
}
function UNDERLINE(text, _word, value = Ti.UI.ATTRIBUTE_UNDERLINE_STYLE_SINGLE) {
return {
type: Ti.UI.ATTRIBUTE_UNDERLINES_STYLE,
value,
range: _word ? [text.indexOf(_word), _word.length] : [0, text.length]
}
}
function STRIKETHROUGH(text, _word) {
return {
type: Ti.UI.ATTRIBUTE_STRIKETHROUGH_STYLE,
value: Ti.UI.ATTRIBUTE_UNDERLINE_STYLE_THICK,
range: _word ? [text.indexOf(_word), _word.length] : [0, text.length]
}
}
function LINK(text, _word, value) {
return {
type: Ti.UI.ATTRIBUTE_LINK,
value,
range: _word ? [text.indexOf(_word), _word.length] : [0, text.length]
}
}
function KERN(text, _word, value) {
return {
type: Ti.UI.ATTRIBUTE_KERN,
value,
range: _word ? [text.indexOf(_word), _word.length] : [0, text.length]
}
}
function SHADOW(text, _word, value) {
return {
type: Ti.UI.ATTRIBUTE_SHADOW,
value,
range: _word ? [text.indexOf(_word), _word.length] : [0, text.length]
}
}
function LETTERPRESS(text, _word) {
return {
type: Ti.UI.ATTRIBUTE_TEXT_EFFECT,
value: Ti.UI.ATTRIBUTE_LETTERPRESS_STYLE,
range: _word ? [text.indexOf(_word), _word.length] : [0, text.length]
}
}
function BASELINE(text, _word, value) {
return {
type: Ti.UI.ATTRIBUTE_BASELINE_OFFSET,
value,
range: _word ? [text.indexOf(_word), _word.length] : [0, text.length]
}
}
function OBLIQUENESS(text, _word, value) {
return {
type: Ti.UI.ATTRIBUTE_OBLIQUENESS,
value,
range: _word ? [text.indexOf(_word), _word.length] : [0, text.length]
}
}
function STRETCH(text, _word, value) {
return {
type: Ti.UI.ATTRIBUTE_EXPANSION,
value,
range: _word ? [text.indexOf(_word), _word.length] : [0, text.length]
}
}
function apply(_label, attributes) {
_label.attributedString = Ti.UI.createAttributedString({
text: _label.text,
attributes
});
}
function bold(_label, _word, _font) {
const text = _label.text;
const attributes = [BOLD(_label, text, _word, _font)];
apply(_label, attributes);
}
function boldAndColor(_label, _word, _font, _color) {
const text = _label.text;
const attributes = [
BOLD(_label, text, _word, _font),
COLOR(text, _word, _color)
];
apply(_label, attributes);
}
function underline(_label, _word, value) {
const text = _label.text;
const attributes = [UNDERLINE(text, _word, value)];
apply(_label, attributes);
}
function underlineAndColor(_label, _word, _color, value) {
const text = _label.text;
const attributes = [
UNDERLINE(text, _word, value),
COLOR(text, _word, _color)
];
apply(_label, attributes);
}
function color(_label, _word, _color) {
const text = _label.text;
const attributes = [
COLOR(text, _word, _color)
];
apply(_label, attributes);
}
function backgroundColor(_label, _word, _color) {
const text = _label.text;
const attributes = [
BACKGROUNDCOLOR(text, _word, _color)
];
apply(_label, attributes);
}
function strikethrough(_label, _word) {
const text = _label.text;
const attributes = [
STRIKETHROUGH(text, _word)
];
apply(_label, attributes);
}
function link(_label, _word, value) {
const text = _label.text;
const attributes = [
LINK(text, _word, value)
];
apply(_label, attributes);
}
function kern(_label, _word, value) {
const text = _label.text;
const attributes = [
KERN(text, _word, value)
];
apply(_label, attributes);
}
function shadow(_label, _word, value) {
const text = _label.text;
const attributes = [
SHADOW(text, _word, value)
];
apply(_label, attributes);
}
function letterpress(_label, _word) {
const text = _label.text;
const attributes = [
LETTERPRESS(text, _word)
];
apply(_label, attributes);
}
function baseline(_label, _word, value) {
const text = _label.text;
const attributes = [
BASELINE(text, _word, value)
];
apply(_label, attributes);
}
function obliqueness(_label, _word, value) {
const text = _label.text;
const attributes = [
OBLIQUENESS(text, _word, value)
];
apply(_label, attributes);
}
function stretch(_label, _word, value) {
const text = _label.text;
const attributes = [
STRETCH(text, _word, value)
];
apply(_label, attributes);
}
exports.createLabel = function (args) {
const label = Ti.UI.createLabel(args);
switch (args.attribute) {
case 'color':
color(label, args.wordAttribute, args.attributeColor);
break;
case 'backgroundColor':
backgroundColor(label, args.wordAttribute, args.attributeColor);
break;
case 'underline':
underline(label, args.wordAttribute, args.wordValue);
break;
case 'underlineAndColor':
underlineAndColor(label, args.wordAttribute, args.attributeColor, args.wordValue);
break;
case 'bold':
bold(label, args.wordAttribute, args.attributeFont);
break;
case 'boldAndColor':
boldAndColor(label, args.wordAttribute, args.attributeFont, args.attributeColor);
break;
case 'strikethrough':
strikethrough(label, args.wordAttribute);
break;
case 'link':
link(label, args.wordAttribute, args.wordValue);
break;
case 'kern':
kern(label, args.wordAttribute, args.wordValue);
break;
case 'shadow':
shadow(label, args.wordAttribute, args.wordValue);
break;
case 'letterpress':
letterpress(label, args.wordAttribute);
break;
case 'baseline':
baseline(label, args.wordAttribute, args.wordValue);
break;
case 'obliqueness':
obliqueness(label, args.wordAttribute, args.wordValue);
break;
case 'stretch':
stretch(label, args.wordAttribute, args.wordValue);
break;
}
return label;
};
exports.color = color;
exports.backgroundColor = backgroundColor;
exports.underlineAndColor = underlineAndColor;
exports.underline = underline;
exports.boldAndColor = boldAndColor;
exports.bold = bold;
exports.strikethrough = strikethrough;
exports.link = link;
exports.kern = kern;
exports.shadow = shadow;
exports.letterpress = letterpress;
exports.baseline = baseline;
exports.obliqueness = obliqueness;
exports.stretch = stretch;