NSString+BBAAttributed.h 2.19 KB
//
//  NSString+BBAAttributed.h
//  
//
//  Created by linling on 15/6/27.
//  Copyright (c) 2015年 baidu. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "BBAAttributedStringMaker.h"

/**  快捷创建属性字符串,使用方式如下:
 
 NSString *info = @"hello world";
 
 1,创建字体大小为14、颜色为红色的属性字符串:
 NSAttributedString *attrInfo = [info bba_attrRender:^(BBAAttributedStringMaker *maker) {
    maker.font([UIFont systemFontOfSize:14]).color([UIColor redColor]);
 }];

 2,创建整体字体大小为14、颜色为红色,只有hello部分的字体大小为20、颜色为白色的属性字符串:
 NSAttributedString *attrInfo = [info bba_attrRender:^(BBAAttributedStringMaker *maker) {
    maker.font([UIFont systemFontOfSize:14]).color([UIColor redColor]);
    maker.subString(@"hello", ^(BBAAttributedStringMaker *maker){
        maker.font([UIFont systemFontOfSize:20]).color([UIColor whiteColor]);
    });
 }];
 
 3,创建整体字体大小为14、颜色为红色,只有最后2个字符的字体大小为20、颜色为白色的属性字符串:
 NSAttributedString *attrInfo = [info bba_attrRender:^(BBAAttributedStringMaker *maker) {
    maker.font([UIFont systemFontOfSize:14]).color([UIColor redColor]);
    maker.fromIndex(info.length - 2, ^(BBAAttributedStringMaker *maker){
        maker.font([UIFont systemFontOfSize:20]).color([UIColor whiteColor]);
    });
 }];
 
 4,创建整体字体大小为14、颜色为红色,hello部分的字符间距为10,world部分的字符间距为20的属性字符串:
 NSAttributedString *attrInfo = [info bba_attrRender:^(BBAAttributedStringMaker *maker) {
    maker.font([UIFont systemFontOfSize:14]).color([UIColor redColor]);
 
    maker.subString(@"hello", ^(BBAAttributedStringMaker *maker){
        maker.wordSpace(10);
    });
 
    maker.subString(@"world", ^(BBAAttributedStringMaker *maker){
        maker.wordSpace(20);
    });
 }];
 
 */
@interface NSString (BBAAttributed)

- (NSAttributedString *)bba_attrRender:(void(^)(BBAAttributedStringMaker *maker))block;

/// 返回指定字体、大小下当前文本所需的行数
- (NSUInteger)bba_numberOfLineForFont:(UIFont *)font size:(CGSize)size;

@end