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;
- NSAttributedString的用法
- NSAttributedString的用法详解
- iOS--NSAttributedString的基本用法
- NSAttributedString用法
- NSAttributedString用法
- NSAttributedString用法
- NSAttributedString几个用法
- NSAttributedString(富文本)用法
- iOS中NSAttributedString用法
- ios学习--NSAttributedString用法 & NSMutableParagraphStyle 来创建多姿多彩的文本
- iOS NSAttributedString和NSMutableAttributedString、NSMutableParagraphStyle的详细用法
- iOS NSAttributedString和NSMutableAttributedString、NSMutableParagraphStyle的详细用法
- iOS NSAttributedString和NSMutableAttributedString、NSMutableParagraphStyle的详细用法
- iOS NSMutableParagraphStyle、NSAttributedString和NSMutableAttributedString的详细用法
- NSAttributedString的理解
- NSAttributedString的属性
- NSAttributedstring的属性
- NSAttributedString的使用
- 【js报类型未定义的错误】20140826
- golang tcp 2 unix socket proxy
- synchronized用法
- 自己动手写CPU之第七阶段(5)——流水线暂停机制的设计与实现
- Android手机在开发调试时logcat不显示输出信息的解决办法
- NSAttributedString的用法
- C#学习笔记-委托
- SQL中left join和inner join配合使用
- 斯坦福大学公开课——傅里叶变换及其应用笔记
- Comparable和Comparator2个接口的作用和区别
- LeetCode 021. Merge Two Sorted Lists
- RSA算法
- 第十章 10.3.4节练习
- hdu 1715 大斐波数(大数高精)