NSATTRIBUTEDSTRING在iPhone SE上在模拟器中起作用,但不能在iPhone 7中使用



我设置了一个属性字符串,该字符串使用iPhone SE在模拟器中正常工作,但没有iPhone7。没有错误,只是没有显示任何内容。

我也会在属性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

要验证此内容,我在App委托中运行以下代码。

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

此问题主要是由于字体,如果是自定义字体,请检查字体的实际名称。

相关内容

最新更新