iOS NSMutableAttributedString Usage Details

Keywords: iOS Attribute

1. Property name

1> NSFontAttributeName (Font)
The value corresponding to this property is a UIFont object.This property is used to change the font of a piece of text.If this property is not specified, it defaults to 12-point Helvetica(Neue).

2> NSParagraph StyleAttributeName (Paragraph)
The value corresponding to this property is a NSParagraphStyle object.This attribute applies multiple attributes to a single piece of text.If you do not specify this property,
Default is the default paragraph property returned by the defaultParagraphStyle method of NSParagraphStyle.

3> NSForegroundColorAttributeName (font color)
The value corresponding to this property is a UIColor object.This property is used to specify the font color of a piece of text.If this property is not specified, it defaults to black.

4> NSBackgroundColorAttributeName (Font Background Color)
The value corresponding to this property is a UIColor object.This property is used to specify the background color of a piece of text.If this property is not specified, no background color is defaulted.

5> NSLigatureAttributeName (hyphen)
The value corresponding to this property is a NSNumber object (integer).Hyphenated characters refer to certain characters that are hyphenated together.
They use a single meta-symbol.0 means there are no hyphens.1 indicates that the default hyphenation character is used.2 means to use all the conjoined symbols.The default value is 1 (note that iOS does not support a value of 2).

6> NSKernAttributeName
The value corresponding to this property is a NSNumber object (integer).Alphabetical alignment specifies the number of pixels used to adjust the kerning.The effect of tight letters depends on the font.A value of 0 means no tight alphabets are used.The default value is 0.

7> NSStrikethrough StyleAttributeName (Strikeout)
The value corresponding to this property is a NSNumber object (integer).The value specifies whether to add a strikethrough to the text, which refers to Underline Style Attributes.The default value is NSUnderlineStyleNone.

8> NSUnderlineStyleAttributeName
The value corresponding to this property is a NSNumber object (integer).The value specifies whether to underline the text, which refers to Underline Style Attributes.The default value is NSUnderlineStyleNone.

9> NSStrokeColorAttributeName
The value corresponding to this property is a UIColor object.If this property is not specified (default), it is equivalent to NSForegroundColorAttributeName.
Otherwise, specify the color of the strikethrough or underscore.

10> NSStrokeWidthAttributeName (Edge Width)
The value corresponding to this property is a NSNumber object (decimal).This value changes the stroke width (as a percentage of the font size).Default is 0, that is, unchanged.Positive numbers only change the stroke width.
Negative numbers change both the description edge and fill width of the text.For example, for common hollow words, this value is usually 3.0.
 
11> NSShadowAttributeName (Shadow)
The value corresponding to this property is a NSShadow object.The default is nil.

12> NSVerticalGlyphFormAttributeName (horizontal and vertical typesetting)
The value corresponding to this property is a NSNumber object (integer).0 means horizontal text.1 stands for vertical text.In iOS, horizontal text is always used, and values other than 0 are undefined.


2. Code Effects



3. Code

  1. #import "ViewController.h"  
  2.   
  3. @interface ViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation ViewController  
  8.   
  9. - (void)viewDidLoad {  
  10.     [super viewDidLoad];  
  11.       
  12.     NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"NSAttributeString Can be used to set font, paragraph style, font color, font background color, can add strikethroughs, underscores, can set word spacing, shadows, hollow words, italics, flattening"];  
  13.     [attributedString addAttribute:NSExpansionAttributeName value:@1 range:NSMakeRange(017)];  //Flattening  
  14.     [attributedString addAttribute:NSObliquenessAttributeName value:@1 range:NSMakeRange(188)];//Tilt  
  15.       
  16.     //Paragraph  
  17.     NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];  
  18.     paragraphStyle.firstLineHeadIndent = 80;        //First line indentation  
  19.     paragraphStyle.headIndent = 25;                 //Other line indentation  
  20.     paragraphStyle.lineSpacing = 10;                //Row spacing  
  21.       
  22.     [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];//Paragraph  
  23.       
  24.       
  25.     NSShadow *shadow = [[NSShadow alloc] init];  
  26.     shadow.shadowBlurRadius = 5;    //Fuzziness  
  27.     shadow.shadowColor = [UIColor yellowColor];  
  28.     shadow.shadowOffset = CGSizeMake(13);  
  29.     [attributedString addAttribute:NSVerticalGlyphFormAttributeName value:@(0) range:NSMakeRange(274)];  
  30.     [attributedString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(274)];  
  31.     [attributedString addAttribute:NSStrokeWidthAttributeName value:@(-3.0) range:NSMakeRange(3211)];//Edge Line Width  
  32.     [attributedString addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(3211)];//Side Line Color, Side Line Width needs to be set first  
  33.     [attributedString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(447)]; //Delete line  
  34.     [attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(523)]; //Underline  
  35.       
  36.       
  37.     [attributedString setAttributes:@{  
  38.                                       NSFontAttributeName:[UIFont fontWithName:@"Arial-BoldItalicMT" size:18],      //Font, Size  
  39.                                       NSKernAttributeName:@(10),    //Word spacing  
  40.                                       NSForegroundColorAttributeName:[UIColor blueColor],  
  41.                                       NSBackgroundColorAttributeName:[UIColor brownColor]  
  42.                                       }  
  43.                               range:NSMakeRange(5620)];  
  44.      
  45.     UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];  
  46.     label.numberOfLines = 0;  
  47.     label.backgroundColor = [UIColor grayColor];  
  48.     label.attributedText = attributedString;  
  49.       
  50.     [self.view addSubview:label];  
  51. }  
  52.   
  53. @end 

Posted by alexu' on Sun, 30 Jun 2019 09:53:01 -0700