OPAttributeString
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Requirements
- iOS 8.0 or later
- Xcode 7.3 or later
Getting Started
OPAttributeString is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'OPAttributeString'
How To Use
- 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]);
}];
or you can write like this.
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]);
}];
or this
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;
now you can see it.
But most of the time , you want set a part attribute of string. there is some API can use.
from(index)
If the index parameter exceeds the string range, the correction processing rules are as follows:
(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)
the correction processing rules are as follows:
(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)
the correction processing rules are as follows
location rules: same with from(index).
length rules: same with 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)
use it like 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]);
}];
the correction processing rules are as follows
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)
Use this function to find the range of characters or strings.
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(...)
this is a multi-parameter function, roughly as follows:
first case: insert a string to source string
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);
}];
second case: insert a NSAttributedString to source string
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);
}];
let us change the diffrent index paramters.
make.insert(string,0);
make.insert(string,-1);
third case: insert a UIImage to source string
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);
}];
regex pattern
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);
}];
remove some char or string from source string, you can do it like by this way.
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));
}];
replace some char or string from source string, you can do it like by this way. range rule: rangeCheck c function
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));
}];
Author
urm9ril, [email protected]
License
All source code is licensed under the MIT License.