如何使用Swift创建属性字符串?

葛学民
2023-12-01

本文翻译自:How do I make an attributed string using Swift?

I am trying to make a simple Coffee Calculator. 我正在尝试制作一个简单的咖啡计算器。 I need to display the amount of coffee in grams. 我需要显示咖啡的克数。 The "g" symbol for grams needs to be attached to my UILabel that I am using to display the amount. 克的“ g”符号需要附加到我用来显示数量的UILabel上。 The numbers in the UILabel are changing dynamically with user input just fine, but I need to add a lower case "g" on the end of the string that is formatted differently from the updating numbers. UILabel中的数字随用户输入的变化而动态变化,但是我需要在字符串的末尾添加小写的“ g”,其格式与更新数字的格式不同。 The "g" needs to be attached to the numbers so that as the number size and position changes, the "g" "moves" with the numbers. 需要将“ g”附加到数字上,以便随着数字大小和位置的变化,“ g”随数字一起“移动”。 I'm sure this problem has been solved before so a link in the right direction would be helpful as I've googled my little heart out. 我敢肯定这个问题已经解决了,所以正确的链接会很有帮助,因为我已经用心搜寻了。

I've searched through the documentation for an attributed string and I even downloded an "Attributed String Creator" from the app store, but the resulting code is in Objective-C and I am using Swift. 我在文档中搜索了属性字符串,甚至从应用程序商店下载了“属性字符串创建器”,但是结果代码在Objective-C中,并且我正在使用Swift。 What would be awesome, and probably helpful to other developers learning this language, is a clear example of creating a custom font with custom attributes using an attributed string in Swift. 一个很棒的示例,这显然是在Swift中使用属性字符串创建具有自定义属性的自定义字体的明显示例,并且可能对其他开发人员有所帮助。 The documentation for this is very confusing as there is not a very clear path on how to do so. 有关此操作的文档非常混乱,因为如何执行此操作的路径并不明确。 My plan is to create the attributed string and add it to the end of my coffeeAmount string. 我的计划是创建属性字符串,并将其添加到coffeeAmount字符串的末尾。

var coffeeAmount: String = calculatedCoffee + attributedText

Where calculatedCoffee is an Int converted to a string and "attributedText" is the lowercase "g" with customized font that I am trying to create. 其中calculatedCoffee是将Int转换为字符串的整数,而“ attributedText”是我尝试创建的具有自定义字体的小写字母“ g”。 Maybe I'm going about this the wrong way. 也许我正在以错误的方式进行操作。 Any help is appreciated! 任何帮助表示赞赏!


#1楼

参考:https://stackoom.com/question/1FuT1/如何使用Swift创建属性字符串


#2楼

Swift uses the same NSMutableAttributedString that Obj-C does. Swift使用与Obj-C相同的NSMutableAttributedString You instantiate it by passing in the calculated value as a string: 您可以通过将计算值作为字符串传入来实例化它:

var attributedString = NSMutableAttributedString(string:"\(calculatedCoffee)")

Now create the attributed g string (heh). 现在创建属性g字符串(heh)。 Note: UIFont.systemFontOfSize(_) is now a failable initializer, so it has to be unwrapped before you can use it: 注意: UIFont.systemFontOfSize(_)现在是一个可失败的初始化程序,因此必须先将其拆开,然后才能使用它:

var attrs = [NSFontAttributeName : UIFont.systemFontOfSize(19.0)!]
var gString = NSMutableAttributedString(string:"g", attributes:attrs)

And then append it: 然后附加:

attributedString.appendAttributedString(gString)

You can then set the UILabel to display the NSAttributedString like this: 然后,您可以设置UILabel以显示NSAttributedString,如下所示:

myLabel.attributedText = attributedString

#3楼

Works well in beta 6 在Beta 6中运作良好

let attrString = NSAttributedString(
    string: "title-title-title",
    attributes: NSDictionary(
       object: NSFont(name: "Arial", size: 12.0), 
       forKey: NSFontAttributeName))

#4楼

Xcode 6 version : Xcode 6版本

let attriString = NSAttributedString(string:"attriString", attributes:
[NSForegroundColorAttributeName: UIColor.lightGrayColor(), 
            NSFontAttributeName: AttriFont])

Xcode 9.3 version : Xcode 9.3版本

let attriString = NSAttributedString(string:"attriString", attributes:
[NSAttributedStringKey.foregroundColor: UIColor.lightGray, 
            NSAttributedStringKey.font: AttriFont])

Xcode 10, iOS 12, Swift 4 : Xcode 10,iOS 12,Swift 4

let attriString = NSAttributedString(string:"attriString", attributes:
[NSAttributedString.Key.foregroundColor: UIColor.lightGray, 
            NSAttributedString.Key.font: AttriFont])

#5楼

Swift: xcode 6.1 斯威夫特:xcode 6.1

    let font:UIFont? = UIFont(name: "Arial", size: 12.0)

    let attrString = NSAttributedString(
        string: titleData,
        attributes: NSDictionary(
            object: font!,
            forKey: NSFontAttributeName))

#6楼

 let attrString = NSAttributedString (
            string: "title-title-title",
            attributes: [NSAttributedStringKey.foregroundColor: UIColor.black])
 类似资料: