/// <summary> /// Constructs an <see cref="AttributedString"/> from an /// <see cref="AttributedCharacterIterator"/>, which represents attributed text. /// </summary> /// <param name="iterator">The <see cref="AttributedCharacterIterator"/> that contains the text /// for this attributed string.</param> public AttributedString(AttributedCharacterIterator iterator) { if (iterator.BeginIndex > iterator.EndIndex) { // text.0A=Invalid substring range throw new ArgumentException(/*Messages.getString("text.0A")*/); //$NON-NLS-1$ } StringBuilder buffer = new StringBuilder(); for (int i = iterator.BeginIndex; i < iterator.EndIndex; i++) { buffer.Append(iterator.Current); iterator.Next(); } text = buffer.ToString(); var attributes = iterator .GetAllAttributeKeys(); if (attributes == null) { return; } attributeMap = new Dictionary <AttributedCharacterIteratorAttribute, IList <Range> >( /*(attributes.size() * 4 / 3) + 1*/); //Iterator<Attribute> it = attributes.iterator(); //while (it.hasNext()) //{ // AttributedCharacterIterator.Attribute attribute = it.next(); foreach (var attribute in attributes) { iterator.SetIndex(0); while (iterator.Current != CharacterIterator.Done) { int start = iterator.GetRunStart(attribute); int limit = iterator.GetRunLimit(attribute); object value = iterator.GetAttribute(attribute); if (value != null) { AddAttribute(attribute, value, start, limit); } iterator.SetIndex(limit); } } }
/// <summary> /// Constructs an <see cref="AttributedString"/> from a range of the text contained /// in the specified <see cref="AttributedCharacterIterator"/>, starting at /// <paramref name="start"/> and ending at <paramref name="end"/>. All attributes will be copied to this /// attributed string. /// </summary> /// <param name="iterator">The <see cref="AttributedCharacterIterator"/> that contains the text /// for this attributed string.</param> /// <param name="start">the start index of the range of the copied text.</param> /// <param name="end">the end index of the range of the copied text.</param> /// <exception cref="ArgumentException">if <paramref name="start"/> is less than first index of /// <paramref name="iterator"/>, <paramref name="end"/> is greater than the last index + /// 1 in <paramref name="iterator"/> or if <paramref name="start"/> > <paramref name="end"/>.</exception> public AttributedString(AttributedCharacterIterator iterator, int start, int end) : this(iterator, start, end, iterator.GetAllAttributeKeys()) { }