본문 바로가기
VB.net

textbox 사이즈에 맞게 Font 크기 자동 조정

by 호야호잇 2024. 4. 19.
    Private Sub auto_fontSize()
        Dim g As Graphics = TB1.CreateGraphics()
        Dim textSize As SizeF = g.MeasureString(TB1.Text, TB1.Font)
        Dim scale As Single = Math.Min(TB1.Width / textSize.Width, TB1.Height / textSize.Height)
        Dim fontSize As Single = TB1.Font.Size * scale
        TB1.Font = New Font(TB1.Font.FontFamily, fontSize, TB1.Font.Style)

    End Sub

 

 

textBox 컨트롤의 Graphics object를 구함

 

text와 font를 매개변수로 넣고 sizeF 구조체로 너비와 높이 구함

 

scaling factor를 구하고 scale down 한 font size로 변경