Save it for viewing at any time, thanks for sharing.
Reference website:
http://shijue.me/show_text/521c396a8ddf876566000007
http://www.tuicool.com/articles/zquENb
http://blog.csdn.net/a451493485/article/details/9454695
http://wiki. eoe.cn/page/iOS_pptl_artile_28190.html
http://www.xue5.com/Mobile/iOS/673562.html
#import "ViewController.h" #import <CoreText/CoreText.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //Learning Content /* 1.控件 UIView UILabel UITextField UITextView UIButton 2. Font, size, unit, color */ UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 260)]; label.text = @"Label Text Content, This is a text label things attribute";//默认为空 label.font = [UIFont systemFontOfSize:17];//Use the system's 17 by default label.textColor = [UIColor orangeColor];//Use text black by default label.shadowColor = [UIColor lightGrayColor];//No shadow by default label.shadowOffset = CGSizeMake(1,0);//The default is an upward shadow (0,-1) label.textAlignment = NSTextAlignmentCenter;//The default is left alignment label.lineBreakMode = NSLineBreakByTruncatingTail;//Paragraph style, the default is to truncate the tail at the end, replace it with ... //The basic data type of rich text, attributed string. Based on this, if the corresponding property is set, the property set above will be ignored, and the default is empty NSString *string = label.text; const CGFloat fontSize = 16.0; NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string]; NSUInteger length = [string length]; //set font UIFont *baseFont = [UIFont systemFontOfSize:fontSize]; [attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];//Set all fonts UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize]; [attrString addAttribute:NSFontAttributeName value:boldFont range:[string rangeOfString:@"Text"]];//Set the font of the four letters of Text to bold //Set the tilt, you need to import coreText UIFont *italicFont = GetVariationOfFontWithTrait(baseFont, kCTFontTraitItalic); [attrString addAttribute: NSFontAttributeName value: italicFont range:[string rangeOfString:@"Label"]]; // set color UIColor *color = [UIColor redColor]; [attrString addAttribute: NSForegroundColorAttributeName value:color range:[string rangeOfString:@"Content"]]; [attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"ent"]]; // You can set values for these properties //The font names are as follows: // label.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:24]; [attrString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Verdana-BoldItalic" size:18] range:[string rangeOfString:@"Label"]]; label.numberOfLines = 2; NSMutableParagraphStyle * style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; style.lineSpacing = 10;//Increase the line height style.headIndent = 10;//Head indent, equivalent to left padding style.tailIndent = -10;//equivalent to right padding style.lineHeightMultiple = 1.5;//How many times is the line spacing style.alignment = NSTextAlignmentLeft;//Alignment style.firstLineHeadIndent = 20;//The first line head is indented style.paragraphSpacing = 10;//Spacing after paragraph style.paragraphSpacingBefore = 20;//Spacing before paragraph [attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, length)]; [attrString addAttribute:NSKernAttributeName value:@2 range:NSMakeRange(0, length)];//字符间距 2pt [attrString addAttribute:NSStrokeColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"is"]];//To set the text stroke color, you need to set the stroke width with NSStrokeWidthAttributeName, so that the text can be hollow [attrString addAttribute:NSStrokeWidthAttributeName value:@2 range:[string rangeOfString:@"is"]];//Hollow characters, text border description [attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:[string rangeOfString:@"text"]];//下划线 [attrString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleThick) range:[string rangeOfString:@"label"]];//厚的下划线 [attrString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:[string rangeOfString:@"things"]];//删除线 [attrString addAttribute:NSStrikethroughColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"things"]];//strikethrough blue label.attributedText = attrString; label.highlightedTextColor = [UIColor redColor];//Set the text highlight color, used with highlighted. label.highlighted = NO; //whether the highlighted state is turned on label.enabled = YES;//Set whether the text content is variable label.userInteractionEnabled = YES;//Set whether the label ignores or removes user interaction. Default is NO label.baselineAdjustment = UIBaselineAdjustmentNone;//If the adjustsFontSizeToFitWidth property is set to YES, this property controls the behavior of the text baseline. // UIBaselineAdjustmentAlignBaselines=0, by default, the top of the text is aligned with the center line. // UIBaselineAdjustmentAlignCenters, the text center line is aligned with the label center line. // UIBaselineAdjustmentNone, the lowest end of the text is aligned with the center line of the label. ; [self.view addSubview:label]; /* The font names are as follows: Font Family: American Typewriter Font: AmericanTypewriter Font: AmericanTypewriter-Bold Font Family: AppleGothic Font: AppleGothic Font Family: Arial Font: ArialMT Font: Arial-BoldMT Font: Arial-BoldItalicMT Font: Arial-ItalicMT Font Family: Arial Rounded MT Bold Font: ArialRoundedMTBold Font Family: Arial Unicode MS Font: ArialUnicodeMS Font Family: Courier Font: Courier Font: Courier-BoldOblique Font: Courier-Oblique Font: Courier-Bold Font Family: Courier New Font: CourierNewPS-BoldMT Font: CourierNewPS-ItalicMT Font: CourierNewPS-BoldItalicMT Font: CourierNewPSMT Font Family: DB LCD Temp Font: DBLCDTempBlack Font Family: Georgia Font: Georgia-Bold Font: Georgia Font: Georgia-BoldItalic Font: Georgia-Italic Font Family: Helvetica Font: Helvetica-Oblique Font: Helvetica-BoldOblique Font: Helvetica Font: Helvetica-Bold Font Family: Helvetica Neue Font: HelveticaNeue Font: HelveticaNeue-Bold Font Family: Hiragino Kaku Gothic **** W3 Font: HiraKakuProN-W3 Font Family: Hiragino Kaku Gothic **** W6 Font: HiraKakuProN-W6 Font Family: Marker Felt Font: MarkerFelt-Thin Font Family: STHeiti J Font: STHeitiJ-Medium Font: STHeitiJ-Light Font Family: STHeiti K Font: STHeitiK-Medium Font: STHeitiK-Light Font Family: STHeiti SC Font: STHeitiSC-Medium Font: STHeitiSC-Light Font Family: STHeiti TC Font: STHeitiTC-Light Font: STHeitiTC-Medium Font Family: Times New Roman Font: TimesNewRomanPSMT Font: TimesNewRomanPS-BoldMT Font: TimesNewRomanPS-BoldItalicMT Font: TimesNewRomanPS-ItalicMT Font Family: Trebuchet MS Font: TrebuchetMS-Italic Font: TrebuchetMS Font: Trebuchet-BoldItalic Font: TrebuchetMS-Bold Font Family: Verdana Font: Verdana-Bold Font: Verdana-BoldItalic Font: Verdana Font: Verdana-Italic Font Family: Zapfino Font: Zapfino */ //text alignment /* Values for NSTextAlignment */ /* NSTextAlignmentLeft left alignment NSTextAlignmentCenter in-play alignment NSTextAlignmentRight right alignment NSTextAlignmentJustified justified NSTextAlignmentNatural aligns according to the displayed text properties */ // paragraph style /* lineSpacing; to increase the line spacing paragraphSpacing; alignment; alignment firstLineHeadIndent; the indent pixel at the beginning of the paragraph headIndent; can adjust the indentation distance of all text, can be used as left padding tailIndent; Adjusts the indentation distance at the end of the text. It should be noted that the value specified here can be used as the width of the text display, and can also be used as the padding on the right, depending on the positive or negative value of the input: lineBreakMode; minimumLineHeight; maximumLineHeight; For different font types and font sizes, we can avoid too high or too narrow by specifying the maximum and minimum line spacing (maximumLineHeight and minimumLineHeight). baseWritingDirection; lineHeightMultiple; If you want to adjust the line spacing, you can use lineHeightMultiple to change the line spacing multiple paragraphSpacingBefore; If the content of the article is divided into paragraphs, you can also specify the distance at the end of the paragraph (paragraphSpacing) and the distance at the beginning of the paragraph (paragraphSpacingBefore): hyphenationFactor; @property(readwrite,copy,NS_NONATOMIC_IOSONLY) NSArray *tabStops NS_AVAILABLE_IOS(7_0); @property(readwrite,NS_NONATOMIC_IOSONLY) CGFloat defaultTabInterval NS_AVAILABLE_IOS(7_0); */ /* Predefined character attributes for text. If the key is not in the dictionary, then use the default values as described below. //Predefined character attributes for text. If the key is not in the dictionary, then use the default values as described below. NSFontAttributeName font is Helvetica 12 by default NSParagraphStyleAttributeName paragraph style */ /* UIKIT_EXTERN NSString *const NS_AVAILABLE_IOS(6_0); // NSParagraphStyle, default defaultParagraphStyle UIKIT_EXTERN NSString *const NSForegroundColorAttributeName NS_AVAILABLE_IOS(6_0); // UIColor, default blackColor UIKIT_EXTERN NSString *const NSBackgroundColorAttributeName NS_AVAILABLE_IOS(6_0); // UIColor, default nil: no background UIKIT_EXTERN NSString *const NSLigatureAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing integer, default 1: default ligatures, 0: no ligatures UIKIT_EXTERN NSString *const NSKernAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled. (note: values other than nil and 0 are unsupported on iOS) UIKIT_EXTERN NSString *const NSStrikethroughStyleAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing integer, default 0: no strikethrough UIKIT_EXTERN NSString *const NSUnderlineStyleAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing integer, default 0: no underline UIKIT_EXTERN NSString *const NSStrokeColorAttributeName NS_AVAILABLE_IOS(6_0); // UIColor, default nil: same as foreground color UIKIT_EXTERN NSString *const NSStrokeWidthAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0) UIKIT_EXTERN NSString *const NSShadowAttributeName NS_AVAILABLE_IOS(6_0); // NSShadow, default nil: no shadow UIKIT_EXTERN NSString *const NSTextEffectAttributeName NS_AVAILABLE_IOS(7_0); // NSString, default nil: no text effect UIKIT_EXTERN NSString *const NSAttachmentAttributeName NS_AVAILABLE_IOS(7_0); // NSTextAttachment, default nil UIKIT_EXTERN NSString *const NSLinkAttributeName NS_AVAILABLE_IOS(7_0); // NSURL (preferred) or NSString UIKIT_EXTERN NSString *const NSBaselineOffsetAttributeName NS_AVAILABLE_IOS(7_0); // NSNumber containing floating point value, in points; offset from baseline, default 0 UIKIT_EXTERN NSString *const NSUnderlineColorAttributeName NS_AVAILABLE_IOS(7_0); // UIColor, default nil: same as foreground color UIKIT_EXTERN NSString *const NSStrikethroughColorAttributeName NS_AVAILABLE_IOS(7_0); // UIColor, default nil: same as foreground color UIKIT_EXTERN NSString *const NSObliquenessAttributeName NS_AVAILABLE_IOS(7_0); // NSNumber containing floating point value; skew to be applied to glyphs, default 0: no skew UIKIT_EXTERN NSString *const NSExpansionAttributeName NS_AVAILABLE_IOS(7_0); // NSNumber containing floating point value; log of expansion factor to be applied to glyphs, default 0: no expansion UIKIT_EXTERN NSString *const NSWritingDirectionAttributeName NS_AVAILABLE_IOS(7_0); // NSArray of NSNumbers representing the nested levels of writing direction overrides as defined by Unicode LRE, RLE, LRO, and RLO characters. The control characters can be obtained by masking NSWritingDirection and NSTextWritingDirection values. LRE: NSWritingDirectionLeftToRight|NSTextWritingDirectionEmbedding, RLE: NSWritingDirectionRightToLeft|NSTextWritingDirectionEmbedding, LRO: NSWritingDirectionLeftToRight|NSTextWritingDirectionOverride, RLO: NSWritingDirectionRightToLeft|NSTextWritingDirectionOverride, UIKIT_EXTERN NSString *const NSVerticalGlyphFormAttributeName NS_AVAILABLE_IOS(6_0); // An NSNumber containing an integer value. 0 means horizontal text. 1 indicates vertical text. If not specified, it could follow higher-level vertical orientation settings. Currently on iOS, it's always horizontal. The behavior for any other value is undefined. */ // NSParagraphStyle paragraph style // typedef NS_ENUM(NSInteger, NSLineBreakMode) { /* What to do with long lines */ //How to deal with long content or multi-line content // NSLineBreakByWordWrapping = 0, /* Wrap at word boundaries, default */ //Truncate at word boundaries // NSLineBreakByCharWrapping, /* Wrap at character boundaries */ //Truncate by character boundaries // NSLineBreakByClipping, /* Simply clip */ //Simple clipping // NSLineBreakByTruncatingHead, /* Truncate at head of line: "...wxyz" */ //Truncate the head // NSLineBreakByTruncatingTail, /* Truncate at tail of line: "abcd..." */ //Truncate the tail // NSLineBreakByTruncatingMiddle /* Truncate middle of line: "ab...yz" */ //Truncate middle of line // } NS_ENUM_AVAILABLE_IOS(6_0); } // get italics UIFont * GetVariationOfFontWithTrait(UIFont *baseFont, CTFontSymbolicTraits trait) { CGFloat fontSize = [baseFont pointSize]; CFStringRef baseFontName = (__bridge CFStringRef)[baseFont fontName]; CTFontRef baseCTFont = CTFontCreateWithName(baseFontName, fontSize, NULL); CTFontRef ctFont = CTFontCreateCopyWithSymbolicTraits(baseCTFont, 0, NULL, stroke, stroke); NSString *variantFontName = CFBridgingRelease(CTFontCopyName(ctFont, kCTFontPostScriptNameKey)); UIFont *variantFont = [UIFont fontWithName:variantFontName size:fontSize]; CFRelease(ctFont); CFRelease(baseCTFont); return variantFont; }; - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end