富文本(NSMuttableAttstring)
在iOS开发中,有时会需要将一段文字显示成不同大小,不同颜色的需求,我们可以借助NSAttstring和NSMuttaleAttstring来实现
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:gradeString];
[attr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:_gradeFontSize*2] range:NSMakeRange(0, 1)];
[attr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:_gradeFontSize] range:NSMakeRange(1, 1)];
[attr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, attr.length)];
_gradeView.attributedText = attr;Button设置富文本标题
- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(UIControlState)stateLabel设置富文本字符串
label.attributedText = *****;
1.初始化方法
- (id)initWithString:(NSString *)str;- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;2.方法
为某一范围内文字设置多个属性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
为某一范围内文字添加某个属性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
为某一范围内文字添加多个属性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
移除某范围内的某个属性
- (void)removeAttribute:(NSString *)name range:(NSRange)range;
3.字段和说明
字体NSFontAttributeName 段落格式
NSParagraphStyleAttributeName 字体颜色
NSForegroundColorAttributeName 背景颜色
NSBackgroundColorAttributeName 删除线格式
NSStrikethroughStyleAttributeName下划线格式
NSUnderlineStyleAttributeName
删除线颜色
NSStrokeColorAttributeName 删除线宽度
NSStrokeWidthAttributeName阴影
NSShadowAttributeName
本文介绍如何使用NSMuttableAttributedString在iOS应用中实现富文本效果,包括不同大小、颜色的文字显示,以及如何为按钮和标签设置富文本。
2036

被折叠的 条评论
为什么被折叠?



