根据矩形宽度计算fontSize

问题描述 投票:0回答:1

我有此方法

    private Shape CreateTextShape(string text, double placeX, double placeY, double fontSize, System.Windows.Media.Color color, FontFamily fontFamily = null)
    {
        Shape textShape;

        if(fontFamily == null)
            fontFamily = new System.Windows.Media.FontFamily("Arial");

        double fontHeight = Math.Ceiling(fontSize * fontFamily.LineSpacing);

        Typeface typeface = new Typeface(fontFamily, FontStyles.Normal, FontWeights.Normal, new FontStretch());

        FormattedText fText = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, fontSize, Brushes.Black, 1.25);

        Point textPosition1;
        textPosition1 = new Point(placeX, placeY - fontHeight);
        Path path = new Path();
        path.Data = fText.BuildGeometry(textPosition1);
        path.Fill = new SolidColorBrush(color);
        textShape = path;
        return textShape;
    }

我希望它考虑首选宽度,并将fontSize设置为一个值,该值可使文本与首选宽度一样宽:

Current result

想要的结果:

Wanted result

编辑:

此代码在新的WPF应用程序中有效。它将创建一个随机大小的矩形。我希望文本匹配与矩形相同的宽度。

public partial class MainWindow : Window
{
    static Random random = new Random();

    public MainWindow()
    {
        InitializeComponent();

        double size = random.Next(500);
        Rectangle rect = new Rectangle()
        {
            Width = size,
            Height = size,
            Stroke = Brushes.Red,
            StrokeThickness = 2
        };
        Canvas.SetLeft(rect, random.Next(500));
        Canvas.SetTop(rect, random.Next(500));

        canvas.Children.Add(rect);
        canvas.Children.Add(CreateTextShape("Hello world!", Canvas.GetLeft(rect), Canvas.GetTop(rect), 28, Colors.Red, rect.Width)); // Unknown text, should be dynamic
    }

    private Shape CreateTextShape(string text, double placeX, double placeY, double fontSize, System.Windows.Media.Color color, double width)
    {
        // do something with width to adjust the fontsize

        Shape textShape;

        var fontFamily = new System.Windows.Media.FontFamily("Arial");

        double fontHeight = Math.Ceiling(fontSize * fontFamily.LineSpacing);

        Typeface typeface = new Typeface(fontFamily, FontStyles.Normal, FontWeights.Normal, new FontStretch());

        FormattedText fText = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, fontSize, Brushes.Black, 1.25);

        Point textPosition1;
        textPosition1 = new Point(placeX, placeY - fontHeight);
        Path path = new Path();
        path.Data = fText.BuildGeometry(textPosition1);
        path.Fill = new SolidColorBrush(color);
        textShape = path;
        return textShape;
    }
}

XAML:

<Window x:Class="WpfApp2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp2"
        mc:Ignorable="d"
        Title="MainWindow" Height="1000" Width="1000">
    <Grid>
        <Canvas x:Name="canvas" MaxWidth="1000" MaxHeight="1000"/>
    </Grid>
</Window>

编辑2:

矩形的文本和大小未知,此方法必须是动态的。

c# .net wpf shapes
1个回答
0
投票
此结果以我想要的方式工作。

private Shape CreateTextShape(string text, double placeX, double placeY, System.Windows.Media.Color color, double width) { Shape textShape; double fontSize = 8; FontFamily fontFamily = new FontFamily("Arial"); double fontHeight = Math.Ceiling(fontSize * fontFamily.LineSpacing); Typeface typeface = new Typeface(fontFamily, FontStyles.Normal, FontWeights.Normal, new FontStretch()); FormattedText fText = new FormattedText(text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, fontSize, Brushes.Black, 1.25); double scale = width / (fText.Width); Path path = new Path(); path.Data = fText.BuildGeometry(new Point()); ScaleTransform transform = new ScaleTransform(scale, scale); path.Data.Transform = transform; Canvas.SetLeft(path, placeX); Canvas.SetTop(path, placeY - fontHeight * scale); path.Fill = new SolidColorBrush(color); textShape = path; return textShape; }

© www.soinside.com 2019 - 2024. All rights reserved.