我设置了一个属性字符串,该字符串在使用iPhone SE而不是iPhone 7的模拟器中可以正常工作。没有错误,它什么也没显示。

我也收到有关属性Expression implicitly coerced from NSObject? to Any的警告

这是我的代码:

let firstName = "Mark"

let welcomeAttributes = [ NSForegroundColorAttributeName: Constants.APP_TEXT_COLOR,
                                              NSFontAttributeName: Constants.APP_CELL_FONT ]

let userNameAttributes = [ NSForegroundColorAttributeName: Constants.APP_THEME_COLOR,
                                               NSFontAttributeName: Constants.APP_CELL_FONT ]

let unformattedUserFirstName = firstName

let userFirstName = NSAttributedString(string: unformattedUserFirstName, attributes: userNameAttributes)

let unformattedWelcome = "Welcome "

let welcome = NSAttributedString(string: unformattedWelcome, attributes: welcomeAttributes)

let welcomeString = NSMutableAttributedString()

welcomeString.append(welcome)

welcomeString.append(userFirstName)

self.welcomeLabel.attributedText = welcomeString




let welcomeLabel: UILabel = {
    let label = UILabel()
    label.backgroundColor = .white
    label.font = Constants.APP_CELL_FONT
    label.textColor = Constants.APP_TEXT_COLOR
    let welcomeAttributes = [ NSForegroundColorAttributeName: Constants.APP_TEXT_COLOR,
                              NSFontAttributeName: Constants.APP_CELL_FONT ]
    let unformattedWelcome = "Welcome"
    let welcome = NSAttributedString(string: unformattedWelcome, attributes: welcomeAttributes)
    let welcomeString = NSMutableAttributedString()
    welcomeString.append(welcome)
    label.attributedText = welcomeString
    return label
}()




static let APP_CELL_FONT = UIFont(name: "Muli", size: 12)
static let APP_TEXT_COLOR:UIColor = UIColor(red: 50.0/255.0, green: 50.0/255.0, blue: 50.0/255.0, alpha: 1.0)
static let APP_THEME_COLOR:UIColor = UIColor(red: 231.0/255.0, green: 76.0/255.0, blue: 60.0/255.0, alpha: 1.0)


这些字体已安装:

Muli
== Muli-Light
== Muli-ExtraLight
== Muli


为了验证这一点,我在应用程序委托中运行了以下代码。

    for family: String in UIFont.familyNames
    {
        print("\(family)")
        for names: String in UIFont.fontNames(forFamilyName: family)
        {
            print("== \(names)")
        }
    }

最佳答案

出现此问题的主要原因是字体,如果这是自定义字体,请检查字体的实际名称。

10-08 05:26
查看更多