/// <summary> /// /// </summary> /// <param name="start"></param> /// <param name="length"></param> /// <param name="text"></param> public void Substitute(int start, int length, string text) { if (MessageBlocks.Count == 0 || start < 0 || length < 0) { return; } int count = 0; int startBlock = -1; int endBlock = -1; var blocks = new List <TextMessageBlock>(); for (int i = 0; i < MessageBlocks.Count; ++i) { if (start < count + MessageBlocks[i].Text.Length) { startBlock = i; break; } count += MessageBlocks[i].Text.Length; } if (startBlock == -1) { return; } var indexInStartBlock = start - count; for (int i = startBlock; i < MessageBlocks.Count; ++i) { if (start + length <= count + MessageBlocks[i].Text.Length) { endBlock = i; break; } count += MessageBlocks[i].Text.Length; } if (endBlock == -1) { return; } var indexInEndBlock = start + length - count; if (indexInStartBlock > 0) { blocks.Add(new TextMessageBlock(MessageBlocks[startBlock].Text.Substring(0, indexInStartBlock), MessageBlocks[startBlock].Foreground, MessageBlocks[startBlock].Background)); } blocks.Add(new TextMessageBlock(text, MessageBlocks[startBlock].Foreground, MessageBlocks[startBlock].Background)); if (indexInEndBlock < MessageBlocks[endBlock].Text.Length) { blocks.Add(new TextMessageBlock(MessageBlocks[endBlock].Text.Substring(indexInEndBlock), MessageBlocks[endBlock].Foreground, MessageBlocks[endBlock].Background)); } MessageBlocks.RemoveRange(startBlock, endBlock + 1 - startBlock); MessageBlocks.InsertRange(startBlock, blocks); UpdateInnerText(); }