VPAttributedFormat 1.2.5

VPAttributedFormat 1.2.5

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最近发布2016年10月

Vladimir Popko维护。



  • 作者
  • Visput

VPAttributedFormat v1.2.4

VPAttributedFormat 项目代表类别

  • NSAttributedString+VPAttributedFormat
  • UILabel+VPAttributedFormat
  • UITextView+VPAttributedFormat
  • UITextField+VPAttributedFormat
  • UIButton+VPAttributedFormat

这些类别提供了根据属性格式和满足此格式的参数构建属性字符串的方法。
使用这些类别最合适的案例是在界面构建器中配置了可变属性文本的文本控件。
您需要为属性文本设置正确的字符串格式并配置必要的属性。
然后您需要通过这些类别的的方法在代码中传递必要的参数。
所有支持与属性字符串一起工作的标准控件都受支持:UILabel,UITextView,UITextField,UIButton。
请参阅使用说明示例部分以获取更多详细信息。
完整文档可在CocoaDocs上找到。

example

安装

Cocoa Pods

添加到您的 Podfile

pod "VPAttributedFormat"
拖放
  1. 将 VPAttributedFormat.xcodeproj 拖放到您的项目中;
  2. 将 VPAttributedFormat 添加到 Build Settings -> Target Dependencies;
  3. 将 VPAttributedFormat.framework 添加到 Build Settings -> Link Binary With Libraries;
  4. 将 "-all_load" 标志添加到 Build Settings -> Other Linker Flags。

使用

导入框架头文件或模块
// Objective C
// By header
#import <VPAttributedFormat/VPAttributedFormat.h>
// By module
@import VPAttributedFormat;
// Swift
import VPAttributedFormat
在界面构建器中为文本控件设置正确的格式和属性

usage

创建 IBOutlet 并将其与文本控件链接
// 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)
}
享受!

result

系统要求

需要使用iOS SDK 6.0及以上版本进行编译。
可以在Objective C和Swift代码中使用。

示例

VPAttributedFormatExample 是一个示例项目。它提供了基本和高级格式示例。

许可证

VPAttributedFormat是在MIT许可证下发布的。有关详细信息,请参阅LICENSE文件。