AttributedString
AttributedString is text with named attributes over ranges of it
(NSAttributedString in shape): the model behind styled text and a rich-text
field. From 0.72.
#import "AttributedString.xc" // not in the Foundation umbrella: import it by nameOverview
Section titled “Overview”AttributedString* s = AttributedString.withString(String.withCString("Hello, world"));s.setAttribute(String.withCString("bold"), Number.withBool(true), Range.make((i32)0, (i32)5));s.runCount(); // 2: "Hello" bold, ", world" plains.attribute(String.withCString("bold"), (i32)1); // trueAttributes are named by Strings and hold any object: a font, a colour, a
link, a flag. Which names mean what is up to the code that draws the text;
Foundation only keeps them. Positions and Ranges are
byte offsets into the UTF-8 text.
Runs. The text is covered by runs: ranges whose bytes share one set of
attributes, with neighbours that have equal sets merged, so drawing can walk the
runs and style one span at a time. Two sets are equal when they have the same
names and their values are equal by equals.
Editing keeps the attributes where they belong. Replacing text gives the new text the attributes of the first byte replaced; inserted text takes those of the byte before it (of the first byte, at the start); text after the edit moves with it.
Topics
Section titled “Topics”Creating · withString · withAttributes · copy
Reading attributes · attribute · attributesAt · runRangeAt · runCount / runRange / runAttributes
Changing attributes · setAttribute · removeAttribute · addAttributes · setAttributes
Editing the text · replace · replaceWithAttributed · appendString · append
Creating
Section titled “Creating”withString
Section titled “withString”static AttributedString* withString(String* text)text with no attributes. new AttributedString() is empty.
withAttributes
Section titled “withAttributes”static AttributedString* withAttributes(String* text, Map* attrs)text with attrs over all of it.
AttributedString* copy(void)The text
Section titled “The text”String* text(void)A copy of the text. It is also the description.
length
Section titled “length”i32 length(void)In bytes.
Reading attributes
Section titled “Reading attributes”attribute
Section titled “attribute”Object* attribute(String* name, i32 at)The value of name at byte at, or null.
attributesAt
Section titled “attributesAt”Map* attributesAt(i32 at)A copy of every attribute at byte at; empty out of range.
runRangeAt
Section titled “runRangeAt”Range* runRangeAt(i32 at)The run holding byte at: the longest range around it with the same
attributes.
runCount / runRange / runAttributes
Section titled “runCount / runRange / runAttributes”u32 runCount(void)Range* runRange(u32 k)Map* runAttributes(u32 k)The runs in order, for drawing: the k-th run’s range and a copy of its attributes.
Changing attributes
Section titled “Changing attributes”Each takes a range, clipped to the text.
setAttribute
Section titled “setAttribute”void setAttribute(String* name, Object* value, Range* r)Sets name to value over r; a null value removes it.
removeAttribute
Section titled “removeAttribute”void removeAttribute(String* name, Range* r)addAttributes
Section titled “addAttributes”void addAttributes(Map* attrs, Range* r)Sets every attribute of attrs over r, leaving the others.
setAttributes
Section titled “setAttributes”void setAttributes(Map* attrs, Range* r)Replaces every attribute over r with attrs (null: none).
Editing the text
Section titled “Editing the text”replace
Section titled “replace”void replace(Range* r, String* text)Replaces the bytes of r with text, which takes the attributes described in
the overview. An empty r inserts.
replaceWithAttributed
Section titled “replaceWithAttributed”void replaceWithAttributed(Range* r, AttributedString* other)The same, keeping other’s own attributes.
appendString
Section titled “appendString”void appendString(String* text)Adds text at the end with the attributes of the last byte.
append
Section titled “append”void append(AttributedString* other)Pieces
Section titled “Pieces”substring
Section titled “substring”AttributedString* substring(Range* r)The text and attributes of r, clipped to the text.
equals
Section titled “equals”bool equals(Object* other)Equal text, and equal attributes over the same runs.