VPAttributedFormat 项目代表类别
这些类别提供了根据属性格式和满足此格式的参数构建属性字符串的方法。
使用这些类别最合适的案例是在界面构建器中配置了可变属性文本的文本控件。
您需要为属性文本设置正确的字符串格式并配置必要的属性。
然后您需要通过这些类别的的方法在代码中传递必要的参数。
所有支持与属性字符串一起工作的标准控件都受支持:UILabel,UITextView,UITextField,UIButton。
请参阅使用说明和示例部分以获取更多详细信息。
完整文档可在CocoaDocs上找到。
添加到您的 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!
使用 UILabel / UITextView / UITextField / UIButton 类方法。
将keepFormat参数设置为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);
}
使用 UILabel / UITextView / UITextField / UIButton 类方法。
将keepFormat参数设置为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);
}
使用NSAttributedString类别方法。
适用于属性格式来自应用程序其他部分的场合。
// 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)
}
需要使用iOS SDK 6.0及以上版本进行编译。
可以在Objective C和Swift代码中使用。
VPAttributedFormatExample 是一个示例项目。它提供了基本和高级格式示例。
VPAttributedFormat是在MIT许可证下发布的。有关详细信息,请参阅LICENSE文件。