1.NSKernAttributeName: @10 調整字句 kerning 字句調整 php
2.NSFontAttributeName : [UIFont systemFontOfSize:_fontSize] 設置字體 html
3.NSForegroundColorAttributeName :[UIColor redColor] 設置文字顏色 app
4.NSParagraphStyleAttributeName : paragraph 設置段落樣式 ide
5.NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];學習
paragraph.alignment = NSTextAlignmentCenter;字體
6.NSBackgroundColorAttributeName: [UIColor blackColor] 設置背景顏色 ui
7.NSStrokeColorAttributeName設置文字描邊顏色,須要和NSStrokeWidthAttributeName設置描邊寬度,這樣就能使文字空心.url
NSStrokeWidthAttributeName這個屬性所對應的值是一個 NSNumber 對象(小數)。該值改變描邊寬度(相對於字體size 的百分比)。默認爲 0,即不改變。正數只改變描邊寬度。負數同時改變文字的描邊和填充寬度。例如,對於常見的空心字,這個值一般爲3.0。spa
同時設置了空心的兩個屬性,而且NSStrokeWidthAttributeName屬性設置爲整數,文字前景色就無效果了.net
效果:
效果:
8. NSStrikethroughStyleAttributeName 添加刪除線,strikethrough刪除線
效果:
9. NSUnderlineStyleAttributeName 添加下劃線
效果:
10. NSShadowAttributeName 設置陰影,單獨設置很差使,必須和其餘屬性搭配纔好使。
和這三個任一個都好使,NSVerticalGlyphFormAttributeName,NSObliquenessAttributeName,NSExpansionAttributeName
11.NSVerticalGlyphFormAttributeName
該屬性所對應的值是一個 NSNumber 對象(整數)。0 表示橫排文本。1 表示豎排文本。在 iOS 中,老是使用橫排文本,0 之外的值都未定義。
效果:
12. NSObliquenessAttributeName設置字體傾斜。Skew 斜
效果:
13. NSExpansionAttributeName 設置文本扁平化
效果:
Example:
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]; //學習內容 /* 1.控件 UIView UILabel UITextField UITextView UIButton 2.字體、大小、單位、顏色 */ 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];//默認使用系統的17 label.textColor = [UIColor orangeColor];//默認使用文本黑色 label.shadowColor = [UIColor lightGrayColor];//默認沒有陰影 label.shadowOffset = CGSizeMake(1,0);//默認是一個向上的陰影(0,-1) label.textAlignment = NSTextAlignmentCenter;//默認是左對齊 label.lineBreakMode = NSLineBreakByTruncatingTail;//段落樣式,默認是最後截斷尾巴,用...代替 //富文本的基本數據類型,屬性字符串。以此爲基礎,若是這個設置了相應的屬性,則會忽略上面設置的屬性,默認爲空 NSString *string = label.text; const CGFloat fontSize = 16.0; NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:string]; NSUInteger length = [string length]; //設置字體 UIFont *baseFont = [UIFont systemFontOfSize:fontSize]; [attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];//設置全部的字體 UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize]; [attrString addAttribute:NSFontAttributeName value:boldFont range:[string rangeOfString:@"Text"]];//設置Text這四個字母的字體爲粗體 //設置傾斜,須要導入coreText UIFont *italicFont = GetVariationOfFontWithTrait(baseFont, kCTFontTraitItalic); [attrString addAttribute:NSFontAttributeName value:italicFont range:[string rangeOfString:@"Label"]]; // 設置顏色 UIColor *color = [UIColor redColor]; [attrString addAttribute:NSForegroundColorAttributeName value:color range:[string rangeOfString:@"Content"]]; [attrString addAttribute:NSBackgroundColorAttributeName value:[UIColor blueColor] range:[string rangeOfString:@"ent"]]; //能夠對這些屬性設置值 //字體名稱有如下: // 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;//增長行高 style.headIndent = 10;//頭部縮進,至關於左padding style.tailIndent = -10;//至關於右padding style.lineHeightMultiple = 1.5;//行間距是多少倍 style.alignment = NSTextAlignmentLeft;//對齊方式 style.firstLineHeadIndent = 20;//首行頭縮進 style.paragraphSpacing = 10;//段落後面的間距 style.paragraphSpacingBefore = 20;//段落以前的間距 [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"]];//設置文字描邊顏色,須要和NSStrokeWidthAttributeName設置描邊寬度,這樣就能使文字空心 [attrString addAttribute:NSStrokeWidthAttributeName value:@2 range:[string rangeOfString:@"is"]];//空心字,文字邊框描述 [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"]];//刪除線藍色 label.attributedText = attrString; label.highlightedTextColor = [UIColor redColor];//設置文本高亮顯示顏色,與highlighted一塊兒使用。 label.highlighted = NO; //高亮狀態是否打開 label.enabled = YES;//設置文字內容是否可變 label.userInteractionEnabled = YES;//設置標籤是否忽略或移除用戶交互。默認爲NO label.baselineAdjustment = UIBaselineAdjustmentNone;//若是adjustsFontSizeToFitWidth屬性設置爲YES,這個屬性就來控制文本基線的行爲。 // UIBaselineAdjustmentAlignBaselines=0,默認,文本最上端與中線對齊。 // UIBaselineAdjustmentAlignCenters, 文本中線與label中線對齊。 // UIBaselineAdjustmentNone, 文本最低端與label中線對齊。; [self.view addSubview:label]; /* 字體名以下: 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 */ //文本對齊方式 /* Values for NSTextAlignment */ /* NSTextAlignmentLeft 左對齊 NSTextAlignmentCenter 劇中對齊 NSTextAlignmentRight 右對齊 NSTextAlignmentJustified 兩端對齊 NSTextAlignmentNatural 根據顯示的文字特性對齊 */ //段落樣式 /* lineSpacing; 來增長行距 paragraphSpacing; alignment; 對齊 firstLineHeadIndent; 段落開始的縮排像素 headIndent; 可調整所有文字的縮排距離,可看成左邊 padding 使用 tailIndent; 可調整文字尾端的縮排距離。須要注意的是,這裏指定的值能夠看成文字顯示的寬、而也可看成右邊 padding 使用,依據輸入的正負值而定: lineBreakMode; minimumLineHeight; maximumLineHeight; 而針對不一樣的字型與字號,咱們能夠透過指定最大與最小行距(maximumLineHeight 與 minimumLineHeight)來避免太高或過窄的情況發生。 baseWritingDirection; lineHeightMultiple; 想要調整行距,能夠透過搭配使用 lineHeightMultiple 更改行距倍數 paragraphSpacingBefore; 而如果文章內容有分段落的話,也能夠透過指定段落結尾距離(paragraphSpacing)以及段落開頭距離(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. //預約義的文本屬性值,若是鍵不是一個字典,那麼使用默認的值做爲如下描述 NSFontAttributeName 字體 默認是Helvetica 12號 NSParagraphStyleAttributeName 段落樣式 */ /* 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 段落樣式 // typedef NS_ENUM(NSInteger, NSLineBreakMode) { /* What to do with long lines */ //對於長內容或多行內容的處理方式 // NSLineBreakByWordWrapping = 0, /* Wrap at word boundaries, default */ //按包含單詞爲界限截斷 // NSLineBreakByCharWrapping, /* Wrap at character boundaries */ //按字符爲界限截斷 // NSLineBreakByClipping, /* Simply clip */ //簡單的修剪 // NSLineBreakByTruncatingHead, /* Truncate at head of line: "...wxyz" */ //截斷頭部 // NSLineBreakByTruncatingTail, /* Truncate at tail of line: "abcd..." */ //截斷尾巴 // NSLineBreakByTruncatingMiddle /* Truncate middle of line: "ab...yz" */ //截斷中間 // } NS_ENUM_AVAILABLE_IOS(6_0); } //獲取斜體 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, trait, trait); 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