TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Oct 2016 |
Maintained by Vladimir Popko.
VPAttributedFormat project represents categories:
These categories provide methods for building attributed string based on attributed format and arguments that should satisfy this format.
The most suitable case of using these categories is text controls with variable attributed text configured in interface builder.
You need set correct string format to attributed text and configure necessary attributes.
Then you need pass necessary arguments in code by using methods of these categories.
All standard controls that work with attributed strings are supported: UILabel, UITextView, UITextField, UIButton.
See Usage and Examples sections for more details.
Full documentation is available on CocoaDocs.
Add to your Podfile:
pod "VPAttributedFormat"
// Objective C
// By header
#import <VPAttributedFormat/VPAttributedFormat.h>
// By module
@import VPAttributedFormat;
// Swift
import VPAttributedFormat
// Objective C
@property (nonatomic, weak) IBOutlet UILabel *textLabel;
// Swift
@IBOutlet weak var textLabel: UILabel!
Use UILabel / UITextView / UITextField / UIButton category methods.
Set keepFormat parameter to YES.
// Objective C
NSString *hot = @"Hot";
NSString *cold = @"Cold";
[self.textLabel vp_setAttributedTextFormatArguments:YES, hot, cold];
// Swift
let hot = "Hot"
let cold = "Cold"
var arguments: [CVarArgType] = [hot, cold]
withVaList(arguments) { pointer in
textLabel.vp_setAttributedTextFormatArguments(pointer, keepFormat: true);
}
Use UILabel / UITextView / UITextField / UIButton category methods.
Set keepFormat parameter to NO.
// Objective C
NSString *hot = @"Hot";
NSString *cold = @"Cold";
[self.textLabel vp_setAttributedTextFormatArguments:NO, hot, cold];
// Swift
let hot = "Hot"
let cold = "Cold"
var arguments: [CVarArgType] = [hot, cold]
withVaList(arguments) { pointer in
textLabel.vp_setAttributedTextFormatArguments(pointer, keepFormat: false);
}
Use NSAttributedString category methods.
It's suitable for situations when attributed format comes from another part of application.
// 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)
}
It requires building with iOS SDK 6.0 and later.
It can be used in Objective C and Swift code.
VPAttributedFormatExample is an example project. It provides Basic and Pro format examples.
VPAttributedFormat is released under the MIT license. See LICENSE for details.