첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ CustomTextBlock.cs

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

namespace TestProject
{
    /// <summary>
    /// 커스텀 텍스트 블럭
    /// </summary>
    public class CustomTextBlock : FrameworkElement
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Dependency Property
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 폰트 패밀리 속성 - FontFamilyProperty

        /// <summary>
        /// 폰트 패밀리 속성
        /// </summary>
        public static readonly DependencyProperty FontFamilyProperty = DependencyProperty.Register
        (
            "FontFamily",
            typeof(FontFamily), 
            typeof(CustomTextBlock), 
            new FrameworkPropertyMetadata
            (
                new FontFamily("나눔고딕코딩"),
                FrameworkPropertyMetadataOptions.AffectsRender
            )
        );

        #endregion
        #region 폰트 크기 속성 - FontSizeProperty

        /// <summary>
        /// 폰트 크기 속성
        /// </summary>
        public static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register
        (
            "FontSize",
            typeof(double), 
            typeof(CustomTextBlock), 
            new FrameworkPropertyMetadata
            (
                (double)11,
                FrameworkPropertyMetadataOptions.AffectsRender
            )
        );

        #endregion
        #region 폰트 확장 속성 - FontStretchProperty

        /// <summary>
        /// 폰트 확장 속성
        /// </summary>
        public static readonly DependencyProperty FontStretchProperty = DependencyProperty.Register
        (
            "FontStretch",
            typeof(double), 
            typeof(CustomTextBlock), 
            new FrameworkPropertyMetadata
            (
                (double)1,
                FrameworkPropertyMetadataOptions.AffectsRender
            )
        );

        #endregion
        #region 전경 브러시 속성 - ForegroundProperty

        /// <summary>
        /// 전경 브러시 속성
        /// </summary>
        public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register
        (
            "Foreground",
            typeof(Brush), 
            typeof(CustomTextBlock), 
            new FrameworkPropertyMetadata
            (
                Brushes.Black,
                FrameworkPropertyMetadataOptions.AffectsRender
            )
        );

        #endregion
        #region 텍스트 속성 - TextProperty

        /// <summary>
        /// 텍스트 속성
        /// </summary>
        public static readonly DependencyProperty TextProperty = DependencyProperty.Register
        (
            "Text",
            typeof(string),
            typeof(CustomTextBlock),
            new FrameworkPropertyMetadata
            (
                "",
                FrameworkPropertyMetadataOptions.AffectsRender
            )
        );

        #endregion

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

        #region 폰트 패밀리 - FontFamily

        /// <summary>
        /// 폰트 패밀리
        /// </summary>
        public FontFamily FontFamily
        {
            get
            {
                return (FontFamily)GetValue(FontFamilyProperty);
            }
            set
            {
                SetValue(FontFamilyProperty, value);
                
                InvalidateVisual();
            }
        }

        #endregion
        #region 폰트 크기 - FontSize

        /// <summary>
        /// 폰트 크기
        /// </summary>
        public double FontSize
        {
            get
            {
                return (double)GetValue(FontSizeProperty);
            }
            set
            {
                SetValue(FontSizeProperty, value);
                
                Height = value;
            }
        }

        #endregion
        #region 폰트 확장 - FontStretch

        /// <summary>
        /// 폰트 확장
        /// </summary>
        public Double FontStretch
        {
            get
            {
                return (double)GetValue(FontStretchProperty);
            }
            set
            {
                SetValue(FontStretchProperty, value);
                
                InvalidateVisual();
            }
        }

        #endregion
        #region 전경 브러시 - Foreground

        /// <summary>
        /// 전경 브러시
        /// </summary>
        public Brush Foreground
        {
            get
            {
                return (Brush)GetValue(ForegroundProperty);
            }
            set
            {
                SetValue(ForegroundProperty, value);
                
                InvalidateVisual();
            }
        }

        #endregion
        #region 텍스트 - Text

        /// <summary>
        /// 텍스트
        /// </summary>
        public String Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set
            {
                SetValue(TextProperty, value);
                
                InvalidateVisual();
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 측정하기 (오바라이드용) - MeasureOverride(availableSize)

        /// <summary>
        /// 측정하기 (오바라이드용)
        /// </summary>
        /// <param name="availableSize">이용 가능한 크기</param>
        /// <returns>측정 크기</returns>
        protected override Size MeasureOverride(Size availableSize)
        {
            availableSize.Height = FontSize * 1.2;

            return availableSize;
        }

        #endregion
        #region 렌더링시 처리하기 - OnRender(drawingContext)

        /// <summary>
        /// 렌더링시 처리하기
        /// </summary>
        /// <param name="drawingContext">드로잉 컨텍스트</param>
        protected override void OnRender(DrawingContext drawingContext)
        {
            double offsetX = 0;

            if(Text == null)
            {
                return;
            }

            foreach(char character in Text)
            {
                FormattedText formattedText = new FormattedText
                (
                    character.ToString(), 
                    CultureInfo.CurrentCulture,
                    FlowDirection.LeftToRight,
                    new Typeface
                    (
                        FontFamily,
                        FontStyles.Normal,
                        FontWeights.Normal, 
                        FontStretches.Normal
                    ),
                    FontSize,
                    Foreground
                );

                drawingContext.DrawText(formattedText, new Point(offsetX, 0));

                offsetX += formattedText.WidthIncludingTrailingWhitespace * FontStretch / 100;
            }
        }

        #endregion
    }
}

 

728x90

 

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    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="FrameworkElement 클래스 : 장평을 설정하는 커스텀 텍스트 블럭 사용하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <StackPanel>
            <StackPanel.Resources>
                <Style TargetType="{x:Type local:CustomTextBlock}">
                    <Setter Property="Text"       Value="테스트 문자열 입니다."/>
                    <Setter Property="FontSize"   Value="25"           />
                    <Setter Property="FontFamily" Value="나눔고딕코딩" />
                </Style>
            </StackPanel.Resources>
            <TextBlock Text="장평 120" />
            <local:CustomTextBlock FontStretch="120" />
            <TextBlock Text="장평 110" />
            <local:CustomTextBlock FontStretch="110" />
            <TextBlock Text="장평 100" />
            <local:CustomTextBlock FontStretch="100" />
            <TextBlock Text="장평90" />
            <local:CustomTextBlock FontStretch="90" />
            <TextBlock Text="장평 80" />
            <local:CustomTextBlock FontStretch="80" />
        </StackPanel>        
    </Grid>
</Window>
728x90
반응형
그리드형(광고전용)
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요