本文整理匯總了Java中java.text.AttributedString類的典型用法代碼示例。如果您正苦於以下問題:Java AttributedString類的具體用法?Java AttributedString怎麽用?Java AttributedString使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
AttributedString類屬於java.text包,在下文中一共展示了AttributedString類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: while
import java.text.AttributedString; //導入依賴的package包/類
private static AttributedCharacterIterator getTrimmedTrailingSpacesIterator
(AttributedCharacterIterator iterator) {
int curIdx = iterator.getIndex();
char c = iterator.last();
while(c != CharacterIterator.DONE && Character.isWhitespace(c)) {
c = iterator.previous();
}
if (c != CharacterIterator.DONE) {
int endIdx = iterator.getIndex();
if (endIdx == iterator.getEndIndex() - 1) {
iterator.setIndex(curIdx);
return iterator;
} else {
AttributedString trimmedText = new AttributedString(iterator,
iterator.getBeginIndex(), endIdx + 1);
return trimmedText.getIterator();
}
} else {
return null;
}
}
示例2: testAttributedStringSerialization3
import java.text.AttributedString; //導入依賴的package包/類
/**
* Tests the serialization of an {@link AttributedString}.
*/
public void testAttributedStringSerialization3() {
AttributedString s1 = new AttributedString("ABC");
s1.addAttribute(TextAttribute.LANGUAGE, "English");
AttributedString s2 = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buffer);
SerialUtilities.writeAttributedString(s1, out);
out.close();
ByteArrayInputStream bais = new ByteArrayInputStream(
buffer.toByteArray());
ObjectInputStream in = new ObjectInputStream(bais);
s2 = SerialUtilities.readAttributedString(in);
in.close();
}
catch (Exception e) {
e.printStackTrace();
}
assertTrue(AttributedStringUtilities.equal(s1, s2));
}
示例3: getComposedTextAttribute
import java.text.AttributedString; //導入依賴的package包/類
private static AttributedString getComposedTextAttribute(DocumentEvent e) {
if (e instanceof BaseDocumentEvent) {
AttributeSet attribs = ((BaseDocumentEvent) e).getChangeAttributes();
if (attribs != null) {
Object value = attribs.getAttribute(StyleConstants.ComposedTextAttribute);
if (value instanceof AttributedString) {
return (AttributedString) value;
}
}
}
return null;
}
示例4: formatToCharacterIterator
import java.text.AttributedString; //導入依賴的package包/類
/**
* {@inheritDoc}
*
* @stable ICU 49
*/
@Override
public AttributedCharacterIterator formatToCharacterIterator(Object obj) {
StringBuffer toAppendTo = new StringBuffer();
FieldPosition pos = new FieldPosition(0);
toAppendTo = format(obj, toAppendTo, pos);
// supporting only DateFormat.Field.TIME_ZONE
AttributedString as = new AttributedString(toAppendTo.toString());
as.addAttribute(DateFormat.Field.TIME_ZONE, DateFormat.Field.TIME_ZONE);
return as.getIterator();
}
示例5: TextLayout
import java.text.AttributedString; //導入依賴的package包/類
/**
* Constructs a <code>TextLayout</code> from a <code>String</code>
* and an attribute set.
* <p>
* All the text is styled using the provided attributes.
* <p>
* <code>string</code> must specify a single paragraph of text because an
* entire paragraph is required for the bidirectional algorithm.
* @param string the text to display
* @param attributes the attributes used to style the text
* @param frc contains information about a graphics device which is needed
* to measure the text correctly.
* Text measurements can vary slightly depending on the
* device resolution, and attributes such as antialiasing. This
* parameter does not specify a translation between the
* <code>TextLayout</code> and user space.
*/
public TextLayout(String string, Map<? extends Attribute,?> attributes,
FontRenderContext frc)
{
if (string == null) {
throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
}
if (attributes == null) {
throw new IllegalArgumentException("Null map passed to TextLayout constructor.");
}
if (string.length() == 0) {
throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
}
char[] text = string.toCharArray();
Font font = singleFont(text, 0, text.length, attributes);
if (font != null) {
fastInit(text, font, attributes, frc);
} else {
AttributedString as = new AttributedString(string, attributes);
standardInit(as.getIterator(), text, frc);
}
}
示例6: dispatchCommittedText
import java.text.AttributedString; //導入依賴的package包/類
/**
* Dispatches committed text from XIM to the awt event queue. This
* method is invoked from the event handler in canvas.c in the
* AWT Toolkit thread context and thus inside the AWT Lock.
* @param str committed text
* @param long when
*/
// NOTE: This method may be called by privileged threads.
// This functionality is implemented in a package-private method
// to insure that it cannot be overridden by client subclasses.
// DO NOT INVOKE CLIENT CODE ON THIS THREAD!
void dispatchCommittedText(String str, long when) {
if (str == null)
return;
if (composedText == null) {
AttributedString attrstr = new AttributedString(str);
postInputMethodEvent(InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
attrstr.getIterator(),
str.length(),
null,
null,
when);
} else {
// if there is composed text, wait until the preedit
// callback is invoked.
committedText = str;
}
}
示例7: flushText
import java.text.AttributedString; //導入依賴的package包/類
/**
* Flushes composed and committed text held in this context.
* This method is invoked in the AWT Toolkit (X event loop) thread context
* and thus inside the AWT Lock.
*/
// NOTE: This method may be called by privileged threads.
// This functionality is implemented in a package-private method
// to insure that it cannot be overridden by client subclasses.
// DO NOT INVOKE CLIENT CODE ON THIS THREAD!
void flushText() {
String flush = (committedText != null ? committedText : "");
if (composedText != null) {
flush += composedText.toString();
}
if (!flush.equals("")) {
AttributedString attrstr = new AttributedString(flush);
postInputMethodEvent(InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
attrstr.getIterator(),
flush.length(),
null,
null,
EventQueue.getMostRecentEventTime());
composedText = null;
committedText = null;
}
}
示例8: getRawDataIterator
import java.text.AttributedString; //導入依賴的package包/類
/**
* Gets a lazy list of raw data in frames with the given stream, starting at
* this frame and extending backwards over previous frames. The data is
* coloured according to time difference with a given time, and control
* codes are coloured red and translated into a human-readable form.
* @param relativeTime The time, relative to the start of the ttyrec, to
* colour the data relative to.
* @param stream The stream from which the raw data should be returned.
* @return A lazy list of raw data from frames with the given stream.
*/
public Iterable<AttributedString> getRawDataIterator(final double relativeTime, final int stream) {
return () -> new Iterator<AttributedString>() {
int lastSeqNumber = seqNumber;
public boolean hasNext() {
return hasNextAnnotation(
relativeTime, lastSeqNumber, stream);
}
public AttributedString next() {
AttributedStringAndNumber asan = nextAnnotation(
relativeTime, lastSeqNumber, stream);
lastSeqNumber = asan.getN();
return asan.getA();
}
public void remove() {
throw new UnsupportedOperationException(
"Attempt to modify an immutable list");
}
};
}
示例9: TextLayout
import java.text.AttributedString; //導入依賴的package包/類
/**
* Constructs a {@code TextLayout} from a {@code String}
* and an attribute set.
* <p>
* All the text is styled using the provided attributes.
* <p>
* {@code string} must specify a single paragraph of text because an
* entire paragraph is required for the bidirectional algorithm.
* @param string the text to display
* @param attributes the attributes used to style the text
* @param frc contains information about a graphics device which is needed
* to measure the text correctly.
* Text measurements can vary slightly depending on the
* device resolution, and attributes such as antialiasing. This
* parameter does not specify a translation between the
* {@code TextLayout} and user space.
*/
public TextLayout(String string, Map<? extends Attribute,?> attributes,
FontRenderContext frc)
{
if (string == null) {
throw new IllegalArgumentException("Null string passed to TextLayout constructor.");
}
if (attributes == null) {
throw new IllegalArgumentException("Null map passed to TextLayout constructor.");
}
if (string.length() == 0) {
throw new IllegalArgumentException("Zero length string passed to TextLayout constructor.");
}
char[] text = string.toCharArray();
Font font = singleFont(text, 0, text.length, attributes);
if (font != null) {
fastInit(text, font, attributes, frc);
} else {
AttributedString as = new AttributedString(string, attributes);
standardInit(as.getIterator(), text, frc);
}
}
示例10: dispatchCommittedText
import java.text.AttributedString; //導入依賴的package包/類
/**
* Dispatches committed text from XIM to the awt event queue. This
* method is invoked from the event handler in canvas.c in the
* AWT Toolkit thread context and thus inside the AWT Lock.
* @param str committed text
* @param when when
*/
// NOTE: This method may be called by privileged threads.
// This functionality is implemented in a package-private method
// to insure that it cannot be overridden by client subclasses.
// DO NOT INVOKE CLIENT CODE ON THIS THREAD!
void dispatchCommittedText(String str, long when) {
if (str == null)
return;
if (composedText == null) {
AttributedString attrstr = new AttributedString(str);
postInputMethodEvent(InputMethodEvent.INPUT_METHOD_TEXT_CHANGED,
attrstr.getIterator(),
str.length(),
null,
null,
when);
} else {
// if there is composed text, wait until the preedit
// callback is invoked.
committedText = str;
}
}
示例11: main
import java.text.AttributedString; //導入依賴的package包/類
public static void main(String[] args) {
// Create a new AttributedString with one attribute.
Hashtable attributes = new Hashtable();
attributes.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
AttributedString origString = new AttributedString("Hello world.", attributes);
// Create an iterator over part of the AttributedString.
AttributedCharacterIterator iter = origString.getIterator(null, 4, 6);
// Attempt to create a new AttributedString from the iterator.
// This will throw IllegalArgumentException.
AttributedString newString = new AttributedString(iter);
// Without the exception this would get executed.
System.out.println("DONE");
}
示例12: getLineBreakMeasurers
import java.text.AttributedString; //導入依賴的package包/類
private LineBreakMeasurer[] getLineBreakMeasurers(Graphics2D g) {
if (lbmTexto == null && (Texto != null && !Texto.equals(""))) {
lbmTexto = new LineBreakMeasurer[Textos.length];
for (int i = 0; i < lbmTexto.length; i++) {
String tmp = Textos[i].isEmpty()? " " : Textos[i];
AttributedString attribString = new AttributedString(tmp);
attribString.addAttribute(TextAttribute.FONT, getFont());
//attribString.addAttribute(TextAttribute.FONT, getFont());
AttributedCharacterIterator attribCharIterator = attribString.getIterator();
//FontRenderContext frc = new FontRenderContext(null, true, false);
FontRenderContext frc = g.getFontRenderContext();
lbmTexto[i] = new LineBreakMeasurer(attribCharIterator, frc);
}
}
return lbmTexto;
}
示例13: getPreferredSize
import java.text.AttributedString; //導入依賴的package包/類
@Override
public Dimension getPreferredSize(JComponent c) {
String tipText = ((JToolTip)c).getTipText();
if (tipText == null || tipText.isEmpty()) {
return new Dimension(0, 0);
}
float x = 0f;
float y = 0f;
for (String line : lineBreak.split(tipText)) {
if (line.isEmpty()) {
y += LEADING;
continue;
}
AttributedCharacterIterator styledText
= new AttributedString(line).getIterator();
LineBreakMeasurer measurer = new LineBreakMeasurer(styledText, frc);
while (measurer.getPosition() < styledText.getEndIndex()) {
TextLayout layout = measurer.nextLayout(maximumWidth);
x = Math.max(x, layout.getVisibleAdvance());
y += layout.getAscent() + layout.getDescent() + layout.getLeading();
}
}
return new Dimension((int) (x + 2 * margin),
(int) (y + 2 * margin));
}
示例14: dumpACIWord
import java.text.AttributedString; //導入依賴的package包/類
protected void dumpACIWord(AttributedString as) {
if (as == null) return;
StringBuffer chars = new StringBuffer();
StringBuffer brkStr = new StringBuffer();
AttributedCharacterIterator aci = as.getIterator();
AttributedCharacterIterator.Attribute WORD_LIMIT =
TextLineBreaks.WORD_LIMIT;
for (char ch = aci.current();
ch!=AttributedCharacterIterator.DONE;
ch = aci.next()) {
chars.append( ch ).append( ' ' ).append( ' ' );
int w = ((Integer)aci.getAttribute(WORD_LIMIT)).intValue();
brkStr.append( w ).append( ' ' );
if (w < 10) {
// for small values append another ' '
brkStr.append( ' ' );
}
}
System.out.println( chars.toString() );
System.out.println( brkStr.toString() );
}
示例15: decorateAttributedString
import java.text.AttributedString; //導入依賴的package包/類
public void decorateAttributedString(Graphics2D g2, AttributedString attributedWord, ChangeableAttributedString newAttrString) {
Color oldColor = g2.getColor();
Composite oldComp = g2.getComposite();
g2.setComposite(AlphaComposite.getInstance(alphaCompositeType));
for (int j = 0; j < attributedWord.getIterator().getEndIndex(); j++) {
g2.setColor(holesColorGenerator.getNextColor());
Rectangle2D bounds = newAttrString.getBounds(j).getFrame();
double circleMaxSize = (double) bounds.getWidth() / 2;
for (int i = 0; i < numberOfHolesPerGlyph.intValue(); i++) {
double circleSize = circleMaxSize * (1 + myRandom.nextDouble()) / 2;
double circlex = bounds.getMinX() + bounds.getWidth() * circleXRatio
* myRandom.nextDouble();
double circley = bounds.getMinY() - bounds.getHeight() * circleYRatio
* myRandom.nextDouble();
Ellipse2D circle = new Ellipse2D.Double(circlex, circley, circleSize, circleSize);
g2.fill(circle);
}
}
g2.setColor(oldColor);
g2.setComposite(oldComp);
}