как добавить font weight и font size вместе в NSAttributedstring в UITextView?


    let style = NSMutableParagraphStyle()
                style.lineSpacing = 4
                let weight = UIFont.Weight(500)
                let fontSize = UIFont(name: "CustomFont", size: 13)

let attributes: [NSAttributedString.Key: Any] = [
                    NSAttributedString.Key.paragraphStyle: style,
                    NSAttributedString.Key.font: fontSize ?? 0
                    NSAttributedString.Key.font: weight
                  ]
                self.myTextView.attributedText = NSAttributedString(string: myTextView.text, attributes: attributes)

Мне выдает ошибку Dictionary literal contains duplicate keys


Ответы (1 шт):

Автор решения: schmidt9

Насколько я знаю, вес может быть назначен только для системных шрифтов, поскольку там гарантировано наличие соответствующей насыщенности (при этом я бы использовал имеющиеся константы, а не произвольные значения, например UIFont.Weight.semibold). Для других шрифтов насыщенность обычно указывается в названии и поменять ее нельзя

let weight = UIFont.Weight(500)
let systemFont = UIFont.systemFont(ofSize: 13, weight: weight)

let attributes: [NSAttributedString.Key: Any] = [
                    NSAttributedString.Key.paragraphStyle: style,
                    NSAttributedString.Key.font: systemFont
                  ]
→ Ссылка