Why is the callback function pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? not showing the correct font.
Here is my code:
extension ViewController: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
if component == 0 {
print(keysDataSource[row])
return keysDataSource[row]
} else {
print(chordsDataSource[row])
return chordsDataSource[row]
}
}
Here is the results in the debug window showing the result of the print statement when that callback function is called:
vi{
NSFont = "<UICTFont: 0x113d55700> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 30.00pt";
}
Here's the code to create the attributed strings:
public let fontSizePickerView: CGFloat = 30
func getFont(_ fontType: FontType, size: CGFloat) -> UIFont {
var fontName: String
switch fontType {
case .timesNewRomanRegular:
fontName = "TimesNewRomanPSMT"
case .timesNewRomanBold:
fontName = "TimesNewRomanPS-BoldMT"
case .maestroWide:
fontName = "MaestroWide"
}
return UIFont(name: fontName, size: size) ?? UIFont.systemFont(ofSize: size)
}
func makeAttributedText(_ text: String, fontSize: CGFloat) -> NSAttributedString? {
print(text)
let nsStringText: NSString = text as NSString
var nsMutableAttributedText: NSMutableAttributedString = getMutableAttributedString(text, fontSize: fontSize)
let fontSizeDiminished: CGFloat = fontSize * fontSizeRatioDiminished
let fontSizeFlat: CGFloat = fontSize * fontSizeRatioFlat
makeDiminishedAttribute(text: nsStringText, fontSize: fontSizeDiminished, baselineOffsetRatio: fontDiminishedBaselineOffset, mutableAttributedText: &nsMutableAttributedText)
makeFlatAttribute(text: nsStringText, fontSize: fontSizeFlat, baselineOffsetRatio: fontFlatBaselineOffset, mutableAttributedText: &nsMutableAttributedText)
return nsMutableAttributedText
}
func makeDiminishedAttribute(text: NSString, fontSize: CGFloat, baselineOffsetRatio: Double, mutableAttributedText: inout NSMutableAttributedString) {
var nsRange: NSRange = text.range(of: "L")
guard nsRange.length != 0 else {
return
}
let baselineOffset: CGFloat = fontSize * CGFloat(baselineOffsetRatio)
let attributesDiminished = getAttributesDiminished(fontSize: fontSize, baselineOffset: baselineOffset)
repeat {
mutableAttributedText.addAttributes(attributesDiminished, range: nsRange)
if nsRange.location + 1 < text.length {
let nsRangeToCheck: NSRange = NSRange(location: nsRange.location + 1, length: text.length - (nsRange.location + 1))
nsRange = text.range(of: "L", range: nsRangeToCheck)
print(nsRangeToCheck)
print(nsRange)
} else {
break
}
} while nsRange.length != 0
}
func makeFlatAttribute(text: NSString, fontSize: CGFloat, baselineOffsetRatio: Double, mutableAttributedText: inout NSMutableAttributedString) {
var nsRange: NSRange = text.range(of: "b")
guard nsRange.length != 0 else {
return
}
let baselineOffset: CGFloat = fontSize * CGFloat(baselineOffsetRatio)
let attributesFlat = getAttributesFlat(fontSize: fontSize, baselineOffset: baselineOffset)
repeat {
mutableAttributedText.addAttributes(attributesFlat, range: nsRange)
if nsRange.location + 1 < text.length {
let nsRangeToCheck: NSRange = NSRange(location: nsRange.location + 1, length: text.length - (nsRange.location + 1))
nsRange = text.range(of: "b", range: nsRangeToCheck)
print(nsRangeToCheck)
print(nsRange)
} else {
break
} while nsRange.length != 0
}
let keysString: [String] = wheel.map() {
item in
item.key.name
}
let keysDataSource: [NSAttributedString] = {
var arrayAttributedString = [NSAttributedString]()
for string in keysString {
if let attributedString = makeAttributedText(string, fontSize: fontSizePickerView) {
arrayAttributedString.append(attributedString)
}
}
return arrayAttributedString
}()
let chordsString: [String] = ["I", "ii", "iii", "IV", "V", "vi", "viiL", "II (V of V)", "III (V of vi)"]
let chordsDataSource: [NSAttributedString] = {
var arrayAttributedString = [NSAttributedString]()
for string in chordsString {
if let attributedString = makeAttributedText(string, fontSize: fontSizePickerView) {
arrayAttributedString.append(attributedString)
}
}
return arrayAttributedString
}()
Effectively, I tested.
Attributes as style do work ; but size is not taken into account.
As you read elsewhere, you have to use viewForRow to get it work (not a big change, you should do this).
Good luck and don't forget to close the thread.
Note: is this your SO post ?
https://stackoverflow.com/questions/55650114/why-is-picker-view-not-displaying-attributed-string