富文本之NSAttributedString与NSMutableAttributedString
来源:互联网 发布:冒险岛交易市场数据 编辑:程序博客网 时间:2024/05/20 06:22
一,概述
NSAttributedString 叫做富文本,是一种带有属性的字符串,通过它可以轻松的在一个字符串中表现出多种字体、字号、字体大小等各不相同的风格,还可以对段落进行格式化.拥有文本显示功能(text 属性)的 UI 控件也都拥有 attributedText 属性.
二,NSAttributedString
API@interface NSAttributedString : NSObject <NSCopying, NSMutableCopying, NSSecureCoding> // 1.属性 /** * 把NSAttributedString转为字符串 */ @property (readonly, copy) NSString *string; // 2.方法 /** * 取到设置的属性字典 参数1: 起始位置 参数2: 范围 */ - (NSDictionary<NSString *, id> *)attributesAtIndex:(NSUInteger)location effectiveRange:(nullable NSRangePointer)range; @end
@interface NSAttributedString (NSExtendedAttributedString) @property (readonly) NSUInteger length; /** *用某个字符串,创建富文本对象 参数:用于创建的字符串 */ - (instancetype)initWithString:(NSString *)str; /** *用某个字符串和一些字符属性,创建富文本属性 参数1:初始字符串 参数2:字符属性字典 */ - (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary<NSString *, id> *)attrs; /** *用一个富文本字符串,创建一个富文本字符串 参数1:用于创建对象的富文本字符串 */ - (instancetype)initWithAttributedString:(NSAttributedString *)attrStr; /** * 从第几个字符开始找属性 */ - (nullable id)attribute:(NSString *)attrName atIndex:(NSUInteger)location effectiveRange:(nullable NSRangePointer)range; - (NSAttributedString *)attributedSubstringFromRange:(NSRange)range;/** * 返回指定范围的属性 */ - (NSDictionary<NSString *, id> *)attributesAtIndex:(NSUInteger)location longestEffectiveRange:(nullable NSRangePointer)range inRange:(NSRange)rangeLimit;- (nullable id)attribute:(NSString *)attrName atIndex:(NSUInteger)location longestEffectiveRange:(nullable NSRangePointer)range inRange:(NSRange)rangeLimit;/** * // 等同性判断 */- (BOOL)isEqualToAttributedString:(NSAttributedString *)other;typedef NS_OPTIONS(NSUInteger, NSAttributedStringEnumerationOptions) { NSAttributedStringEnumerationReverse = (1UL << 1), NSAttributedStringEnumerationLongestEffectiveRangeNotRequired = (1UL << 20)};/** * 遍历获得符合指定属性或属性字典的区域(range),并在 block 中进行设置 */- (void)enumerateAttributesInRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^) (NSDictionary<NSString *, id> *attrs, NSRange range, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);- (void)enumerateAttribute:(NSString *)attrName inRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (NS_NOESCAPE ^)(id _Nullable value, NSRange range, BOOL *stop))block NS_AVAILABLE(10_6, 4_0);@end
@interface NSAttributedString (NSAttributedStringAttachmentConveniences)/** * 插入表情符 配合NSTextAttachment,NSTextStorage,UITextView或UITextfied使用 */+ (NSAttributedString *)attributedStringWithAttachment:(NSTextAttachment *)attachment NS_AVAILABLE(10_0, 7_0);@end
三,NSMutableAttributedString 继承于 NSAttributedString
API
@interface NSMutableAttributedString (NSExtendedMutableAttributedString) //属性@property (readonly, retain) NSMutableString *mutableString;/** * 在一定范围中添加单个文字属性 参数1:字符属性名 参数2:属性值 参数3:范围 */- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;/** * 在一定范围中使用字典添加多个文字属性 参数1:属性字典 参数2:范围 */- (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range;/** * 在一定范围中删除文字具有的某个文字属性 参数1:字符属性名 参数2:范围 */- (void)removeAttribute:(NSString *)name range:(NSRange)range;/** *在一定范围中替换字符串 参数1:范围 参数2:要替换的字符串 */- (void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString;/** * 在对应的角标处插入富文本 参数1:要插入的字符串 参数2:要插入的角标位置 */- (void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc;/** *将某个富文本拼接到后面 参数:要拼接的字符串 */- (void)appendAttributedString:(NSAttributedString *)attrString;/** *删除一定范围中的字符 参数:范围 */- (void)deleteCharactersInRange:(NSRange)range;/** *将字符串全部置换为另一个富文本字符串 参数:置换后的富文本字符串 */- (void)setAttributedString:(NSAttributedString *)attrString;/** *开始编辑 */- (void)beginEditing;/** *结束编辑 */- (void)endEditing;@end
四,字符属性
1.NSString *const NSFontAttributeName(字体):该属性所对应的值是一个 UIFont 对象。该属性用于改变一段文本的字体。如果不指定该属性,则默认为12-point Helvetica(Neue)。
2.NSString *const NSParagraphStyleAttributeName(段落):
该属性所对应的值是一个 NSParagraphStyle 对 象。该属性在一段文本上应用多个属性。如果不指定该属性,则默认为 NSParagraphStyle 的 defaultParagraphStyle 方法返回的默认段落属性。想要了解NSParagraphStyle可以自行百度学习,在这里不详细描述。注意:lable的numberOfLines 属性必须设置为0,段落样式才能生效。
3.NSString *const NSForegroundColorAttributeName(字体颜色):
该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的字体颜色。如果不指定该属性,则默认为黑色。
4.NSString *const NSBackgroundColorAttributeName(字体背景色):
该属性所对应的值是一个 UIColor 对象。该属性用于指定一段文本的背景颜色。如果不指定该属性,则默认无背景色。
5.NSString *const NSLigatureAttributeName(连字符):
该属性所对应的值是一个 NSNumber 对象(整数)。连体字符是指某些连在一起的字符,它们采用单个的图元符号。0 表示没有连体字符。1 表示使用默认的连体字符。2表示使用所有连体符号。默认值为 1(注意,iOS 不支持值为 2)。
6.NSString *const NSKernAttributeName(字间距):
该属性所对应的值是一个 NSNumber 对象(整数)。连体字符是指某些连在一起的字符,它们采用单个的图元符号。0 表示没有连体字符。1 表示使用默认的连体字符。2表示使用所有连体符号。默认值为 1(注意,iOS 不支持值为 2)。
7.NSString *const NSStrikethroughStyleAttributeName(删除线):
该属性所对应的值是一个 NSNumber 对象(整数)。该值指定是否在文字上加上删除线,该值参考“Underline Style Attributes”。默认值是NSUnderlineStyleNone。
8.NSString *const NSUnderlineStyleAttributeName(下划线):
该属性所对应的值是一个 NSNumber 对象(整数)。该值指定是否在文字上加上下划线,该值参考“Underline Style Attributes”。默认值是NSUnderlineStyleNone。
9.NSString *const NSStrokeColorAttributeName(边线颜色):
该属性所对应的值是一个 UIColor 对象。如果该属性不指定(默认),则等同于 NSForegroundColorAttributeName。否则,指定为删除线或下划线颜色。更多细节见“Drawing attributedstrings that are both filled and stroked”。
10.NSString *const NSStrokeWidthAttributeName(边线宽度):
该属性所对应的值是一个 NSNumber 对象(小数)。该值改变描边宽度(相对于字体size 的百分比)。默认为 0,即不改变。正数只改变描边宽度。负数同时改变文字的描边和填充宽度。例如,对于常见的空心字,这个值通常为3.0。
11.NSString *const NSShadowAttributeName(阴影):
该属性所对应的值是一个 NSShadow 对象。默认为 nil。
12.NSString *const NSVerticalGlyphFormAttributeName(横竖排版):
该属性所对应的值是一个 NSNumber 对象(整数)。0 表示横排文本。1 表示竖排文本。在 iOS 中,总是使用横排文本,0 以外的值都未定义。
0 0
- 富文本之NSAttributedString与NSMutableAttributedString
- 富文本NSAttributedString与NSMutableAttributedString
- NSMutableAttributedString/NSAttributedString 富文本设置
- iOS NSMutableAttributedString/NSAttributedString 富文本设置
- iOS NSMutableAttributedString/NSAttributedString 富文本设置
- iOS NSMutableAttributedString/NSAttributedString 富文本设置
- iOS NSMutableAttributedString/NSAttributedString 富文本设置
- NSAttributedString/NSMutableAttributedString富文本的使用(NSMutableParagraphStyle)
- iphone开发中NSMutableAttributedString/NSAttributedString 富文本设置
- 富文本,NSAttributedString 与 coreText
- iOS之富文本NSAttributedString
- 12-富文本 NSMutableAttributedString
- 富文本 ---- NSMutableAttributedString
- NSMutableAttributedString实现富文本
- ios富文本NSMutableAttributedString
- NSMutableAttributedString富文本
- NSMutableAttributedString富文本处理
- 富文本处理NSMutableAttributedString
- 欢迎使用CSDN-markdown编辑器
- [LeetCode]Combinations
- play框架使用起来(8)
- Eclipse中从svn中检出maven项目
- bootstrap学习总结-css样式设计(一)
- 富文本之NSAttributedString与NSMutableAttributedString
- Protocol Buffer技术详解(1)-语言规范
- laravel项目云服务器linux部署
- 算法系列—广度优先搜索(BFS)
- Java中private、protected、public和default的区别
- 一年之中第几天
- 【GDOI2017模拟11.3】永恒的契约
- Unity3D Shader之光照模型——理论与实践:用两种方式来实现漫反射Diffuse Reflection
- 何恺明经典去雾算法