Latest 0.0.7
Homepage https://github.com/urm9ril/OPAttributeString
License MIT
Platforms ios 8.0, requires ARC
Frameworks UIKit, Foundation
Authors

CI Status
Version
License
Platform

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 minimalist writing

    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.

Alt 20180426.png

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]);
}];

Alt 20180426_1.png

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]);
}];

Alt 20180426_2.png

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]);
}];

Alt 20180426_3.png

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]);
}];

Alt 20180426_4.png

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);
}];

Alt 20180426_5.png

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);
}];

Alt 20180426_6.png
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);
}];

Alt 20180426_7.png

let us change the diffrent index paramters.

make.insert(string,0);
make.insert(string,-1);

then will display like this
Alt 20180426_8.png

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);
}];

Alt 20180426_9.png

append(…)

you can use it like insert(…), the difference between inserting and appending, appending always adding characters or strings to the end of the source string.

NSMutableAttributedString *attributeString = @"e".backgroundColor([UIColor redColor]).string;

UIImage *img = [UIImage imageNamed:@"old_sj"];

self.demoLabel.attributedText = @"".append(@"_append")
.append(attributeString)
.append(@"rn")
.append(img,CGRectMake(0, 0, 18, 18),AttachmentAlignmentNormal).string;

Alt 20180426_17.png

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);
}];

Alt 20180426_10.png

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));
}];

Alt 20180426_11.png
Alt 20180426_12.png

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));
}];

Alt 20180426_13.png
Alt 20180426_14.png
Alt 20180426_15.png
Alt 20180426_16.png

Author

urm9ril, [email protected]

License

All source code is licensed under the MIT License.

Latest podspec

{
    "name": "OPAttributeString",
    "version": "0.0.7",
    "license": "MIT",
    "summary": "A Sample AttributeString Lib",
    "homepage": "https://github.com/urm9ril/OPAttributeString",
    "source": {
        "git": "https://github.com/urm9ril/OPAttributeString.git",
        "tag": "0.0.7"
    },
    "source_files": "OPAttributeString/Classes/*.{h,m}",
    "requires_arc": true,
    "platforms": {
        "ios": "8.0"
    },
    "frameworks": [
        "UIKit",
        "Foundation"
    ],
    "authors": {
        "BY": "[email protected]"
    },
    "social_media_url": "https://urm9ril.github.io/"
}

Pin It on Pinterest

Share This