I was looking for a good existing solution for this issue, but was not successful.
Therefore, I was able to implement this on my own.
That's why I myself answer the question about sharing knowledge with the community.
Decision
The NSAttributedString + VPAttributedFormat category provides methods for constructing an attribute string based on an attribute format and arguments that must satisfy this format.
The most suitable use case for this category is text controls with a variable text attribute configured in the interface builder.
You need to set the correct string format for the text attribute and configure the necessary attributes.
Then you need to pass the necessary arguments in the code using the methods of this category.
- The syntax format is the same as in the
[NSString stringWithFormat:] method; - Can be used in Objective-C and Swift code;
- Requires iOS 6.0 and later;
- Integrated with CocoaPods;
- Complete with unit tests.
Using
1. Import a header or structure module
// Objective C // By header #import <VPAttributedFormat/VPAttributedFormat.h> // By module @import VPAttributedFormat;
// Swift import VPAttributedFormat
2. Set the correct format and attributes for text management in the interface builder

3. Create an IBOutlet and bind it using text management
// Objective C @property (nonatomic, weak) IBOutlet UILabel *textLabel;
// Swift @IBOutlet weak var textLabel: UILabel!
4. Set the format with the necessary arguments
// Objective C NSString *hot = @"Hot"; NSString *cold = @"Cold"; self.textLabel.attributedText = [NSAttributedString vp_attributedStringWithAttributedFormat:self.textLabel.attributedText, hot, cold];
// 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) }
5. See Result

Examples
VPAttributedFormatExample is a sample project. It provides examples of Basic and Pro formats.

source share