/// <summary> /// Applies centered alignment to the text on the linebox /// </summary> /// <param name="g"></param> /// <param name="lineBox"></param> private static void ApplyJustifyAlignment(Graphics g, LineBox lineBox) { if (lineBox.Equals(lineBox.OwnerBox.LineBoxes[lineBox.OwnerBox.LineBoxes.Count - 1])) return; var indent = lineBox.Equals(lineBox.OwnerBox.LineBoxes[0]) ? lineBox.OwnerBox.ActualTextIndent : 0f; var textSum = 0f; var words = 0f; var availWidth = lineBox.OwnerBox.ClientRectangle.Width - indent; #region Gather text sum foreach (var w in lineBox.Words) { textSum += w.Width; words += 1f; } #endregion if (words <= 0f) return; //Avoid Zero division var spacing = (availWidth - textSum)/words; //Spacing that will be used var curx = lineBox.OwnerBox.ClientLeft + indent; foreach (var word in lineBox.Words) { word.Left = curx; curx = word.Right + spacing; if (word == lineBox.Words[lineBox.Words.Count - 1]) { word.Left = lineBox.OwnerBox.ClientRight - word.Width; } //TODO: Background rectangles are being deactivated when justifying text. } }