示例#1
0
        public FormattedTextImpl(
            string text,
            Typeface typeface,
            TextAlignment textAlignment,
            TextWrapping wrapping,
            Size constraint,
            IReadOnlyList <FormattedTextStyleSpan> spans)
        {
            Text = text;

            var factory = AvaloniaLocator.Current.GetService <DWrite.Factory>();

            var textFormat = Direct2D1FontCollectionCache.GetTextFormat(typeface);

            textFormat.WordWrapping =
                wrapping == TextWrapping.Wrap ? DWrite.WordWrapping.Wrap : DWrite.WordWrapping.NoWrap;

            TextLayout = new DWrite.TextLayout(
                factory,
                Text ?? string.Empty,
                textFormat,
                (float)constraint.Width,
                (float)constraint.Height)
            {
                TextAlignment = textAlignment.ToDirect2D()
            };

            textFormat.Dispose();

            if (spans != null)
            {
                foreach (var span in spans)
                {
                    ApplySpan(span);
                }
            }

            Size = Measure();
        }
示例#2
0
        public FormattedTextImpl(
            string text,
            Typeface typeface,
            TextAlignment textAlignment,
            TextWrapping wrapping,
            Size constraint,
            IReadOnlyList <FormattedTextStyleSpan> spans)
        {
            Text = text;

            using (var textFormat = Direct2D1FontCollectionCache.GetTextFormat(typeface))
            {
                textFormat.WordWrapping =
                    wrapping == TextWrapping.Wrap ? DWrite.WordWrapping.Wrap : DWrite.WordWrapping.NoWrap;

                TextLayout = new DWrite.TextLayout(
                    Direct2D1Platform.DirectWriteFactory,
                    Text ?? string.Empty,
                    textFormat,
                    (float)constraint.Width,
                    (float)constraint.Height)
                {
                    TextAlignment = textAlignment.ToDirect2D()
                };
            }

            if (spans != null)
            {
                foreach (var span in spans)
                {
                    ApplySpan(span);
                }
            }

            Bounds = Measure();
        }