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


C# TextAlignment.HasFlag方法代码示例

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


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

示例1: CreateTextRenderingSettings

        /// <summary>
        /// Calculates the position and origin vectors that should be used when calling a SpriteBatch.DrawString method.
        /// </summary>
        /// <param name="font">The SpriteFont that will be used to draw the string.</param>
        /// <param name="text">The string that will be rendered.</param>
        /// <param name="bounds">The rendering boundary.</param>
        /// <param name="alignment">The alignment of the string.</param>
        /// <param name="shiftPositionUsingOrigin">
        /// If true the position will be at the center of the bounding box and origin will be modified to move text into place.
        /// If false, the origin will remain at 0,0 and the position will be directly modified.
        /// </param>
        public static TextRenderingSettings CreateTextRenderingSettings(SpriteFont font, string text, Rectangle bounds, TextAlignment alignment, bool shiftPositionUsingOrigin)
        {
            // Start by determining the amount of space the text will require
                        Vector2 sizeOfText = font.MeasureString(text);

                        float halfWidthOfBoundary = bounds.Width * 0.5f;
                        float halfHeightOfBoundary = bounds.Height * 0.5f;
                        float halfOfWidthRequiredToRenderText = sizeOfText.X * 0.5f;
                        float halfOfHeightRequiredToRenderText = sizeOfText.Y * 0.5f;
                        float deltaBetweenHalfWidthOfBoundaryAndHalfOfWidthRequiredToRenderText = halfWidthOfBoundary - halfOfWidthRequiredToRenderText;
                        float deltaBetweenHalfHeightOfBoundaryAndHalfOfHeightRequiredToRenderText = halfHeightOfBoundary - halfOfHeightRequiredToRenderText;

                        Vector2 position;
                        Vector2 origin;

                        if(shiftPositionUsingOrigin == true)
                        {
                                // Position the text at the center of the allowed boundry
                                position = new Vector2(halfWidthOfBoundary + bounds.X, halfHeightOfBoundary + bounds.Y);

                                // Set the origin at the center of the text
                                origin = new Vector2(halfOfWidthRequiredToRenderText, halfOfHeightRequiredToRenderText);
                        }
                        else
                        {
                                // Position the text at the center of the allowed boundry
                                position = new Vector2(deltaBetweenHalfWidthOfBoundaryAndHalfOfWidthRequiredToRenderText, deltaBetweenHalfHeightOfBoundaryAndHalfOfHeightRequiredToRenderText);

                                // Position the origin at the center of the allowed boundry
                                origin = Vector2.Zero;
                        }

                        //
                        // The below code can seem a bit tricky at first glance, but it works because shifting
                        // the origin in a direction, causes the text to be moved in the opposite direction.
                        //

                        if (alignment.HasFlag(TextAlignment.Left))
                        {
                                if (shiftPositionUsingOrigin == true)
                                {
                                        // Shift the origin to the right
                                        origin.X += deltaBetweenHalfWidthOfBoundaryAndHalfOfWidthRequiredToRenderText;
                                }
                                else
                                {
                                        // Shift the position to the left
                                        position.X -= deltaBetweenHalfWidthOfBoundaryAndHalfOfWidthRequiredToRenderText;
                                }
                        }

                        if (alignment.HasFlag(TextAlignment.Right))
                        {
                                if (shiftPositionUsingOrigin == true)
                                {
                                        // Shift the origin to the left
                                        origin.X -= deltaBetweenHalfWidthOfBoundaryAndHalfOfWidthRequiredToRenderText;
                                }
                                else
                                {
                                        // Shift the position to the right
                                        position.X += deltaBetweenHalfWidthOfBoundaryAndHalfOfWidthRequiredToRenderText;
                                }
                        }

                        if (alignment.HasFlag(TextAlignment.Top))
                        {
                                if (shiftPositionUsingOrigin == true)
                                {
                                        // Shift the origin down
                                        origin.Y += deltaBetweenHalfHeightOfBoundaryAndHalfOfHeightRequiredToRenderText;
                                }
                                else
                                {
                                        // Shift the position up
                                        position.Y -= deltaBetweenHalfHeightOfBoundaryAndHalfOfHeightRequiredToRenderText;
                                }
                        }

                        if (alignment.HasFlag(TextAlignment.Bottom))
                        {
                                if (shiftPositionUsingOrigin == true)
                                {
                                        // Shift the origin up
                                        origin.Y -= deltaBetweenHalfHeightOfBoundaryAndHalfOfHeightRequiredToRenderText;
                                }
                                else
                                {
                                        // Shift the position down
//.........这里部分代码省略.........
开发者ID:TheProjecter,项目名称:xna-space-invaders,代码行数:101,代码来源:TextRenderingHelper.cs


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