NSAttributedString是否有[NSString stringWithFormat:]的类似物

Vla*_*pko 11 objective-c nsattributedstring ios swift

通常我在界面构建器中构建app界面.有时设计需要使用属性字符串(字体,颜色等).如果string是静态的,则很容易配置.
但是如果string是动态的(带参数的格式),则无法在界面构建器中配置属性.它需要编写大量代码.
我找的一些类似物[NSString stringWithFormat:]NSAttributedString.所以我将能够在界面构建器中设置字符串格式和必要的属性,然后在代码中提供必要的参数.

例如:
让我们考虑我需要这样格式的显示字符串:" %d + %d = %d "(所有数字都是粗体).
我想在界面构建器中配置此格式.在代码中我想提供参数:1,1,2.应该显示" 1 + 1 = 2 ".

Leo*_*Leo 5

与Swift 4.2兼容

public extension NSAttributedString {
    convenience init(format: NSAttributedString, args: NSAttributedString...) {
        let mutableNSAttributedString = NSMutableAttributedString(attributedString: format)

        args.forEach { (attributedString) in
            let range = NSString(string: mutableNSAttributedString.string).range(of: "%@")
            mutableNSAttributedString.replaceCharacters(in: range, with: attributedString)
        }
        self.init(attributedString: mutableNSAttributedString)
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

let content = NSAttributedString(string: "The quick brown %@ jumps over the lazy %@")
let fox = NSAttributedString(string: "fox", attributes: [.font: Fonts.CalibreReact.boldItalic.font(size: 40)])
let dog = NSAttributedString(string: "dog", attributes: [.font: Fonts.CalibreReact.lightItalic.font(size: 11)])
attributedLabel.attributedText = NSAttributedString(format: content, args: fox, dog)
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明

  • 还可以通过添加 `if range.location != NSNotFound {` 来变得更安全 (3认同)

Vla*_*pko 4

我一直在寻找这个问题的现有良好解决方案,但没有成功。
所以我能够自己实现它。
这就是为什么我自我回答这个问题以与社区分享知识。

解决方案

NSAttributedString+VPAttributedFormat类别提供了基于属性格式和应满足此格式的参数构建属性字符串的方法。
使用此类别的最合适的情况是在界面生成器中配置了可变属性文本的文本控件。
您需要为属性文本设置正确的字符串格式并配置必要的属性。
然后,您需要使用此类别的方法在代码中传递必要的参数。

  • 格式语法与方法中相同[NSString stringWithFormat:]
  • 可用于 Objective C 和 Swift 代码;
  • 需要 iOS 6.0 及更高版本;
  • 与 CocoaPods 集成;
  • 覆盖单元测试。

用法

1.导入框架头或模块

// Objective C
// By header
#import <VPAttributedFormat/VPAttributedFormat.h>

// By module
@import VPAttributedFormat;
Run Code Online (Sandbox Code Playgroud)
// Swift
import VPAttributedFormat
Run Code Online (Sandbox Code Playgroud)

2. 在界面生成器中为文本控件设置正确的格式和属性
用法

3. 创建IBOutlet并将其与文本控件链接

// Objective C
@property (nonatomic, weak) IBOutlet UILabel *textLabel;
Run Code Online (Sandbox Code Playgroud)
// Swift
@IBOutlet weak var textLabel: UILabel!
Run Code Online (Sandbox Code Playgroud)

4. 使用必要的参数填充格式

// Objective C
NSString *hot = @"Hot";
NSString *cold = @"Cold";
  
self.textLabel.attributedText = [NSAttributedString vp_attributedStringWithAttributedFormat:self.textLabel.attributedText,
                                 hot,
                                 cold];
Run Code Online (Sandbox Code Playgroud)
// Swift
let hot = "Hot"
let cold = "Cold"
var arguments: [CVarArgType] = [hot, cold]
textLabel.attributedText = withVaList(arguments) { pointer in
    NSAttributedString.vp_attributedStringWithAttributedFormat(textLabel.attributedText, arguments: pointer)
}
Run Code Online (Sandbox Code Playgroud)

5. 查看结果
结果

例子

VPAttributedFormatExample是一个示例项目。它提供了 Basic 和 Pro 格式示例。
例子