NSMutableAttributedString简单使用

来源:互联网 发布:卡巴斯基官网软件 编辑:程序博客网 时间:2024/04/30 12:47


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为垂直排版的字。




NSDictionary *attributes = @{ NSForegroundColorAttributeName : [ UIColorredColor

],

NSFontAttributeName : [ UIFont fontWithName : @"Zapfino"   size : 16.0

]

}

;

NSString *strDisplayText = @"This is an attributed string."

;

NSAttributedString *attributedText = [[ NSAttributedString alloc ] initWithString:strDisplayText attributes

:attributes];

self . lblInfo . attributedText = attributedText;

NSDictionary *attributes1 = @{ 
NSBackgroundColorAttributeName : [ UIColor orangeColor ], 
    NSFontAttributeName : [ UIFont fontWithName : @"Zapfino"   size : 22.0 ], 
NSKernAttributeName : @- 1.0 
    } ; 
    NSString *strDisplayText1 = @"Orange Background" ; 
    NSAttributedString *attributedText1 = [[ NSAttributedString alloc ] initWithString:strDisplayText1 attributes :attributes1]; 
    self . lblInfo1 . attributedText = attributedText1; 

NSShadow *shadow = [[ NSShadow alloc ] init ]; 
    shadow. shadowColor = [ UIColor greenColor ]; 
    shadow. shadowBlurRadius = 5.0 ; 
    shadow. shadowOffset = CGSizeMake ( 1.0 , 1.0 ); 
    NSDictionary *attributes2 = @{ 
NSUnderlineStyleAttributeName : @1 , 
NSShadowAttributeName : shadow 
    } ; 
    NSString *strDisplayText2 = @"Shadow Font" ; 
    NSAttributedString *attributedText2 = [[ NSAttributedString alloc ] initWithString:strDisplayText2 attributes :attributes2]; 
    self . lblInfo2 . attributedText = attributedText2; 

NSDictionary *subStrAttribute1 = @{ 
NSForegroundColorAttributeName : [ UIColor redColor ], 
NSStrikethroughStyleAttributeName : @2 
    } ; 
    
    NSDictionary *subStrAttribute2 = @{ 
NSForegroundColorAttributeName : [ UIColor greenColor ] 
    } ; 
    
    NSString *strDisplayText3 = @"Red and Green" ; 
    NSMutableAttributedString *attributedText3 = [[ NSMutableAttributedString alloc ]initWithString :strDisplayText3]; 
    [attributedText3 setAttributes :subStrAttribute1 range : NSMakeRange ( 0 , 3 )]; 
    [attributedText3 setAttributes :subStrAttribute2 range : NSMakeRange ( 8 , 5 )]; 
    self . lblInfo3 . attributedText = attributedText3; 

NSMutableParagraphStyle *paragraph = [[ NSMutableParagraphStyle alloc ] init

];

paragraph.

alignment = NSTextAlignmentJustified

;

paragraph.

firstLineHeadIndent = 20.0

;

paragraph.

paragraphSpacingBefore = 10.0

;

paragraph.

lineSpacing = 5

;

paragraph.

hyphenationFactor = 1.0

;

NSDictionary *attributes4 = @{ NSForegroundColorAttributeName : [ UIColorredColor

],

NSParagraphStyleAttributeName : paragraph     }

;

NSString *strDisplayText4 = @“iPad inspires creativity and ……”

;

NSAttributedString *attributedText4 = [[ NSAttributedString alloc ] initWithString : strDisplayText4 attributes :attributes4];

    self . lblInfo4 . attributedText = attributedText4;

0 0
原创粉丝点击