An AttributedString is a string which also contains information about styles such as text color, font, font size. It gets drawn in an UiArea element.
var libui = require('.');
var str = new libui.AttributedString('');
str.appendAttributed('Test\n', libui.FontAttribute.newSize(24));
str.appendAttributed('Background', libui.FontAttribute.newBackgroundColor(new libui.Color(0.5, 0.5, 0.5, 1)));
function draw(area, p) {
const font = new libui.FontDescriptor('Georgia', 14, libui.textWeight.normal, libui.textItalic.normal, libui.textStretch.normal);
console.log(p.getAreaWidth())
const layout = new libui.DrawTextLayout(str, font, p.getAreaWidth(), libui.textAlign.left);
p.getContext().text(0, 0, layout);
}
function noop(){}
var win = new libui.UiWindow('AttributedString example', 300, 300, true);
win.margined = true;
const box = new libui.UiHorizontalBox();
win.setChild(box);
var area = new libui.UiArea(draw, noop, noop, noop, noop);
box.append(area, true);
win.onClosing(function () {
win.close();
libui.stopLoop();
});
win.show();
libui.startLoop();A styled string.
Arguments
- s: String
Removes characters in the range start - end(exclusive).
Arguments
- start: Number
- end: Number
Sets an attribute in the range start - end(exclusive).
Arguments
- attr: FontAttribute
- start: Number
- end: Number
Appends a string without any attributes.
Arguments*
- s: String
Inserts a string without any attributes at pos.
Arguments*
- s: String
- pos: Number
Appends a string with the specified attributes.
Arguments*
- s: String
- a1: FontAttribute
- a2: FontAttribute (optional)
- (optionally more attributes)
Inserts a string with the specified attributes at pos.
Arguments*
- s: String
- pos: Number
- a1: FontAttribute
- a2: FontAttribute (optional)
- (optionally more attributes)
Returns the text content.
Iterates over all attributes. Return true in the callback to break.
Arguments
- cb:
function(AttributedString, FontAttribute, start: number, end: Number)
Returns the number of graphemes (characters from the point of view of the user).
The cursor of a text editor is always placed on a grapheme boundary, so you can use these features to move the cursor left or right by one "character".
Converts a byte index in the string to a grapheme index.
Arguments
- pos: Number
Converts a graphmeme index in the string to a byte index.
Arguments
- pos: Number
Frees the object immediately.
not every font supports every style
Returns a new FontAttribute for the font family.
Arguments
- family: String
Returns a new FontAttribute for the font size size.
Arguments
- size: Number
Returns a new FontAttribute for the font weight weight.
Arguments
- weight: Number. Possible values:
libui.textWeight.minimumlibui.textWeight.thinlibui.textWeight.ultraLightlibui.textWeight.lightlibui.textWeight.booklibui.textWeight.normallibui.textWeight.mediumlibui.textWeight.semiBoldlibui.textWeight.boldlibui.textWeight.ultraBoldlibui.textWeight.heavylibui.textWeight.ultraHeavylibui.textWeight.maximum- any number between
minimumandmaximum
Returns a new FontAttribute for the italic style style.
Arguments
- style:
libui.textItalic.normallibui.textItalic.oblique("slanted version of normal")libui.textItalic.italic("true italics")
Returns a new FontAttribute for the stretch (or width) style style.
Arguments
- style:
libui.textStretch.ultraCondensedlibui.textStretch.extraCondensedlibui.textStretch.condensedlibui.textStretch.semiCondensedlibui.textStretch.normallibui.textStretch.semiExpandedlibui.textStretch.expandedlibui.textStretch.extraExpandedlibui.textStretch.ultraExpanded
Returns a new FontAttribute for the text color color.
Arguments
- color: Color
Returns a new FontAttribute for the background color color.
Arguments
- color: Color
Returns a new FontAttribute for the underline style style.
Arguments
- style:
libui.textUnderline.nonelibui.textUnderline.singlelibui.textUnderline.doublelibui.textUnderline.suggestion
Returns a new FontAttribute for the underline color.
Arguments
- colorAttr:
libui.textUnderlineColor.customlibui.textUnderlineColor.spellinglibui.textUnderlineColor.grammarlibui.textUnderlineColor.auxiliary
- color: Color (required only with
textUnderlineColor.custom)
Returns a new FontAttribute with the OpenTypeFeatures otf.
Arguments
- otf: OpenTypeFeatures
Returns the type of the attribute. Possible values:
libui.textAttributeType.familylibui.textAttributeType.sizelibui.textAttributeType.weightlibui.textAttributeType.italiclibui.textAttributeType.stretchlibui.textAttributeType.colorlibui.textAttributeType.backgroundlibui.textAttributeType.underlinelibui.textAttributeType.underlineColorlibui.textAttributeType.features
Returns the font family string or null if called on a non-family attribute.
Returns the font size or null if called on a non-size attribute.
Returns the font weight (see newWeight for values) or null if called on a non-weight attribute.
Returns the font italic style (see newItalic for values) or null if called on a non-italic attribute.
Returns the font stretch (see newStretch for values) or null if called on a non-stretch attribute.
Returns the color or null if called on a non-color attribute.
Returns the underline style (see newUnderline for values) or null if called on a non-underline-style attribute.
Returns an object describing the underline color or null if called on a non-underline-style attribute.
{
type: textUnderlineColor.custom,
color: Color
}
// or
{
type: textUnderlineColor.grammar | spelling | auxiliary,
color: null
}See newUnderlineColor for type values)
Returns the OpenTypeFeatures or null if called on a non-OpenType-features attribute.
Frees the object immediately.
Defines font glyph settings (ignored if not supported by the font).
See here for more information and a list of feature tags.
Example: Setting liga to 1 enables ligatures (not supported by every font):
const otf = new libui.OpenTypeFeatures();
otf.add('liga', 1)
str.appendAttributed('affix', FontAttribute.newOTFeatures(otf));Returns a new object containg all tags from f2.
Arguments
- f2: OpenTypeFeatures
Adds/overwrites a tag with value.
Arguments
- tag: String
- value: Number
Remove a tag (and use the default).
Arguments
- tag: String
Returns the value of tag or null if not set.
Arguments
- tag: String
Iterates over all tags. Return true in the callback to break.
Arguments
- cb:
function(OpenTypeFeatures, tag: String, value: Number)
Frees the object immediately.
Defines a font.
Arguments
- family: String
- size: Number
- weight: see FontAttribute.newWeight
- italic: see FontAttribute.newItalic
- stretch: see FontAttribute.newStretch
Returns the font family.
Returns the font size.
Returns the font weight.
Returns the italic style.
Returns the font stretch.
Frees the object immediately.
Defines how an attributed string should get drawn onto an area. (See Area UiDrawContext.text)
Arguments
- str: AttributedString
- defaultFont: FontDescriptor
- width: Number (i.e.
params.getAreaWidth()) - align:
libui.textAlign.leftlibui.textAlign.centerlibui.textAlign.right
Returns a SizeDouble containing the actual width and height of the text.
Frees the object immediately.