TextBlock.ATZ.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Topten.RichTextKit
  4. {
  5. partial class TextBlock
  6. {
  7. /// <summary>
  8. /// Determines whether to add ellipsis symbol automatically to a truncated text.
  9. /// </summary>
  10. public bool AllowEllipsis { get; set; } = true;
  11. /// <summary>
  12. /// Corresponds to LineLimit flag of gdi+ StringFormat. Allows to draw a partially visible text line if set to false.
  13. /// </summary>
  14. public bool LineLimit { get; set; } = true;
  15. /// <summary>
  16. /// Gets or sets the tab stop list.
  17. /// </summary>
  18. public float[] TabStops { get; set; }
  19. /// <summary>
  20. /// Gets or sets the default tab width, in pixels.
  21. /// </summary>
  22. public float FirstTabOffset { get; set; }
  23. private float GetTabStopPositionOffset(float pos)
  24. {
  25. float result = FirstTabOffset;
  26. if (TabStops != null && TabStops.Length > 0)
  27. {
  28. float tabWidth = 0;
  29. for (int i = 0; i < TabStops.Length; i++)
  30. {
  31. tabWidth = TabStops[i];
  32. result += tabWidth;
  33. if (result > pos)
  34. return result - pos;
  35. if (i == 0 && tabWidth > 0)
  36. result -= result % tabWidth;
  37. }
  38. if (tabWidth <= 0)
  39. return 0;
  40. while (result < pos)
  41. {
  42. result += tabWidth;
  43. }
  44. return result - pos;
  45. }
  46. if (result > pos)
  47. return result - pos;
  48. return 0;
  49. }
  50. /// <summary>
  51. /// Adds a text that may contain tabs.
  52. /// </summary>
  53. /// <param name="text">The text to add.</param>
  54. /// <param name="style">The style.</param>
  55. public void AddTextWithTabs(string text, Style style)
  56. {
  57. // each tab is added as a separate symbol
  58. if (text.Contains("\t"))
  59. {
  60. while (text != "")
  61. {
  62. int tabPos = text.IndexOf("\t");
  63. if (tabPos == -1)
  64. break;
  65. if (tabPos != 0)
  66. AddText(text.Substring(0, tabPos), style);
  67. AddText("\t", style);
  68. text = text.Substring(tabPos + 1);
  69. }
  70. }
  71. AddText(text, style);
  72. }
  73. }
  74. }