OPAttributeString
示例
要运行示例项目,首先克隆仓库,然后从 Example 目录中以 pod install
开始。
要求
- iOS 8.0 或更高版本
- Xcode 7.3 或更高版本
起步
OPAttributeString 可通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中
pod 'OPAttributeString'
如何使用
- Objective-C
NSString *despicableMe = @"I know someone whoabccan fix that for you.";
self.someLabel.attributedText = [despicableMe make_Attribute:^(OPAttribute *make) {
make.font([UIFont fontWithName:@"Courier New" size:12]);
make.textColor([UIColor redColor]);
make.backgroundColor([UIColor greenColor]);
}];
或者您可以这样写。
NSString *despicableMe = @"I know someone whoabccan fix that for you.";
self.someLabel.attributedText = [despicableMe make_Attribute:^(OPAttribute *make) {
make.font([UIFont fontWithName:@"Courier New" size:12])
.textColor([UIColor redColor])
.backgroundColor([UIColor greenColor]);
}];
或者这样
NSString *despicableMe = @"I know someone whoabccan fix that for you.";
self.someLabel.attributedText = despicableMe.
font([UIFont fontWithName:@"Courier New" size:12])
.textColor([UIColor redColor])
.backgroundColor([UIColor greenColor])
.string;
现在您可以看到了。
但在大多数情况下,您想设置字符串的某个部分属性,有一些API可以使用。
from(index)
如果索引参数超出字符串范围,纠正处理规则如下
(index < 0 || index > string.length) ? 0 : index;
NSString *despicableMe = @"I know someone whoabccan fix that for you.";
self.someLabel.attributedText = [despicableMe make_Attribute:^(OPAttribute *make) {
make.from(0).font([UIFont systemFontOfSize:10]);
}];
to(index)
纠正处理规则如下
(index < 0 || index > string.length) ? string.length : index;
NSString *despicableMe = @"I know someone whoabccan fix that for you.";
self.someLabel.attributedText = [despicableMe make_Attribute:^(OPAttribute *make) {
make.font([UIFont systemFontOfSize:10]);
make.to(5).font([UIFont systemFontOfSize:17]);
}];
fromTo(location,length)
纠正处理规则如下
位置规则:与from(index)相同。
长度规则:与to(index)相同。
NSString *despicableMe = @"I know someone whoabccan fix that for you.";
self.someLabel.attributedText = [despicableMe make_Attribute:^(OPAttribute *make) {
make.font([UIFont systemFontOfSize:10]);
make.to(5).font([UIFont systemFontOfSize:17]);
make.fromTo(8,12).backgroundColor([UIColor orangeColor]);
}];
range(NSRange)
使用方式类似于fromTo(location,length)
NSString *despicableMe = @"I know someone whoabccan fix that for you.";
self.someLabel.attributedText = [despicableMe make_Attribute:^(OPAttribute *make) {
make.font([UIFont systemFontOfSize:10]);
make.to(5).font([UIFont systemFontOfSize:17]);
make.range(NSMakeRange(0,10)).backgroundColor([UIColor orangeColor]);
}];
纠正处理规则如下
NS_INLINE BOOL rangeCheck(NSRange range, NSRange rangeContainer) {
if(range.location < 0 || rangeContainer.location < 0 || rangeContainer.location > range.length) return false;
return range.location <= rangeContainer.location && range.length >= rangeContainer.length;
}
rangeOf(somestring)
使用此函数查找字符或字符串的范围。
NSString *despicableMe = @"I know someone whoabccan fix that for you.";
self.someLabel.attributedText = [despicableMe make_Attribute:^(OPAttribute *make) {
make.rangeOf(@"fix").strokeColor([UIColor blueColor]).strokeWidth(3);
}];
insert(...)
这是一个多参数函数,大体如下
第一种情况:在源字符串中插入一个字符串
NSString *despicableMe = @"I know someone whoabccan fix that for you.";
self.someLabel.attributedText = [despicableMe make_Attribute:^(OPAttribute *make) {
// default insert a string to the end of source string
make.insert(@"\n");
// or you can specified a string and index to insert
// the index parameter has rules same way to(index) used
// so -1 means 'ooops' will be inserted to the end
make.insert(@"ooops",-1);
}];
第二种情况:在源字符串中插入一个 NSAttributedString
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"e"];
[string addAttribute:NSBackgroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 1)];
NSString *despicableMe = @"I know someone whoabccan fix that for you.";
self.someLabel.attributedText = [despicableMe make_Attribute:^(OPAttribute *make) {
make.insert(string);
make.insert(string,-1);
}];
让我们更改不同的索引参数。
make.insert(string,0);
make.insert(string,-1);
第三种情况:在源字符串中插入一个 UIImage
NSString *despicableMe = @"I know someone whoabccan fix that for you.";
self.demoLabel.attributedText = [despicableMe make_Attribute:^(OPAttribute *make) {
UIImage *img = [UIImage imageNamed:@"old_sj"];
make.insert(img, 0, CGRectMake(0, 0, 18, 18), AttachmentAlignmentNormal);
}];
正则表达式模式
NSString *despicableMe = @"I know someone whoabccan fix that for you.";
self.demoLabel.attributedText = [despicableMe make_Attribute:^(OPAttribute *make) {
make.pattern(@"o").strokeColor([UIColor redColor]).strokeWidth(-5);
}];
从源字符串中删除一些字符或字符串,你可以通过这种方式完成。
NSString *despicableMe = @"I know someone whoabccan fix that for you.";
self.demoLabel.attributedText = [despicableMe make_Attribute:^(OPAttribute *make) {
// remove all char 'o' from source string.
// defalut matching hole source string.
make.removeE(@[@"o"]);
// specified range
make.removeE(@[@"o"],, NSMakeRange(0, 10));
// mult-matching and specified range.
make.removeE(@[@"e",@"o"], NSMakeRange(0, 10));
}];
用这种方式从源字符串中替换一些字符或字符串。范围规则:rangeCheck c 函数
NSString *despicableMe = @"I know someone whoabccan fix that for you.";
self.demoLabel.attributedText = [despicableMe make_Attribute:^(OPAttribute *make) {
// matching the hole source string
make.replace(@"o",@"O");
// matching the hole source string in range
make.replace(@"o",@"O",NSMakeRange(0, 8));
// mult-matching the hole string with dictionary
// dictionary's key will matching.
// the dictionary key's matching value will be replaced by dictionary's value.
// defalut matching hole source string.
make.replaceE(@[@{@"e" : @"o"},@{@"y" : @"Y"}]);
// in the same way ... in range
make.replaceE(@[@{@"o" : @"0"},@{@"n" : @"u"}],NSMakeRange(0, 8));
}];
作者
urm9ril, [email protected]
许可协议
所有源代码均受MIT 许可协议许可。