当前位置: 首页>>代码示例>>C#>>正文


C# TextBlock.Measure方法代码示例

本文整理汇总了C#中TextBlock.Measure方法的典型用法代码示例。如果您正苦于以下问题:C# TextBlock.Measure方法的具体用法?C# TextBlock.Measure怎么用?C# TextBlock.Measure使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TextBlock的用法示例。


在下文中一共展示了TextBlock.Measure方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: StringSize

 static System.Drawing.SizeF StringSize(string text, int fontSize)
 {
     TextBlock txtMeasure = new TextBlock();
     txtMeasure.FontSize = fontSize;
     txtMeasure.Text = text;
     txtMeasure.Measure (new Size (1, 1));
     return new System.Drawing.SizeF((float)txtMeasure.ActualWidth, (float)txtMeasure.ActualHeight);
 }
开发者ID:xhcnb,项目名称:CrossGraphics,代码行数:8,代码来源:SilverlightGraphics.cs

示例2: StringSize

 static System.Drawing.SizeF StringSize(string text, int fontSize, FontFamily fontFamily, bool bold)
 {
     TextBlock txtMeasure = new TextBlock();
     txtMeasure.FontFamily = fontFamily;
     txtMeasure.FontSize = fontSize;
     txtMeasure.FontWeight = bold ? Windows.UI.Text.FontWeights.Bold : Windows.UI.Text.FontWeights.Normal;
     txtMeasure.Text = text;
     txtMeasure.Measure (new Size (1, 1));
     return new System.Drawing.SizeF((float)txtMeasure.ActualWidth, (float)txtMeasure.ActualHeight);
 }
开发者ID:h2oman,项目名称:CrossGraphics,代码行数:10,代码来源:XamlGraphics.cs

示例3: MeasureText

        /// <summary>
        /// Measures the text.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="fontFamily">The font family.</param>
        /// <param name="fontSize">Size of the font.</param>
        /// <param name="fontWeight">The font weight.</param>
        /// <returns>The text size.</returns>
        public OxySize MeasureText(string text, string fontFamily, double fontSize, double fontWeight)
        {
            if (string.IsNullOrEmpty(text))
            {
                return OxySize.Empty;
            }

            var tb = new TextBlock { Text = text };

            if (fontFamily != null)
            {
                tb.FontFamily = new FontFamily(fontFamily);
            }

            if (fontSize > 0)
            {
                tb.FontSize = fontSize;
            }

            tb.FontWeight = GetFontWeight(fontWeight);

            tb.Measure(new Size(1000, 1000));

            return new OxySize(tb.ActualWidth, tb.ActualHeight);
        }
开发者ID:huoxudong125,项目名称:oxyplot,代码行数:33,代码来源:RenderContext.cs

示例4: DrawText

        /// <summary>
        /// Draws the text.
        /// </summary>
        /// <param name="p">The position.</param>
        /// <param name="text">The text.</param>
        /// <param name="fill">The fill color.</param>
        /// <param name="fontFamily">The font family.</param>
        /// <param name="fontSize">Size of the font.</param>
        /// <param name="fontWeight">The font weight.</param>
        /// <param name="rotate">The rotation angle.</param>
        /// <param name="halign">The horizontal alignment.</param>
        /// <param name="valign">The vertical alignment.</param>
        /// <param name="maxSize">The maximum size of the text.</param>
        public void DrawText(
            ScreenPoint p,
            string text,
            OxyColor fill,
            string fontFamily,
            double fontSize,
            double fontWeight,
            double rotate,
            OxyPlot.HorizontalAlignment halign,
            OxyPlot.VerticalAlignment valign,
            OxySize? maxSize)
        {
            var tb = new TextBlock { Text = text, Foreground = new SolidColorBrush(fill.ToColor()) };

            // tb.SetValue(TextOptions.TextHintingModeProperty, TextHintingMode.Animated);
            if (fontFamily != null)
            {
                tb.FontFamily = new FontFamily(fontFamily);
            }

            if (fontSize > 0)
            {
                tb.FontSize = fontSize;
            }

            tb.FontWeight = GetFontWeight(fontWeight);

            tb.Measure(new Size(1000, 1000));
            var size = new Size(tb.ActualWidth, tb.ActualHeight);
            if (maxSize != null)
            {
                if (size.Width > maxSize.Value.Width)
                {
                    size.Width = maxSize.Value.Width;
                }

                if (size.Height > maxSize.Value.Height)
                {
                    size.Height = maxSize.Value.Height;
                }

                tb.Clip = new RectangleGeometry { Rect = new Rect(0, 0, size.Width, size.Height) };
            }

            double dx = 0;
            if (halign == OxyPlot.HorizontalAlignment.Center)
            {
                dx = -size.Width / 2;
            }

            if (halign == OxyPlot.HorizontalAlignment.Right)
            {
                dx = -size.Width;
            }

            double dy = 0;
            if (valign == OxyPlot.VerticalAlignment.Middle)
            {
                dy = -size.Height / 2;
            }

            if (valign == OxyPlot.VerticalAlignment.Bottom)
            {
                dy = -size.Height;
            }

            var transform = new TransformGroup();
            transform.Children.Add(new TranslateTransform { X = (int)dx, Y = (int)dy });
            if (!rotate.Equals(0))
            {
                transform.Children.Add(new RotateTransform { Angle = rotate });
            }

            transform.Children.Add(new TranslateTransform { X = (int)p.X, Y = (int)p.Y });
            tb.RenderTransform = transform;
            this.ApplyTooltip(tb);

            if (this.clip)
            {
                // add a clipping container that is not rotated
                var c = new Canvas();
                c.Children.Add(tb);
                this.Add(c);
            }
            else
            {
                this.Add(tb);
//.........这里部分代码省略.........
开发者ID:huoxudong125,项目名称:oxyplot,代码行数:101,代码来源:RenderContext.cs

示例5: ExtTextOut

        public void ExtTextOut(Canvas surface, double x, double y, ExtTextOutOptions options, Rect dimensions, string text, double[] dx)
        {
            TextBlock block = null;
            Grid container = null;
            double deviceX = LtoDX(x);
            double deviceY = LtoDY(y);
            double textWidth;
            double textHeight;
            Geometry clipGeometry = null;

            if (dx == null)
            {
                // No character placement
                block = new TextBlock
                    {
                        Inlines = {
                            new Run
                                {
                                    Text = text
                                }
                        }
                    };

                ApplyTextStyle(block, deviceY, true, options);

                //Canvas.SetLeft(block, deviceX);

                surface.Children.Add(block);
                block.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                textHeight = block.DesiredSize.Height;
                textWidth = block.DesiredSize.Width;
            }
            else
            {
                // Place individual characters
                int stringLength = text.Length;
                double currentPos = 0; // LtoDX(x);
                container = new Grid();

                for (int i = 0; i < stringLength; i++)
                {
                    block = new TextBlock
                        {
                            Inlines = {
                                new Run
                                {
                                    Text = text.Substring(i, 1)
                                }
                            }
                        };
                    //TextOptions.SetTextFormattingMode(block, TextFormattingMode.Display);
                    ApplyTextStyle(block, deviceY, false, null);

                    if (i > 0)
                    {
                        currentPos += ScaleWidth(dx[i - 1]);
                    }
                    block.Margin = new Thickness(currentPos, 0, 0, 0);
                    // TODO: Character width
                    //block.Width = dx[i];

                    container.Children.Add(block);
                }

                //Canvas.SetLeft(container, deviceX);
                ApplyTextContainerStyle(container, deviceY);

                surface.Children.Add(container);
                container.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                textHeight = container.DesiredSize.Height;
                textWidth = container.DesiredSize.Width;
            }

            // Horizontal alignment
            if ((_currentState.TextAlign & (ushort)TextAlignmentMode.TA_CENTER) == (ushort)TextAlignmentMode.TA_CENTER)
            {
                deviceX -= textWidth / 2.0;
            }
            else if ((_currentState.TextAlign & (ushort)TextAlignmentMode.TA_RIGHT) == (ushort)TextAlignmentMode.TA_RIGHT)
            {
                deviceX -= textWidth;
            }

            // Vertical Alignment
            if ((_currentState.TextAlign & (ushort)TextAlignmentMode.TA_BASELINE) == (ushort)TextAlignmentMode.TA_BASELINE)
            {
                double baseline;

            #if NETFX_CORE
                baseline = textHeight;
            #else
                if (block != null)
                {
                    var formattedText = new FormattedText(
                         text,
                         System.Globalization.CultureInfo.CurrentCulture,
                         block.FlowDirection,
                         new Typeface(block.FontFamily, block.FontStyle, block.FontWeight, block.FontStretch, block.FontFamily),
                         block.FontSize,
                         block.Foreground);
//.........这里部分代码省略.........
开发者ID:bbowyersmyth,项目名称:WMF2WPF,代码行数:101,代码来源:WpfGdi.cs

示例6: createFont

        public override object createFont(int face, int style, int size)
        {
            if (fontCache.ContainsKey(face | style | size))
            {
                return fontCache[face | style | size];
            }

            int a = 24;
            switch (size)
            {
                case 8: //com.codename1.ui.Font._fSIZE_1SMALL:
                    a = 15;
                    break;
                case 16: //com.codename1.ui.Font._fSIZE_1LARGE:
                    a = 56;
                    break;
            }
            
            NativeFont nf = new NativeFont();
            nf.height = a;
            nf.systemFace = face;
            nf.systemSize = size;
            nf.systemStyle = style;
            using (AutoResetEvent are = new AutoResetEvent(false))
            {
                System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    TextBlock tb = new TextBlock();
                    tb.FontSize = nf.height;
                    tb.Text = "Xp";
                    tb.Measure(new Size(1000000, 1000000));
                    nf.actualHeight = (int)tb.ActualHeight;
                    are.Set();
                });
                are.WaitOne();
            }

            if ((style & com.codename1.ui.Font._fSTYLE_1BOLD) == com.codename1.ui.Font._fSTYLE_1BOLD)
            {
                nf.bold = true;
            }
            if ((style & com.codename1.ui.Font._fSTYLE_1ITALIC) == com.codename1.ui.Font._fSTYLE_1ITALIC)
            {
                nf.italic = true;
            }

            fontCache[face | style | size] = nf;
            return nf;
        }
开发者ID:mehulsbhatt,项目名称:CodenameOne,代码行数:49,代码来源:SilverlightImplementation.cs

示例7: stringWidth

 public override int stringWidth(java.lang.Object n1, java.lang.String n2)
 {
     NativeFont font = f(n1);
     string str = toCSharp(n2);
     StringFontPair sfp = new StringFontPair(str, font);
     if (stringWidthCache.ContainsKey(sfp))
     {
         return stringWidthCache[sfp];
     }
     int result = 0;
     using (AutoResetEvent are = new AutoResetEvent(false))
     {
         System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
         {
             TextBlock tb = new TextBlock();
             tb.FontSize = font.height;
             tb.Text = str;
             tb.Measure(new Size(1000000, 1000000));
             result = (int)tb.ActualWidth;
             are.Set();
         });
         are.WaitOne();
     }
     stringWidthCache.Add(sfp, result);
     return result;
 }
开发者ID:mehulsbhatt,项目名称:CodenameOne,代码行数:26,代码来源:SilverlightImplementation.cs


注:本文中的TextBlock.Measure方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。