富文本NSAttributedString与NSMutableAttributedString

来源:互联网 发布:淘宝网童装保暖 编辑:程序博客网 时间:2024/05/19 16:36


NSAttributedString

NSAttributedString用来处理字符串,使在同一字符串内显示出不同属性的字符。(例如:用来处理字符串)总之就是可以设置字符串中指定位置或指定范围内字符的属性。


创建一个NSAttributedString对象


- (instancetype)initWithString:(NSString *)aString  

使用字符串初始化对象


- (instancetype)initWithAttributedString:(NSAttributedString *)attributedString

使用NSAttributedString对象初始化


- (instancetype)initWithString:(NSString *)aString
                    attributes:(NSDictionary *)attributes

使用字符串和属性初始化对象


检索字符信息


string 获取对象的字符串信息


length 获取对象的字符串的长度


检索属性信息


- (NSDictionary *)attributesAtIndex:(NSUInteger)index
                     effectiveRange:(NSRangePointer)aRange

获取指定位置上的属性信息,并返回与指定位置属性相同并且连续的字符串的范围信息。

例如:用来处理字符串  如果 index为2 ,那么aRange就是’处理’的位置信息。

如果你不需要 range 信息的话就传一个 nil (就像 &error )


- (NSDictionary *)attributesAtIndex:(NSUInteger)index
              longestEffectiveRange:(NSRangePointer)aRange
                            inRange:(NSRange)rangeLimit

同上,只是在返回range信息做了一个限制rangeLimit


- (id)attribute:(NSString *)attributeName
        atIndex:(NSUInteger)index
 effectiveRange:(NSRangePointer)aRange

获取指定位置上指定属性名称的值,attributeName即为指定的属性名称。并返回range信息


- (id)attribute:(NSString *)attributeName
        atIndex:(NSUInteger)index
longestEffectiveRange:(NSRangePointer)aRange
        inRange:(NSRange)rangeLimit

获取指定位置上指定属性名称的值,attributeName即为指定的属性名称。并返回限定范围内的range信息


对象间的比较


- (BOOL)isEqualToAttributedString:(NSAttributedString *)otherString

与另一个 NSAttributedString 对象做对比返回bool值。

这里的相等不仅仅是字符串相等,连带这所有关联的属性也要相等


获取对象Substring


- (NSAttributedString *)attributedSubstringFromRange:(NSRange)aRange

获取指定范围内的子字符串


枚举字符串上覆盖的属性信息


- (void)enumerateAttribute:(NSString *)attrName
                   inRange:(NSRange)enumerationRange
                   options:(NSAttributedStringEnumerationOptions)opts
                usingBlock:(void (^)(id value,

                                     NSRange range,

                                     BOOL *stop))block

枚举指定范围内指定属性名称的值信息和range信息



- (void)enumerateAttributesInRange:(NSRange)enumerationRange
                           options:(NSAttributedStringEnumerationOptions)opts
                        usingBlock:(void (^)(NSDictionary *attrs,

                                            NSRange range,

                                             BOOL *stop))block

枚举指定范围内的属性信息和range信息




NSMutableAttributedString


NSAttributedStringNSMutableAttributedString是一组处理字符串属性的类。NSAttributedString处理的是不可变的富文本字符串,而NSMutableAttributedString处理的是可变的。

NSMutableAttributedString你可以添加或删除字符和属性。


检索字符信息


mutableString  获取对象的字符内容


改变字符


- (void)replaceCharactersInRange:(NSRange)aRange
                      withString:(NSString *)aString

用aString替换特定范围内的字符。

特别的:新替换上去的字符会继承在aRange范围内的第一个字符的属性信息,当aRange的length为0时,如果在aRange位置之前的字符有特定属性,那么新字符串就继承。否则就继承aRange位置之后的。


- (void)deleteCharactersInRange:(NSRange)aRange

删除aRange范围内的字符


改变属性


- (void)setAttributes:(NSDictionary *)attributes
                range:(NSRange)aRange

设置 aRange 范围内的字符属性为 attributes



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

设置 aRange范围内,名为 name的属性的值 value


- (void)addAttributes:(NSDictionary *)attributes
                range:(NSRange)aRange

在指定范围内添加属性



- (void)removeAttribute:(NSString *)name
                  range:(NSRange)aRange

删除指定范围内指定名称的属性


改变字符和属性


- (void)appendAttributedString:(NSAttributedString *)attributedString

在富文本结尾添加指定的富文本(含字符和属性)


- (void)insertAttributedString:(NSAttributedString *)attributedString
                       atIndex:(NSUInteger)index

在指定位置插入富文本,原富文本会根据 attributedString的length而从index的开始位置移动变化



- (void)replaceCharactersInRange:(NSRange)aRange
            withAttributedString:(NSAttributedString *)attributedString

替换指定范围内的字符及属性


- (void)setAttributedString:(NSAttributedString *)attributedString

用指定富文本 attributedString替换现有的


Grouping changes


- (void)beginEditing//开始编辑

- (void)endEditing //结束编辑


今天就到这里 ————LC


PS:文章一上传格式就全变了,真是无语






0 0
原创粉丝点击