iOS使用NSMutableAttributedString 实现富文本(不同颜色字体、下划线等)

来源:互联网 发布:淘宝怎么装修全屏海报 编辑:程序博客网 时间:2024/05/16 05:29

在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求。之前在网上找了一些资料,有的是重绘UILabel的textLayer,有的是用html5实现的,都比较麻烦,而且很多UILabel的属性也不起作用了,效果都不理想。后来了解到NSMuttableAttstring(带属性的字符串),上面的一些需求都可以很简便的实现。

  1. 实例化方法和使用方法

实例化方法:

使用字符串初始化

  • (id)initWithString:(NSString *)str;

例:

                                    [UIFontsystemFontOfSize:15.0],NSFontAttributeName,                                    [UIColorredColor],NSForegroundColorAttributeName,                                   NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天气不错呀" attributes:attributeDict];```- (id)initWithAttributedString:(NSAttributedString *)attester;使用NSAttributedString初始化,跟NSMutableStringNSString类似使用方法:为某一范围内文字设置多个属性`- (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;`###常见的属性及说明字体NSFontAttributeName 段落格式 NSParagraphStyleAttributeName 字体颜色NSForegroundColorAttributeName 背景颜色NSBackgroundColorAttributeName  删除线格式NSStrikethroughStyleAttributeName下划线格式NSUnderlineStyleAttributeName     删除线颜色NSStrokeColorAttributeName       删除线宽度NSStrokeWidthAttributeName阴影NSShadowAttributeName 更多方法和属性说明详见苹果官方说明文档:https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html#//apple_ref/doc/uid/TP400036893.   使用实例

UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 30)];

testLabel.backgroundColor = [UIColor lightGrayColor];

testLabel.textAlignment = NSTextAlignmentCenter;

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@”今天天气不错呀”];

[AttributedStr addAttribute:NSFontAttributeName

                     value:[UIFont systemFontOfSize:16.0]                     range:NSMakeRange(2, 2)];

[AttributedStr addAttribute:NSForegroundColorAttributeName

                     value:[UIColor redColor]                     range:NSMakeRange(2, 2)];

testLabel.attributedText = AttributedStr;

[self.view addSubview:testLabel];
“`

运行效果:
这里写图片描述

另外,其他可以设置text 的控件(如UIButton,UITextField)也都有该属性,该文章不够详细,只是简单介绍,其他效果的实现参考API中更多的属性及使用方法。


NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentJustified;//设置对齐方式
paragraph.lineBreakMode = NSLineBreakByWordWrapping;

NSAttributedString的初始化方法有

-initWithString:用String初始化,并没有Attributed信息。

-initWithAttributedString:用AttributedString去初始化。

-initWithString:Attributed:用string及attribute的dictionary来初始化。

具体AttributtedString属性的键值对如下:

NSString *const NSFontAttributeName;//值为UIFont,设置字体,默认值为12-point Helvetica(Neue) 。

下面这段代码可以查看ios中可用的字体,具体那些字体长什么样,可以查看字体册工具。

NSArray *familyArray = [UIFont familyNames];for (id family in familyArray) {printf(“%s\n”,[family cStringUsingEncoding:NSUTF8StringEncoding]);NSArray *fontArray = [UIFont fontNamesForFamilyName:family];for (id font in fontArray) {printf(”   %s\n”,[font cStringUsingEncoding:NSUTF8StringEncoding]);}}NSString *const NSParagraphStyleAttributeName;//值为NSParagraphStyle,设置段落属性,默认值为[NSParagraphStyle defaultParagraphStyle]返回的值。NSMutableParagraphStyle与NSParagraphStyle包括一下属性----------  alignment //对齐方式  firstLineHeadIndent //首行缩进  headIndent //缩进  tailIndent  //尾部缩进  lineBreakMode  //断行方式  maximumLineHeight  //最大行高  minimumLineHeight  //最低行高  lineSpacing  //行距  paragraphSpacing  //段距  paragraphSpacingBefore  //段首空间  baseWritingDirection  //句子方向  lineHeightMultiple  //可变行高,乘因数。  hyphenationFactor //连字符属性NSString *const NSForegroundColorAttributeName;//值为UIColor,字体颜色,默认为黑色。NSString *const NSBackgroundColorAttributeName;//值为UIColor,字体背景色,默认没有。NSString *const NSLigatureAttributeName;//值为整型NSNumber,连字属性,一般中文用不到,在英文中可能出现相邻字母连笔的情况。0为不连笔;1为默认连笔,也是默认值;2在ios 上不支持。NSString *const NSKernAttributeName;//值为浮点数NSNumber,字距属性,默认值为0。NSString *const NSStrikethroughStyleAttributeName;//值为整型NSNumber,可取值为----------

enum {

NSUnderlineStyleNone = 0×00,

NSUnderlineStyleSingle = 0×01,

};设置删除线。
“`

NSString *const NSUnderlineStyleAttributeName;//同上。设置下划线。

NSString *const NSStrokeColorAttributeName;//值为UIColor,默认值为nil,设置的属性同ForegroundColor。

NSString *const NSStrokeWidthAttributeName;//值为浮点数NSNumber。设置比画的粗细。

NSString *const NSShadowAttributeName;//值为NSShadow,设置比画的阴影,默认值为nil。

NSString *const NSVerticalGlyphFormAttributeName;//值为整型NSNumber,0为水平排版的字,1为垂直排版的字。

0 0
原创粉丝点击