기본적으로 기본 글꼴이있는 NSAttributedString
을 생성하려고하는데 이탤릭체와 다른 텍스트 색상이 있습니다. 지금까지는 충분히 쉽습니다.NSAttributedString with UIFontWeightTraits
이제 전체 문자열의 다른 하위 문자열을 으로 변경하고을 굵게 표시합니다. 기본적으로 다음과 같아야합니다.
존 스미스 on 25.08. 나는 내가 NSMutableAttributedString
의 addAttributes(_:_:)
함수에 전달할 것을 사전이 잘못된 얻는 것 같은 8시
에 (그냥 다른 색.)
것 같습니다.
[UIFontDescriptorTraitsAttribute:
[UIFontWeightTrait: NSNumber(double: Double(UIFontWeightBold))]
을하지만이 작동하지 않는 것 다음 documentation에서,이 사전은 다음과 같이해야한다고 이해했다. 결국 나는 문자열의 이탤릭체 버전입니다. 나는 분명히 뭔가 잘못되고있다. 어떤 생각?
업데이트 : 추가 간단한 예
// Preparation
let rawString = "from John Smith on 25.08. at 8:00"
let attributedString = NSMutableAttributedString(string: rawString)
let nameRange = (rawString as NSString).rangeOfString("John Smith")
let italicFont = UIFont.italicSystemFontOfSize(14)
// Make entire string italic: works!
attributedString.addAttributes([NSFontAttributeName : italicFont], range: NSMakeRange(0, 33))
// Make the name string additionally bold: doesn't work!
attributedString.addAttributes([UIFontDescriptorTraitsAttribute:
[UIFontWeightTrait: NSNumber(double: Double(UIFontWeightBold))]], range: nameRange)
// Show it on the label
attributedStringLabel.attributedText = attributedString
감사합니다!
여러 속성 문자열을 가지고 보여 하나에 결합해야합니다. –
위의 함수를 사용하여 사실을 추가 한 후에 추가 할 수 없다는 것을 의미합니까? – flohei