첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ TextGeometry.cs

using System.Globalization;
using System.Windows;
using System.Windows.Media;

namespace TestProject
{
    /// <summary>
    /// 텍스트 도형
    /// </summary>
    public class TextGeometry
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 텍스트
        /// </summary>
        private string text = string.Empty;

        /// <summary>
        /// 폰트 패밀리
        /// </summary>
        private FontFamily fontFamily = new FontFamily();

        /// <summary>
        /// 폰트 스타일
        /// </summary>
        private FontStyle fontStyle = FontStyles.Normal;

        /// <summary>
        /// 폰트 두께
        /// </summary>
        private FontWeight fontWeight = FontWeights.Normal;

        /// <summary>
        /// 폰트 범위
        /// </summary>
        private FontStretch fontStretch = FontStretches.Normal;

        /// <summary>
        /// 폰트 크기
        /// </summary>
        private double fontSize = 24d;

        /// <summary>
        /// 원점
        /// </summary>
        private Point originPoint = new Point(0, 0);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 텍스트 - Text

        /// <summary>
        /// 텍스트
        /// </summary>
        public string Text
        {
            set
            {
                this.text = value;
            }
            get
            {
                return this.text;
            }
        }

        #endregion
        #region 폰트 패밀리 - FontFamily

        /// <summary>
        /// 폰트 패밀리
        /// </summary>
        public FontFamily FontFamily
        {
            set
            {
                this.fontFamily = value;
            }
            get
            {
                return this.fontFamily;
            }
        }

        #endregion
        #region 폰트 스타일 - FontStyle

        /// <summary>
        /// 폰트 스타일
        /// </summary>
        public FontStyle FontStyle
        {
            set
            {
                this.fontStyle = value;
            }
            get
            {
                return this.fontStyle;
            }
        }

        #endregion
        #region 폰트 두께 - FontWeight

        /// <summary>
        /// 폰트 두께
        /// </summary>
        public FontWeight FontWeight
        {
            set
            {
                this.fontWeight = value;
            }
            get
            {
                return this.fontWeight;
            }
        }

        #endregion
        #region 폰트 범위

        /// <summary>
        /// 폰트 범위
        /// </summary>
        public FontStretch FontStretch
        {
            set
            {
                this.fontStretch = value;
            }
            get
            {
                return this.fontStretch;
            }
        }

        #endregion
        #region 폰트 크기 - FontSize

        /// <summary>
        /// 폰트 크기
        /// </summary>
        public double FontSize
        {
            set
            {
                this.fontSize = value;
            }
            get
            {
                return this.fontSize;
            }
        }

        #endregion
        #region 원점 - Origin

        /// <summary>
        /// 원점
        /// </summary>
        public Point Origin
        {
            set
            {
                this.originPoint = value;
            }
            get
            {
                return this.originPoint;
            }
        }

        #endregion
        #region 도형 - Geometry

        /// <summary>
        /// 도형
        /// </summary>
        public Geometry Geometry
        {
            get
            {
                FormattedText formattedText = new FormattedText
                (
                    Text,
                    CultureInfo.CurrentCulture, 
                    FlowDirection.LeftToRight,
                    new Typeface
                    (
                        FontFamily,
                        FontStyle, 
                        FontWeight,
                        FontStretch
                    ), 
                    FontSize,
                    Brushes.Black
                );

                return formattedText.BuildGeometry(Origin);
            }
        }

        #endregion
        #region 패스 도형 - PathGeometry

        /// <summary>
        /// 패스 도형
        /// </summary>
        public PathGeometry PathGeometry
        {
            get
            {
                return PathGeometry.CreateFromGeometry(Geometry);
            }
        }

        #endregion
    }
}

 

728x90

 

▶ MainWindow.xaml

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Title="FormattedText 클래스 : BuildGeometry 메소드를 사용해 텍스트 효과 만들기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Window.Resources>
        <local:TextGeometry x:Key="HollowTextGeometryKey"
            Text="Hollow"
            FontFamily="Times New Roman" 
            FontSize="192"
            FontWeight="Bold" />
        <local:TextGeometry x:Key="ShadowTextGeometryKey"
            Text="Shadow"
            FontFamily="Times New Roman"
            FontSize="192"
            FontWeight="Bold" />
    </Window.Resources>
    <TabControl>
        <TabItem Header="Hollow">
            <Path
                Stroke="Blue"
                StrokeThickness="5"
                Data="{Binding Source={StaticResource HollowTextGeometryKey}, Path=Geometry}" />
        </TabItem>
        <TabItem Header="Dotted">
            <Path
                Stroke="Blue"
                StrokeThickness="5"
                StrokeDashArray="{Binding Source={x:Static DashStyles.Dot}, Path=Dashes}"
                StrokeDashCap="Round"
                Data="{Binding Source={StaticResource HollowTextGeometryKey}, Path=Geometry}" />
        </TabItem>
        <TabItem Header="Shadow">
            <Canvas>
                <Path Canvas.Left="12" Canvas.Top="12"
                    Fill="DarkGray"
                    Data="{Binding Source={StaticResource ShadowTextGeometryKey}, Path=Geometry}" />
                <Path
                    Fill="White" 
                    Stroke="Black"
                    Data="{Binding Source={StaticResource ShadowTextGeometryKey}, Path=Geometry}" />
            </Canvas>
        </TabItem>
    </TabControl>
</Window>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요