2017-09-06 1 views
1

스위프트 3에서 스위프트 4로 프로젝트를 이전했으며 글꼴 속성을 얻는 방법을 UIFontDescriptor에서 찾을 수 없습니다. swift에서 글꼴 속성 가져 오기

@objc convenience init(myCoder aDecoder: NSCoder) { 
     if let fontDescriptor = aDecoder.decodeObject(forKey: "UIFontDescriptor") as? UIFontDescriptor { 
      if let fontAttribute = fontDescriptor.fontAttributes["NSCTFontUIUsageAttribute"] as? String { 
       var fontName = "" 
       switch fontAttribute { 
       case "CTFontRegularUsage": 
        fontName = Roboto.regular.rawValue 
       case "CTFontEmphasizedUsage", "CTFontBoldUsage": 
        fontName = Roboto.bold.rawValue 
       default: 
        fontName = Roboto.regular.rawValue 
       } 
       self.init(name: fontName, size: fontDescriptor.pointSize)! 
      } 
      else { 
       self.init(myCoder: aDecoder) 
      } 
     } 
     else { 
      self.init(myCoder: aDecoder) 
     } 
    } 

그러나 빠른 4 라인에서

가 :

if let fontAttribute = fontDescriptor.fontAttributes["NSCTFontUIUsageAttribute"] as? String 

에 오류가 있습니다 : 여기에 빠른 3에서 완벽하게 작동하는 코드입니다 Ambiguous reference to member 'subscript'. 아이디어를 어떻게 변경할 수 있습니까?

감사합니다!

+0

을 여기에 워드 프로세서는, 스위프트 4 변경 사항이 강조 표시됩니다. 아무것도 주목 해? https://developer.apple.com/documentation/uikit/uifontdescriptor?changes=latest_minor – matt

+0

또한 무엇을하려하십니까? 사용자 정의 글꼴로 동적 유형을 지원하려는 경우 완전히 새로운 방식으로 지원됩니다. https://developer.apple.com/documentation/uikit/uifontmetrics – matt

+0

@noname, 문제 해결 방법을 알려주십시오. – Rbar

답변

1

사용자 정의 글꼴을 동적 유형 글꼴로 처리하려는 것처럼 보입니다. iOS 11에서 지원되는 방법은 UIFontMetrics입니다.

+0

새로운 UIFontMetrics를 구현하는 방법에 대한 실제 예를 들려 주겠습니까? 'if let fontAttribute = fontDescriptor.fontAttributes [ "NSCTFontUIUsageAttribute"]에서 줄 바꾸기를 시도 했습니까? 당신이 공유 한 링크 중 하나 (https://developer.apple.com/documentation/uikit/uifontdescriptor.attributename?changes=latest_minor)에서 말했듯이 문자열을 {'to'fontAttribute = fontDescriptor.AttributeName {'로 보내지 만 행운이 없다면 .. – Rbar

+0

내 문제는 정확히 OP와 동일합니다. 다른 질문을 게시하는 것은 불필요하며 합법적으로 중복으로 표시됩니다. 나는 당신이 단지 링크가 아닌 답을 뒷받침 할 실제 코드를 제공 할 수 있는지를 묻고있었습니다. – Rbar

+0

@Rbar _you_가 무엇을하는지 모르겠습니다. 내가 아는 한, UIFontMetrics는 사용자의 동적 유형 크기 환경 설정에 따라 _any_ 글꼴의 크기가 조절 된 글꼴을 얻을 수있게 해줍니다.이 글꼴은 OP가 수행하려고 시도했던 것입니다. – matt

0
let font: UIFont 

let fontAttribute = UIFontDescriptor.AttributeName("NSCTFontUIUsageAttribute") 

let fontUsageAttribute = font.fontDescriptor.fontAttributes[fontAttribute] as? String 
0

여기 내 해결책이 있습니다. 나를위한 직장

@objc convenience init(myCoder aDecoder: NSCoder) { 
    if let fontDescriptor = aDecoder.decodeObject(forKey: "UIFontDescriptor") as? UIFontDescriptor { 

      let fontAttribute = UIFontDescriptor.AttributeName("NSCTFontUIUsageAttribute") 
     if let fontAttribute = fontDescriptor.fontAttributes[fontAttribute] as? String { 

      var fontName = "" 
      switch fontAttribute { 
      case "CTFontRegularUsage": 
       fontName = Roboto.regular.rawValue 
      case "CTFontEmphasizedUsage", "CTFontBoldUsage": 
       fontName = Roboto.bold.rawValue 
      default: 
       fontName = Roboto.regular.rawValue 
      } 
      self.init(name: fontName, size: fontDescriptor.pointSize)! 
     } 
     else { 
      self.init(myCoder: aDecoder) 
     } 
    } 
    else { 
     self.init(myCoder: aDecoder) 
    } 
} 
0

1) 모든 프로젝트/대상 빌드 설정에 스위프트 언어 버전 4.x (지금은 4.0)가 표시되어 있는지 확인하십시오. 이 세트가 없으면 새 UIFontDescriptor 값을 사용할 수 없습니다. 참고 : 여러 대상이있는 프로젝트는 선택한 대상과 해당 대상에 지정된 Swift 버전에 따라 오작동 할 수 있습니다. 이를 방지하려면 모든 대상이 Swift 4.0으로 업데이트되었는지 확인하십시오.

2) 예상되는 반환 형식에주의하여 기본 제공된 정적 형식을 사용하여 특정 UIFontAttribute에 액세스하십시오. 옵션으로는 NSString 반환 형식의 UIFontAttribute를 들어

:

let fontAttribute = fontDescriptor.object(forKey: .face) as? String

또는 더 내장 정적 유형이 존재하지 귀하의 예제에서

:

let fontAttribute = fontDescriptor.object(forKey: UIFontDescriptor.AttributeName(rawValue: "NSCTFontUIUsageAttribute")) as? String