NSAttributedString的用法

来源:互联网 发布:淘宝创建追评失败 编辑:程序博客网 时间:2024/06/05 11:54

先上API

@interface NSAttributedString :NSObject <NSCopying,NSMutableCopying, NSCoding>


//把NSAttributedString转为字符串

- (NSString *)string;

//取到设置的属性字典

- (NSDictionary *)attributesAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range;

//例子:

/-------------------------/

 [attrString addAttribute:NSForegroundColorAttributeName

                      value:[UIColorredColor]

                      range:NSMakeRange(0,2)];


 NSRange range =NSMakeRange(0,2);

  NSDictionary*dic = [attrStringattributesAtIndex:0effectiveRange:&range];

//取到设置的属性字典

dic:

{

    NSColor = "UIDeviceRGBColorSpace 1 0 0 1";

}



/-------------------------/

@end


@interface NSAttributedString (NSExtendedAttributedString)

//长度

- (NSUInteger)length;

//取到设置的属性的值

- (id)attribute:(NSString *)attrName atIndex:(NSUInteger)location effectiveRange:(NSRangePointer)range;

//根据range截取

- (NSAttributedString *)attributedSubstringFromRange:(NSRange)range;


- (NSDictionary *)attributesAtIndex:(NSUInteger)location longestEffectiveRange:(NSRangePointer)range inRange:(NSRange)rangeLimit;


- (id)attribute:(NSString *)attrName atIndex:(NSUInteger)location longestEffectiveRange:(NSRangePointer)range inRange:(NSRange)rangeLimit;

//判断两个string是否相同

- (BOOL)isEqualToAttributedString:(NSAttributedString *)other;


//初始化

- (id)initWithString:(NSString *)str;

- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;

- (id)initWithAttributedString:(NSAttributedString *)attrStr;


typedefNS_OPTIONS(NSUInteger, NSAttributedStringEnumerationOptions) {

  NSAttributedStringEnumerationReverse = (1UL <<1),

  NSAttributedStringEnumerationLongestEffectiveRangeNotRequired = (1UL <<20)

};


#if NS_BLOCKS_AVAILABLE

//遍历string

- (void)enumerateAttributesInRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (^)(NSDictionary *attrs,NSRange range, BOOL *stop))blockNS_AVAILABLE(10_6,4_0);

- (void)enumerateAttribute:(NSString *)attrName inRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (^)(id value,NSRange range, BOOL *stop))blockNS_AVAILABLE(10_6,4_0);

#endif


@end


NS_CLASS_AVAILABLE(10_0,3_2)

@interface NSMutableAttributedString :NSAttributedString

//用str替换range内的AttributedString

- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;

- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;


@end


@interface NSMutableAttributedString (NSExtendedMutableAttributedString)


- (NSMutableString *)mutableString;//初始化

//添加属性

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;

- (void)removeAttribute:(NSString *)name range:(NSRange)range;//删除属性

//替换

- (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString;

- (void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc;//插入

- (void)appendAttributedString:(NSAttributedString *)attrString;//拼接string

- (void)deleteCharactersInRange:(NSRange)range;//删除

- (void)setAttributedString:(NSAttributedString *)attrString;//设置


- (void)beginEditing;//开始编辑

- (void)endEditing;//结束编辑


@end



基础用法

UILabel* label = [[UILabelalloc]initWithFrame:CGRectMake(0,0, 320, 200)];

        label.font = [UIFontsystemFontOfSize:20];

    [self.viewaddSubview:label];

    

   NSString* string = @"现金收/刷卡收,样样精通";


    NSMutableAttributedString *attrString =

    [[NSMutableAttributedStringalloc] initWithString:string];


    [attrString addAttribute:NSForegroundColorAttributeName

                      value:[UIColorredColor]

                      range:NSMakeRange(0,2)];

    

    [attrString addAttribute:NSFontAttributeName

                      value:[UIFontsystemFontOfSize:14]

                      range:NSMakeRange(4,2)];

    

    

    label.attributedText = attrString;


用法还是 挺多先了解基础用法有时间再慢慢研究 后期慢慢发掘



0 1