向 NSMutableAttributedString 添加多个属性

Fab*_*nni 6 label uifont nsmutablestring swift

我正在尝试向 NSMutableAttributedString 添加几个属性;我试过这个:

let stringNumero: NSString = "\(squadra.cori.count)" //= two-digit number
var stringNumeroMutable = NSMutableAttributedString()
stringNumeroMutable = NSMutableAttributedString(string: stringNumero as! String, attributes: [NSFontAttributeName: UIFont(name: "Noteworthy-Light", size: 9)!,
                                                                                                  NSForegroundColorAttributeName: UIColor(red: 110/255.0, green: 183/255.0, blue: 93/255.0, alpha: 1.0)])
cell.numeroCori.attributedText = stringNumeroMutable
Run Code Online (Sandbox Code Playgroud)

现在我想添加其他属性,我将使用以下代码:

stringNumeroMutable.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(8), range: NSRange(location: 0, length: 1))
Run Code Online (Sandbox Code Playgroud)

但我遇到了一个问题:

在此处输入图片说明

如您所见,粗体效果仅归因于第一个数字,而前一个属性(例如字体)消失。我认为第二个 NSFontAttributedName 订阅了前一个,那么有没有办法同时设置字体和粗体?非常感谢!!!

编辑:按照建议,我试图回忆这样的方法:

let stringNumero: NSString = "\(squadra.cori.count)" //= two-digit number
var stringNumeroMutable = NSMutableAttributedString()
stringNumeroMutable = NSMutableAttributedString(string: stringNumero as! String, attributes: [NSFontAttributeName: UIFont(name: "Noteworthy-Light", size: 9)!,
                                                                                                  NSForegroundColorAttributeName: UIColor(red: 110/255.0, green: 183/255.0, blue: 93/255.0, alpha: 1.0)])
cell.numeroCori.attributedText = stringNumeroMutable

stringNumeroMutable.addAttribute(NSFontAttributeName, value: UIFont.boldSystemFontOfSize(9), range: NSRange(location: 0, length: stringNumeroMutable.length))

cell.numeroCori.atributedText = stringNumeroMutable
Run Code Online (Sandbox Code Playgroud)

但是第二个调用覆盖了第一个调用(因此数字松散了属性字体)。如何解决这个问题?

在此处输入图片说明

Tik*_*der 6

我认为你的问题,你使用的方法 boldSystemFontOfSize

boldSystemFontOfSize:

返回用于以指定大小以粗体显示的标准界面项的字体对象。

请参阅我的解决方案:

@IBOutlet weak var label: UILabel!
var mutableAttributedString: NSMutableAttributedString!
override func viewDidLoad() {
    super.viewDidLoad()
    let someString = "Hello World"
    let attr: [String: AnyObject] = [NSFontAttributeName: UIFont(name: "Helvetica", size: 10)!,
        NSForegroundColorAttributeName: UIColor(red: 0.18, green: 0.18, blue: 0.18, alpha: 1.0)]
    mutableAttributedString = NSMutableAttributedString(string: someString, attributes: attr)
    label.attributedText = mutableAttributedString
}

@IBAction func buttonPressed(sender: UIButton) {
    let font = mutableAttributedString.attribute(NSFontAttributeName, atIndex: 1, effectiveRange: nil)!
    mutableAttributedString.addAttribute(NSFontAttributeName, value: font.fontWithSize(20) , range: NSMakeRange(0, mutableAttributedString.length))
    label.attributedText = mutableAttributedString
}
Run Code Online (Sandbox Code Playgroud)

在buttonPressed函数中,您定义了当前字体,mutableAttributedString然后您可以使用方法更改字体的大小fontWithSize。