每个带有本地化的UIView中的Swift3不同字体 [英] Swift3 different font in the All of the UIView with Localization each

查看:44
本文介绍了每个带有本地化的UIView中的Swift3不同字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每种语言之前使用不同的字体.

I want to use different fonts up to Languages each.

例如,英语中的roboto,法语中的openSans.

for example, roboto in English, openSans in French.

本地化

arrayOfTitle = [NSLocalizedString("comment", comment: "0"), NSLocalizedString("profile", comment: "1"), NSLocalizedString("Like", comment: "2")]

使用字符串1的模式.

let username: String? = (rList[indexPath.row].userInfo?.name)!

let attrString = NSMutableAttributedString(string: username!,
                                                   attributes: [NSFontAttributeName: UIFont(name: "Roboto-Bold", size: 12.0)!])

attrString.append(NSMutableAttributedString(string: rList[indexPath.row].gender,
                                                    attributes: [NSFontAttributeName: UIFont(name: "Roboto", size: 12.0)!]))

cell.label.attributedText = attrString

使用字符串2的模式.

 customButton.setTitle("edit", for: .normal)

谢谢.

推荐答案

如我所见,您有3个任务要做.

As I can see, there you have 3 tasks to do.

  1. 使用当前语言代码映射字体名称
  2. 从名称中获取常规字体
  3. 从名称中获取粗体字体(可选,但可从您的代码中找到)

1.使用当前语言代码映射字体名称

为此,您需要获取该代码并编写 switch 语句.

1. Map font name with current language code

In order to do that you need to get that code and write a switch statement.

它看起来像这样(可能会因您的需要而有所不同)

It can look like this (may vary depending on your needs)

func languageCode() -> String? {
    return NSLocale.autoupdatingCurrent.languageCode
}

func localizedFontName() -> String {
    let defaultFont = "Arial"
    guard let code = languageCode() else {
        return defaultFont
    }
    switch(code) {
    case "en":
        return "Helvetica"
    case "fr":
        return "Menlo"
    default:
        return defaultFont
    }
}

有关更多信息,请参考 NSLocale文档.

Please refer to NSLocale documentation for more info.

可以通过使用 UIFontDescriptor

let DefaultFontSize: CGFloat = 12.0
func font(from name: String) -> UIFont {
    let descriptor = UIFontDescriptor(name: name, size: DefaultFontSize)
    return UIFont(descriptor: descriptor, size: DefaultFontSize)
}

3.从名称中获取粗体

最后一个任务也可以通过 UIFontDescriptor 完成.

let BoldFontSize: CGFloat = 12.0
func boldFont(from name: String) -> UIFont {
    let regularFont = font(from: name)
    guard let descriptor = regularFont.fontDescriptor
                                      .withSymbolicTraits(.traitBold) else {
        return regularFont
    }
    return UIFont(descriptor: descriptor, size: BoldFontSize)
}

最终,所有这些都可以像这样使用:

Ultimately it all can be used like this:

let nameFont = boldFont(from: localizedFontName())
let nameString = NSAttributedString(string: "username ",
                                    attributes: [NSFontAttributeName: nameFont])

let surnameFont = font(from: localizedFontName())
let surnameString = NSAttributedString(string: "gender",
                                       attributes: [NSFontAttributeName: surnameFont])

完整的游乐场可在此处找到 playground.zip

Complete Playground can be found here playground.zip

希望有帮助!

这篇关于每个带有本地化的UIView中的Swift3不同字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆