gpt4 book ai didi

swift - 调用@Environment 时收到错误 "Class ' 环境'不能用作属性”

转载 作者:行者123 更新时间:2023-12-03 09:23:29 25 4
gpt4 key购买 nike

我正在尝试在 SwiftUI 中包含带有自定义字体的动态类型,并且当我尝试获取环境的 sizeCategory 时不断调用此错误.

我尝试使用@Environment 来获取除 sizeCategory 以外的对象,但它不断抛出相同的错误。

我正在使用来自 this 的修改后的代码StackOverflow 帖子,在 Xcode 11 beta 5 上。它似乎适用于那里的其他所有人,所以我真的很困惑为什么它对我不起作用。

struct RawlineFont: ViewModifier {
var textStyle: Font.TextStyle

@Environment(\.sizeCategory) var sizeCategory : ContentSizeCategory

init(_ textStyle: Font.TextStyle = .body) {
self.textStyle = textStyle
}

func body(content: Content) -> some View {
content.font(getFont())
}

func getFont() -> Font {
switch(self.sizeCategory) {
case .extraSmall:
return Font.custom("Rawline", size: 16 * getStyleFactor())
case .small:
return Font.custom("Rawline", size: 21 * getStyleFactor())
case .medium:
return Font.custom("Rawline", size: 24 * getStyleFactor())
case .large:
return Font.custom("Rawline", size: 28 * getStyleFactor())
case .extraLarge:
return Font.custom("Rawline", size: 32 * getStyleFactor())
case .extraExtraLarge:
return Font.custom("Rawline", size: 36 * getStyleFactor())
case .extraExtraExtraLarge:
return Font.custom("Rawline", size: 40 * getStyleFactor())
case .accessibilityMedium:
return Font.custom("Rawline", size: 48 * getStyleFactor())
case .accessibilityLarge:
return Font.custom("Rawline", size: 52 * getStyleFactor())
case .accessibilityExtraLarge:
return Font.custom("Rawline", size: 60 * getStyleFactor())
case .accessibilityExtraExtraLarge:
return Font.custom("Rawline", size: 66 * getStyleFactor())
case .accessibilityExtraExtraExtraLarge:
return Font.custom("Rawline", size: 72 * getStyleFactor())
@unknown default:
return Font.custom("Rawline", size: 36 * getStyleFactor())
}
}

func getStyleFactor() -> CGFloat {
switch textStyle {
case .caption:
return 0.6
case .footnote:
return 0.7
case .subheadline:
return 0.8
case .callout:
return 0.9
case .body:
return 1.0
case .headline:
return 1.2
case .title:
return 1.5
case .largeTitle:
return 2.0
@unknown default:
return 1.0
}
}

}

Error message

完整的代码文件:

//
// Fonts.swift
// Team Study
//
// Created by Aditya Chugh on 2019-07-02.
// Copyright © 2019 Aditya Chugh. All rights reserved.
//

import SwiftUI

class Rawline {

static let extraLight = "RawlineExtraLight-Regular"
static let extraLightItalic = "RawlineExtraLight-Italic"

static let light = "RawlineLight-Regular"
static let lightItalic = "RawlineLight-Italic"

static let thin = "RawlineThin-Regular"
static let thinItalic = "Rawline-ThinItalic"

static let regular = "Rawline-Regular"
static let italic = "Rawline-Italic"

static let medium = "RawlineMedium-Regular"
static let mediumItalic = "RawlineMedium-Italic"

static let semiBold = "RawlineSemiBold-Regular"
static let semiBoldItalic = "RawlineSemiBold-Italic"

static let bold = "Rawline-Bold"
static let boldItalic = "Rawline-BoldItalic"

static let extraBold = "RawlineExtraBold-Regular"
static let extraBoldItalic = "RawlineExtraBold-Italic"

static let black = "RawlineBlack-Regular"
static let blackItalic = "RawlineBlack-Italic"

}

struct RawlineFont: ViewModifier {
var textStyle: Font.TextStyle

@Environment(\.sizeCategory) var sizeCategory : ContentSizeCategory

init(_ textStyle: Font.TextStyle = .body) {
self.textStyle = textStyle
}

func body(content: Content) -> some View {
content.font(getFont())
}

func getFont() -> Font {
switch(self.sizeCategory) {
case .extraSmall:
return Font.custom("Rawline", size: 16 * getStyleFactor())
case .small:
return Font.custom("Rawline", size: 21 * getStyleFactor())
case .medium:
return Font.custom("Rawline", size: 24 * getStyleFactor())
case .large:
return Font.custom("Rawline", size: 28 * getStyleFactor())
case .extraLarge:
return Font.custom("Rawline", size: 32 * getStyleFactor())
case .extraExtraLarge:
return Font.custom("Rawline", size: 36 * getStyleFactor())
case .extraExtraExtraLarge:
return Font.custom("Rawline", size: 40 * getStyleFactor())
case .accessibilityMedium:
return Font.custom("Rawline", size: 48 * getStyleFactor())
case .accessibilityLarge:
return Font.custom("Rawline", size: 52 * getStyleFactor())
case .accessibilityExtraLarge:
return Font.custom("Rawline", size: 60 * getStyleFactor())
case .accessibilityExtraExtraLarge:
return Font.custom("Rawline", size: 66 * getStyleFactor())
case .accessibilityExtraExtraExtraLarge:
return Font.custom("Rawline", size: 72 * getStyleFactor())
@unknown default:
return Font.custom("Rawline", size: 36 * getStyleFactor())
}
}

func getStyleFactor() -> CGFloat {
switch textStyle {
case .caption:
return 0.6
case .footnote:
return 0.7
case .subheadline:
return 0.8
case .callout:
return 0.9
case .body:
return 1.0
case .headline:
return 1.2
case .title:
return 1.5
case .largeTitle:
return 2.0
@unknown default:
return 1.0
}
}

}

最佳答案

我有同样的错误信息。事实证明,我定义了另一个名为 Environment 的类型,编译器正在使用它。

这对我有用:@SwiftUI.Environment(\.colorScheme) var colorScheme : ColorScheme
希望这对你有帮助

关于swift - 调用@Environment 时收到错误 "Class ' 环境'不能用作属性”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57378736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com