sourcetip

Swift UI에서 텍스트에 대한 사용자 지정 글꼴 크기

fileupload 2023. 10. 20. 14:08
반응형

Swift UI에서 텍스트에 대한 사용자 지정 글꼴 크기

제가 보기에는 시스템 글꼴 크기를 21포인트 크기의 중간 크기로 사용하고 싶다는 라벨이 있습니다.
만든 글꼴을 다시 사용하기 위해 사용자 지정 확장자를 만들었습니다.

extension Font {
    static var primaryButton: Font {
        return Font.custom("SFUIDisplay-Light", size: 21)
    }
}

그러나 이것은 아무런 효과가 없습니다.문자열을 다음으로 변경했습니다.HelveticaNeue-UltraLight효과가 있었고 그래서 아마 제 생각엔SFUIDisplay-Light단순히 잘못된 글꼴 이름입니다.
폰트 북에는 다음과 같이 적혀있습니다.SFProText-Light, 하지만 그것도 저에겐 통하지 않았습니다.

Swift에서 샌프란시스코 글꼴의 올바른 글꼴 이름은 무엇입니까?UI?
또는: 시스템 글꼴을 사용하여 글꼴 크기를 설정하려면 어떻게 해야 합니까?

다음을 사용하여 시스템 글꼴의 명시적 크기를 설정할 수 있습니다.

.font(.system(size: 60))

사용자 지정 글꼴 크기를 이렇게 설정할 수 있습니다.

.font(.custom("FONT_NAME", size: 20))
Text("Default font design").font(Font.system(size:30, design: .default))
Text("Monospaced font design").font(Font.system(size:30, design: .monospaced))
Text("Rounded font design").font(Font.system(size:30, design: .rounded))
Text("Serif font design").font(Font.system(size:30, design: .serif))

Text("Large Title").font(.largeTitle)
Text("Title").font(.title)
Text("Headline").font(.headline)
Text("SubHeadline").font(.subheadline)
Text("Body").font(.body)
Text("Callout").font(.callout)
Text("Caption").font(.caption)
Text("FootNote").font(.footnote)

Text("Custom font").font(Font.custom("OpenSans-Bold", size: 12.3))

자세한 사항은 다음을 참조하시기 바랍니다: https://github.com/arjun011/SwiftUI-Controls

텍스트에 대한 사용자 지정 글꼴을 설정하려면 헬베티카 노이에(Helvetica Neue)라고 하면 됩니다. 그러면 다음과 같은 작업을 수행할 수 있습니다.

Text("Hello World")
.font(.custom("Helvetica Neue", size: 20))

만약 당신이 시스템 폰트를 사용하고 싶다면, 그 경우에 당신은 다음과 같은 것을 할 수 있습니다.

Text("Hello World")
.font(.system(size: 20, weight: .light, design: .default))
// Using system font "San Francisco"
let fontSystem = Font.system(size: 13.0)

// Using installed custom font
let fontCustom = Font.custom("YOUR FONT NAME", size: 13.0)

위치를 지정할 수 있습니다.font*글꼴 스타일을 설정해야 하는 위치에 대해 상수를 지정합니다.

https://developer.apple.com/documentation/swiftui/font 의 "시스템 글꼴 가져오기" 섹션에서 자세히 확인하십시오.

특정 크기의 시스템 글꼴을 추가하는 옵션을 검색하는 경우 동적 유형으로 확장됩니다.

//Usage:
Text("Hello World").systemFont(size: 8.0, relativeTo: .caption, weight: .semibold)

extension View {
    func systemFont(size: CGFloat, relativeTo textStyle: Font.TextStyle, weight: Font.Weight = .regular) -> some View {
        modifier(SystemText(size: size, relativeTo: textStyle, weight: weight))
    }
}
struct SystemText: ViewModifier {
    @ScaledMetric var fontSize: CGFloat
    private let weight: Font.Weight

    init(size: CGFloat, relativeTo textStyle: Font.TextStyle, weight: Font.Weight = .regular) {
        _fontSize = ScaledMetric(wrappedValue: size, relativeTo: textStyle)
        self.weight = weight
    }

    func body(content: Content) -> some View {
        content.font(.system(size: fontSize).weight(weight))
    }
}

저는 이렇게 합니다.

HStack {
 Text("hello world")
  .customFont(AppFonts.WorkSansSemiBold())
   .foregroundColor(Color(.label))
 }

마법이 여기에 있습니다.

import SwiftUI

public enum AppFonts {
    case WorkSansSemiBold(size: CGFloat = 20)
    case WorkSansMedium(size: CGFloat = 17)
    case WorkSansBold(size: CGFloat = 34)
    
    var rawValue: Font? {
        switch self {
        case .WorkSansSemiBold(let size):
            return Font.custom("WorkSans-SemiBold", size: size)
        case .WorkSansBold(let size):
            return Font.custom("WorkSans-Bold", size: size)
        case .WorkSansMedium(size: let size):
            return Font.custom("WorkSans-Medium", size: size)
        }
    }
}

extension View {
    func customFont(_ customFont: AppFonts) -> some View {
        self.font(customFont.rawValue)
    }
}

맞춤 사이즈가 필요할 때는

.customFont(AppFonts.WorkSansSemiBold(size: 14))

언급URL : https://stackoverflow.com/questions/56465083/custom-font-size-for-text-in-swiftui

반응형