Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "Class 'Environment' cannot be used as an attribute" received when @Environment is called

Tags:

swift

swiftui

I'm trying to include Dynamic Type with a custom font in SwiftUI, and this error keeps getting called when I'm trying to get the environment's sizeCategory.

I've tried using @Environment to get objects other than sizeCategory, but it keeps throwing the same error.

I'm using a modified of the code from this StackOverflow post, on Xcode 11 beta 5. It seems to work for everyone else there, so I'm just really confused why it isn't working for me.

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

Complete code file:

//
//  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
    }
  }

}
like image 675
Rishi Kothari Avatar asked Aug 06 '19 14:08

Rishi Kothari


2 Answers

I had this same error message. It turns out I had defined another type named Environment which the compiler was picking up.

this works for me: @SwiftUI.Environment(\.colorScheme) var colorScheme : ColorScheme

Hope this helps you

like image 61
Bil Moorhead Avatar answered Oct 15 '22 04:10

Bil Moorhead


if you have another object in your application named Environment, that would be clashing with SwiftUI's environment keyword. change the name of your custom Environment object

like image 35
H K Avatar answered Oct 15 '22 04:10

H K