首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用@Environment时收到错误"Class 'Environment‘不能用作属性“

调用@Environment时收到错误"Class 'Environment‘不能用作属性“
EN

Stack Overflow用户
提问于 2019-08-06 22:49:41
回答 3查看 2.4K关注 0票数 9

我尝试在SwiftUI中包含具有自定义字体的动态类型,但当我试图获取环境的sizeCategory时,此错误不断被调用。

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

我使用的是Xcode11Beta5上的this StackOverflow帖子中的代码修改版,它似乎适用于其他所有人,所以我真的很困惑为什么它不适用于我。

代码语言:javascript
复制
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

完整的代码文件:

代码语言:javascript
复制
//
//  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
    }
  }

}
EN

回答 3

Stack Overflow用户

发布于 2019-09-02 11:51:41

我也收到了同样的错误信息。原来我定义了另一个名为Environment的类型,编译器正在拾取该类型。

这对我来说很有效:@SwiftUI.Environment(\.colorScheme) var colorScheme : ColorScheme

希望这能对你有所帮助

票数 52
EN

Stack Overflow用户

发布于 2020-11-20 11:28:34

如果您的应用程序中有另一个名为Environment的对象,则会与SwiftUI的环境关键字发生冲突。请更改自定义Environment对象的名称

票数 5
EN

Stack Overflow用户

发布于 2019-08-06 23:18:04

我就是那个回答你提到的问题的人。我刚刚用beta 5测试了你发布的代码,它工作得很好。但是,请注意,使用SwiftUI时,您可能会在某些地方得到错误,而真正的错误在其他地方。我敢打赌,你的代码的其他部分可能有不平衡的括号。也可能是完全不同的东西。再次检查,如果问题仍然存在,请发布您的完整代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57378736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档