富文本

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,

NSRangerange,

BOOL *stop))block

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

- (void)enumerateAttributesInRange:(NSRange)

enumerationRange

options:(NSAttributedStringEnumerationOptions)

opts

usingBlock:(void (^)(NSDictionary*attrs,

NSRangerange,

BOOL *stop))block

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

NSMutableAttributedString

NSAttributedString和NSMutableAttributedString是一组处理字符串属性的类。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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值