NSAttributedString

本文详细介绍了NSAttributedString及其可变形式NSMutableAttributedString的基本概念、创建方法、属性设置及常用属性等,并提供了丰富的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、属性字符串的是什么

NSAttributedString直接继承NSObject,主要包括两个部分,即“字符串”和 “属性字典”。

NS_CLASS_AVAILABLE(10_0, 3_2)
@interface NSAttributedString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding>



二、官方头文件

//创建一个NSAttributedString对象:
- (instancetype)initWithString:(NSString *)aString 
- (instancetype)initWithAttributedString:(NSAttributedString *)attributedString 
- (instancetype)initWithString:(NSString *)aStringattributes:(NSDictionary<NSString *,id> *)attributes

//属性信息:
@property(readonly, copy) NSString *string
@property(readonly) NSUInteger length

//获取属性信息
- (NSDictionary<NSString *,id> *)attributesAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
- (NSDictionary<NSString *,id> *)attributesAtIndex:(NSUInteger)index longestEffectiveRange:(NSRangePointer)aRange inRange:(NSRange)rangeLimit
- (id)attribute:(NSString *)attributeName atIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
- (id)attribute:(NSString *)attributeName atIndex:(NSUInteger)index longestEffectiveRange:(NSRangePointer)aRange inRange:(NSRange)rangeLimit

//属性字符串比较
- (BOOL)isEqualToAttributedString:(NSAttributedString *)otherString

//提取子串
- (NSAttributedString *)attributedSubstringFromRange:(NSRange)aRange



三、可变的NSMutableAttributedString

这里的“可变”,不仅指“属性字符串”本身内容和长度可以改变,而且“属性字典”也是可变的。

NS_CLASS_AVAILABLE(10_0, 3_2)
@interface NSMutableAttributedString : NSAttributedString

              //针对"字符串"本身的可变性//
//1.替换字符
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str;
- (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString;
//2.插入
- (void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc;
//3.拼接
- (void)appendAttributedString:(NSAttributedString *)attrString;
//4.删除
- (void)deleteCharactersInRange:(NSRange)range;
//5.重置
- (void)setAttributedString:(NSAttributedString *)attrString;



          //针对某属性字符串的“属性字典”//
//1.重置属性
- (void)setAttributes:(nullable NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
//2.新增属性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
- (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range;
//3.移除属性
- (void)removeAttribute:(NSString *)name range:(NSRange)range;


四、字符属性列表大全
参考:https://www.2cto.com/kf/201608/535006.html (有讲解哦)
常用如下:

1.字体大小NSFontAttributeName
说明:该属性用于改变一段文本的字体。如果不指定该属性,则默认为12-point Helvetica(Neue)

2.字体颜色NSForegroundColorAttributeName
说明:该属性用于指定一段文本的字体颜色。如果不指定该属性,则默认为黑色

3.背景颜色NSBackgroundColorAttributeName
说明:设置文字背景颜色

4.链接属性NSLinkAttributeName
说明:这货有点奇葩,所以很多人用第三方例如YYLabel来做,这东西不能在UILabel和UITextField使用,只能用UITextView来进行,实现他的代理,在代理方法里面进行URL跳转。
- (BOOL)textView:(UITextView )textView shouldInteractWithURL:(NSURL )URL inRange:(NSRange)characterRange;
该方法返回YES就能打开URL,NO不做任何事情。
注:
一定要实现UITextView的代理才能进行URL跳转
textView的editable属性修改为NO,在编辑时不可点击

5.段落属性NSParagraphStyleAttributeName
说明:设置文本段落排版格式(Label的高度自适应跟这个属性有关哦),取值为 NSParagraphStyle 对象。

NSParagraphStyle与NSMutableParagraphStyle包括以下属性:
alignment ->对齐方式
firstLineHeadIndent ->首行缩进
headIndent ->缩进
tailIndent ->尾部缩进
lineBreakMode ->断行方式
maximumLineHeight ->最大行高
minimumLineHeight ->最低行高
lineSpacing ->行距
paragraphSpacing ->段距
paragraphSpacingBefore ->段首空间
baseWritingDirection ->句子方向

//示例:
 NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.firstLineHeadIndent = 10;
    style.lineSpacing = 10;
    [mAttStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(10, 10)];

6.字间距NSKernAttributeName
说明:字符间距正值间距加宽,负值间距变窄(参考链接有示例)

四、注意事项
对同一个范围内的字符串,
1.可以设置多种不同name的属性;
2.同一种name的属性,比如字体颜色,多次设置后,只保留最后的设置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值