'Condensed'에 해당되는 글 1건

  1. 2008/12/12 Condensed TextBlock (5)
WPF2008/12/12 02:49


WPF를 사용하면서 가장 아쉬웠던 점 중 하나를 꼽으라면 바로 Textblock 혹은 TextBox에서의 자간조절이 불가능하다는 점이였습니다. 디자인 작업을 할 때 보다 세련된 느낌을 표현하기 위해서 Font에 자간을 조절하여 느낌을 많이 개선하곤 하는데요, WPF에서는 기본적으로 자간을 조절을 지원하지 않아 지금까지는 해당 텍스트를 이미지로 만들어 표현하다가, 도서관리 프로그램을 만들면서 새로 자간을 지원하는 TextBlock를 구현 했습니다.


    
        
    

    
    
    
    
    
    
    
    
    
    
        




100을 기준으로 크기가 증가할 수록 자간이 넓어지게 됩니다. 아래는 CondensedTextBlock 클래스의 전체 소스코드입니다.

public class CondensedTextBlock : FrameworkElement
{

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
        "Text", typeof(String), typeof(CondensedTextBlock), new FrameworkPropertyMetadata(
             "", FrameworkPropertyMetadataOptions.AffectsRender));

    public String Text
    {
        get { return (String)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); InvalidateVisual(); }
    }

    public FontFamily FontFamily
    {
        get { return (FontFamily)GetValue(FontFamilyProperty); }
        set { SetValue(FontFamilyProperty, value); InvalidateVisual(); }
    }

    public static readonly DependencyProperty FontFamilyProperty =
        DependencyProperty.Register("FontFamily", typeof(FontFamily), 
        typeof(CondensedTextBlock), 
        new FrameworkPropertyMetadata(new FontFamily("NanumGothic"), FrameworkPropertyMetadataOptions.AffectsRender));


    public Double FontStretch
    {
        get { return (double)GetValue(FontStretchProperty); }
        set { SetValue(FontStretchProperty, value); InvalidateVisual(); }
    }

    public static readonly DependencyProperty FontStretchProperty =
        DependencyProperty.Register("FontStretch", typeof(Double), 
        typeof(CondensedTextBlock), 
        new FrameworkPropertyMetadata((double)1, FrameworkPropertyMetadataOptions.AffectsRender));


    public Brush Foreground
    {
        get { return (Brush)GetValue(ForegroundProperty); }
        set { SetValue(ForegroundProperty, value); InvalidateVisual(); }
    }

    public static readonly DependencyProperty ForegroundProperty =
        DependencyProperty.Register("Foreground", typeof(Brush), 
        typeof(CondensedTextBlock), 
        new FrameworkPropertyMetadata(Brushes.Black, FrameworkPropertyMetadataOptions.AffectsRender));


    public double FontSize
    {
        get { return (double)GetValue(FontSizeProperty); }
        set { SetValue(FontSizeProperty, value); Height = value; }
    }

    public static readonly DependencyProperty FontSizeProperty =
        DependencyProperty.Register("FontSize", typeof(double), 
        typeof(CondensedTextBlock), 
        new FrameworkPropertyMetadata((double)11, FrameworkPropertyMetadataOptions.AffectsRender));


    
    protected override void OnRender(DrawingContext dc)
    {

        DrawingContext DrawingContext = dc;

        double XOffset = 0;
        if (Text == null) return;
        foreach (char Char in Text)
        {
            FormattedText FormattedText = new FormattedText(Char.ToString(), 
                CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                new Typeface(FontFamily, FontStyles.Normal, FontWeights.Normal, 
                FontStretches.Normal), FontSize, Foreground);

            DrawingContext.DrawText(FormattedText, new Point(XOffset, 0));
            XOffset += FormattedText.WidthIncludingTrailingWhitespace * FontStretch / 100;
        }

    }

    protected override Size MeasureOverride(Size availableSize)
    {
        availableSize.Height = FontSize * 1.2;
        return availableSize;
    }

}

아래는 전체 소스코드와 샘플 프로젝트입니다. 질문은 리플이나 이메일로 보내주세요~ !!


크리에이티브 커먼즈 라이선스
Creative Commons License
Posted by 곡스