IOS NSMutableAttributedString 富文本的设置

来源:互联网 发布:php旅游网站文献 编辑:程序博客网 时间:2024/05/22 04:48


1.NSAttributedString的初始化方法有:

1. -initWithString:用String初始化,并没有Attributed信息。
2. -initWithAttributedString:用AttributedString去初始化。
3. -initWithString:Attributed:用string及attribute的dictionary来初始化。


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

NSString *const NSFontAttributeName;//值为UIFont,设置字体,默认值为12-point Helvetica(Neue) 。
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为垂直排版的字。


下面是代码运行的主要例子:   

#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue &0xFF00) >>8))/255.0 blue:((float)(rgbValue &0xFF))/255.0 alpha:1.0]  // 这个是颜色的宏定义


// 主要的代码

 NSString *mStr = @"119当天支付就有奖品";

 NSString *mStrHint = [NSString stringWithFormat:@"%@ 查看详情",mStr];

            UILabel *mLableTitle = [selfaddLableText:CGRectMake(0, 18, Screen_width, 15) withLabText:[SinglecreatDataHandle].mStrPresentHintwithLabFont:[UIFontfontWithName:HeitiSCLight size:18] withTextColor:UIColorFromRGB(0xFF3587)];

            mLableTitle.textAlignment = NSTextAlignmentCenter;

            mLableTitle.userInteractionEnabled = YES;

            [self addSubview:mLableTitle];

            

            NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:mStrHint];

            // 设置一段话的字体是从0 ~ 整个字符串的长度 设置字体,也可以为某几个字设置字体

            [AttributedStr addAttribute:NSFontAttributeName

             

                                  value:[UIFont systemFontOfSize:18.0]

             

                                  range:NSMakeRange(0, mStrHint.length)];

            // 设置一段话的字体是从0 ~ 整个字符串的长度 设置字体的颜色,也可以为某几个字设置字体的颜色

            [AttributedStr addAttribute:NSForegroundColorAttributeName

             

                                  value:UIColorFromRGB(0x8D52FD)

             

                                  range:NSMakeRange(mStrHint.length - 4 , 4)];

            

           // 设置一段话的字体是从后4个字符串 设置字体有下划线,

            [AttributedStr addAttribute:NSUnderlineStyleAttributeName 

                                  value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] 

                                  range:NSMakeRange(mStrHint.length - 4 , 4)];

            mLableTitle.attributedText = AttributedStr;

            [self addSubview:mLableTitle];


运行结果:



参考的链接:http://blog.csdn.net/wenluma/article/details/12838983


0 0
原创粉丝点击