Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

ios - Crazy problem with NumberFormatter and bold font

I have an issue with thousand separator when I used bold font. I have two fonts, 1 regular and 1 bold.

When I used regular one, thousand grouping separator (space) is displayed, when I used bold, thousand separator disappear.

extension Float {
    /// Prints a Float as a currency token displayed like 1 000,00 €. The way it's displayed depends on the locale and currency symbol in its arguments
    /// - Parameters:
    ///   - localeIdentitfier: Default to "fr_FR". It's the locale the float will be formated to
    ///   - currencySymbol: If nil defaults to the locale's default (€ if locale is "fr_FR", $ if locale if "en_US"). Use it to return a foreign
    ///                     amount expressed in the locale format (eg "1 000.00 $" instead of "1 000.00 €")
    /// - Returns: An amount expressed in a locale format and currency
    public func toLocalCurrencyFormat(localeIdentitfier: String? = DefaultConstants.localeIdentitfier, currencyCode: String? = nil) -> String? {
        let formatter: NumberFormatter = .defaultCurrency(currencyCode: currencyCode)
        if let locale = localeIdentitfier {
            formatter.locale = Locale(identifier: locale)
        }
        return toFormat(definedBy: formatter)
    }
}

func createModel(balance: Double?) -> SubCardModel {
    var label: String? = ""
    if let balance = balance,
        let balanceToLocalCurrency = Float(balance).toLocalCurrencyFormat() {
        label = "(balanceToLocalCurrency)"
    }
    return SubCardModel(label: label)
}

public class SubCardView: NibView {

    @IBOutlet weak var subTitleLabel: UILabel!

    public override func awakeFromNib() {
        super.awakeFromNib()
        subTitleLabel.font = Style.Fonts.title1
    }
    func initWith(model: SubCardModel) {
        subTitleLabel.text = model.subtitle
    }
}

public struct Style {
    public struct Fonts {
        public static let title1 = font(ofSize: 14.0, weight: .bold)
        public static let title2 = font(ofSize: 14.0, weight: .regular)
        internal static func font(ofSize: CGFloat, weight: UIFont.Weight) -> UIFont {
            let font = weight == .regular ? FontFamily.MavenPro.regular:
                                            FontFamily.MavenPro.bold
            return font.font(size: ofSize)
        }
    }
}

Not space separator for thousand with bold font: enter image description here

Space separator for thousand with regular font: enter image description here

EDIT 1

Same bug with smaller amount enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...