字體屬性

字體屬性設置示例:

if(color ==nil) {

color = [NSColor redColor];

}

NSFont *font = button.font;

NSMutableParagraphStyle *centredStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];

[centredStyle setAlignment:NSCenterTextAlignment];

NSDictionary * attrs = [NSDictionary dictionaryWithObjectsAndKeys:font,

NSFontAttributeName,

color,

NSForegroundColorAttributeName,

centredStyle,

NSParagraphStyleAttributeName,

nil];

NSAttributedString * attributedString = [[NSAttributedString alloc]initWithString:[button title] attributes:attrs];

[button setAttributedTitle:attributedString];

以下為參數的設定

// NSFontAttributeName? ? ? ? ? ? ? ? 設置字體屬性,默認值:字體:Helvetica(Neue) 字號:12 ?

//NSForegroundColorAttributeName 設置字體顏色,取值為 UIColor,默認為黑色 ?

NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };??

?NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };??

?NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };? ? ??

_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];?

?_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];??

?_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


// NSForegroundColorAttributeNam? ? ? 設置字體顏色,取值為 UIColor對象,默認值為黑色

//NSForegroundColorAttributeName 設置字體顏色,取值為 UIColor,默認為黑色

NSDictionary *attrDict1 = @{ NSForegroundColorAttributeName: [UIColor redColor] };??

?NSDictionary *attrDict2 = @{ NSForegroundColorAttributeName: [UIColor blueColor] };??

?NSDictionary *attrDict3 = @{ NSForegroundColorAttributeName: [UIColor orangeColor] };? ? ??

_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];??

?_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];?

?_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3]; ? ? ? ?


?// NSBackgroundColorAttributeName? ? 設置字體所在區域背景顏色,取值為 UIColor對象,默認值為nil, 透明色 ?

NSDictionary *attrDict4 = @{ NSBackgroundColorAttributeName: [UIColor orangeColor] };?

NSDictionary *attrDict5 = @{ NSBackgroundColorAttributeName: [UIColor redColor] };?

NSDictionary *attrDict6 = @{ NSBackgroundColorAttributeName: [UIColor cyanColor] };? ? ?

_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict4];?

_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict5];?

_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict6];

NSForegroundColorAttributeName 和 NSBackgroundColorAttributeName 的低位是相等的,跟前面介紹的 textColor 一樣,哪個屬性最后一次賦值,就會沖掉前面的效果


// NSLigatureAttributeName? ? ? ? ? ? 設置連體屬性,取值為NSNumber 對象(整數),0 表示沒有連體字符,1 表示使用默認的連體字符??

//NSLigatureAttributeName 設置連體屬性,取值為NSNumber 對象(整數),0 表示沒有連體字符,1 表示使用默認的連體字符, ?// ?2 表示使用所有連體符號,默認值為 1(iOS 不支持 2) ? ? ?

NSString *ligatureStr = @"flush";? ? ??

NSDictionary *attrDict1 = @{ NSLigatureAttributeName: [NSNumber numberWithInt: 0], ? ? ? ? ? ? ? ? ? ? ? ? ? ? ???

NSFontAttributeName: [UIFont fontWithName: @"futura" size: 30] };??

_label01.attributedText = [[NSAttributedString alloc] initWithString: ligatureStr attributes: attrDict1];? ? ??

NSDictionary *attrDict2 = @{ NSLigatureAttributeName: @(1),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont fontWithName: @"futura" size: 30]? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? };??

?_label02.attributedText = [[NSAttributedString alloc] initWithString: ligatureStr attributes: attrDict2];

由于要展示連體字符,所以將前面使用的帶有中文的字符串換成 flush

NSLigatureAttributeName的取值為NSNumber對象,所以不能直接將一個整數值賦給它,創建?NSNumber 對象的方法有很多,或者可以簡寫成 @(int)


// NSKernAttributeName? ? ? ? ? ? ? ? 設定字符間距,取值為 NSNumber 對象(整數),正值間距加寬,負值間距變窄 ?

NSDictionary *attrDict1 = @{ NSKernAttributeName: @(-3),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize: 20]};??

_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];? ? ? ??

?NSDictionary *attrDict2 = @{ NSKernAttributeName: @(0),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize: 20]};??

_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];? ? ? ??

?NSDictionary *attrDict3 = @{ NSKernAttributeName: @(10),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize: 20]};??

_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


// NSStrikethroughStyleAttributeName? 設置刪除線,取值為 NSNumber 對象(整數)??

//NSStrikethroughStyleAttributeName 設置刪除線,取值為 NSNumber 對象(整數),枚舉常量 NSUnderlineStyle中的值??

?// NSUnderlineStyleNone? 不設置刪除線??

// NSUnderlineStyleSingle 設置刪除線為細單實線??

// NSUnderlineStyleThick? 設置刪除線為粗單實線??

// NSUnderlineStyleDouble 設置刪除線為細雙實線? ? ? ??

?NSDictionary *attrDict1 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

NSFontAttributeName: [UIFont systemFontOfSize:20] };??

?_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];? ? ? ??

?NSDictionary *attrDict2 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleThick),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

NSFontAttributeName: [UIFont systemFontOfSize:20] };??

?_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];? ? ? ??

?NSDictionary *attrDict3 = @{ NSStrikethroughStyleAttributeName: @(NSUnderlineStyleDouble),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

NSFontAttributeName: [UIFont systemFontOfSize:20] };??

?_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];

取值為 0 - 7時,效果為單實線,隨著值得增加,單實線逐漸變粗,取值為 9 - 15時,效果為雙實線,取值越大,雙實線越粗

雖然使用了枚舉常量,但是枚舉常量的本質仍為整數,所以同樣必須先轉化為 NSNumber 才能使用

刪除線和下劃線使用相同的枚舉常量作為其屬性值

目前iOS中只有上面列出的4中效果,雖然我們能夠在頭文件中發現其他更多的取值,但是使用后沒有任何效果


// NSStrikethroughColorAttributeName? 設置刪除線顏色,取值為 UIColor 對象,默認值為黑色?? ??

NSDictionary *attrDict1 = @{ NSStrikethroughColorAttributeName: [UIColor blueColor],? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

NSStrikethroughStyleAttributeName: @(1),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

?_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];? ? ? ??

?NSDictionary *attrDict2 = @{ NSStrikethroughColorAttributeName: [UIColor orangeColor],? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

NSStrikethroughStyleAttributeName: @(3),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

?_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];? ? ? ??

?NSDictionary *attrDict3 = @{ NSStrikethroughColorAttributeName: [UIColor greenColor],? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

NSStrikethroughStyleAttributeName: @(7),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

?_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


?// NSUnderlineStyleAttributeName? ? ? 設置下劃線,取值為 NSNumber 對象(整數),枚舉常量 NSUnderlineStyle中的值,與刪除線類似??

?NSDictionary *attrDict1 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??

NSFontAttributeName: [UIFont systemFontOfSize:20] };?

?_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];? ? ? ??

?NSDictionary *attrDict2 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick), NSFontAttributeName: [UIFont systemFontOfSize:20] };??

?_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];? ? ? ??

?NSDictionary *attrDict3 = @{ NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),NSFontAttributeName: [UIFont systemFontOfSize:20] };??

?_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


?// NSUnderlineColorAttributeName? ? ? 設置下劃線顏色,取值為 UIColor 對象,默認值為黑色??

NSDictionary *attrDict1 = @{ NSUnderlineColorAttributeName: [UIColor blueColor],NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),NSFontAttributeName: [UIFont systemFontOfSize:20] };??

?_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];? ? ? ??

?NSDictionary *attrDict2 = @{ NSUnderlineColorAttributeName: [UIColor orangeColor],NSUnderlineStyleAttributeName: @(NSUnderlineStyleThick),NSFontAttributeName: [UIFont systemFontOfSize:20] };??

?_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];? ? ? ??

?NSDictionary *attrDict3 = @{ NSUnderlineColorAttributeName: [UIColor greenColor],NSUnderlineStyleAttributeName: @(NSUnderlineStyleDouble),NSFontAttributeName: [UIFont systemFontOfSize:20] };??

?_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


// NSStrokeWidthAttributeName? ? ? ? 設置筆畫寬度,取值為 NSNumber 對象(整數),負值填充效果,正值中空效果??

NSDictionary *attrDict1 = @{ NSStrokeWidthAttributeName: @(-3),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:30] };??

_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];? ? ? ??

?NSDictionary *attrDict2 = @{ NSStrokeWidthAttributeName: @(0),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:30] };??

_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];? ? ? ??

?NSDictionary *attrDict3 = @{ NSStrokeWidthAttributeName: @(3),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:30] };??

_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


?// NSStrokeColorAttributeName? ? ? ? 填充部分顏色,不是字體顏色,取值為 UIColor 對象??

NSDictionary *attrDict1 = @{ NSStrokeWidthAttributeName: @(-3),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSStrokeColorAttributeName: [UIColor orangeColor],? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:30] };??

?_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];? ? ? ??

?NSDictionary *attrDict2 = @{ NSStrokeWidthAttributeName: @(0),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSStrokeColorAttributeName: [UIColor blueColor],? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:30] };?

_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];? ? ? ??

?NSDictionary *attrDict3 = @{ NSStrokeWidthAttributeName: @(3),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSStrokeColorAttributeName: [UIColor greenColor],? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:30] };??

?_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


?// NSShadowAttributeName? ? ? ? ? ? ? 設置陰影屬性,取值為 NSShadow 對象

NSShadow *shadow1 = [[NSShadow alloc] init];??

//NSShadow 對象比較簡單,只有3個屬性:陰影顏色,模糊半徑和偏移? shadow1.shadowOffset = CGSizeMake(3, 3);//陰影偏移(X方向偏移和Y方向偏移)??

?shadow1.shadowBlurRadius = 0.5;//模糊半徑??

?shadow1.shadowColor = [UIColor orangeColor];? //陰影顏色? ? ??

NSShadow *shadow2 = [[NSShadow alloc] init];??

?shadow2.shadowOffset = CGSizeMake(3, 16);? shadow2.shadowBlurRadius = 2.5;??

?shadow2.shadowColor = [UIColor purpleColor];? ? ??

NSShadow *shadow3 = [[NSShadow alloc] init];??

?shadow3.shadowOffset = CGSizeMake(16, 3);??

?shadow3.shadowBlurRadius = 4.0;??

?shadow3.shadowColor = [UIColor blueColor];? ? ??

NSDictionary *attrDict1 = @{ NSShadowAttributeName: shadow1,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];? ? ? ??

?NSDictionary *attrDict2 = @{ NSShadowAttributeName: shadow2,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];? ? ??

NSDictionary *attrDict3 = @{ NSShadowAttributeName: shadow3,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


?// NSTextEffectAttributeName? ? ? ? ? 設置文本特殊效果,取值為 NSString 對象,目前只有圖版印刷效果可用:

//NSTextEffectLetterpressStyle(凸版印刷效果),適用于iOS 7.0及以上? ? ? ??

NSDictionary *attrDict1 = @{ NSTextEffectAttributeName: NSTextEffectLetterpressStyle,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSForegroundColorAttributeName: [UIColor grayColor],? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:30] };??

?_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];? ? ? ??

?NSDictionary *attrDict2 = @{ //NSTextEffectAttributeName: NSTextEffectLetterpressStyle,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSForegroundColorAttributeName: [UIColor grayColor],? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:30] };??

?_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];? ? ??

NSDictionary *attrDict3 = @{ NSTextEffectAttributeName: NSTextEffectLetterpressStyle,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSForegroundColorAttributeName: [UIColor blueColor],? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:30] };??

?_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


// NSBaselineOffsetAttributeName? ? ? 設置基線偏移值,取值為 NSNumber (float),正值上偏,負值下偏??

NSDictionary *attrDict1 = @{ NSBaselineOffsetAttributeName: @(-10),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];? ? ? ??

?NSDictionary *attrDict2 = @{ NSBaselineOffsetAttributeName: @(0),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];? ? ??

NSDictionary *attrDict3 = @{ NSBaselineOffsetAttributeName: @(10),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


?// NSObliquenessAttributeName? ? ? ? 設置字形傾斜度,取值為 NSNumber (float),正值右傾,負值左傾??

NSDictionary *attrDict1 = @{ NSObliquenessAttributeName: @(-0.5),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:30] };??

_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];? ? ? ??

?NSDictionary *attrDict2 = @{ NSObliquenessAttributeName: @(0),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:30] };??

_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];? ? ??

NSDictionary *attrDict3 = @{ NSObliquenessAttributeName: @(0.8),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:30] };??

_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


?// NSExpansionAttributeName? ? ? ? ? 設置文本橫向拉伸屬性,取值為 NSNumber (float),正值橫向拉伸文本,負值橫向壓縮文本??

NSDictionary *attrDict1 = @{ NSExpansionAttributeName: @(-1),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];? ? ? ??

?NSDictionary *attrDict2 = @{ NSExpansionAttributeName: @(0),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];? ? ??

NSDictionary *attrDict3 = @{ NSExpansionAttributeName: @(0.6),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


?// NSWritingDirectionAttributeName? ? 設置文字書寫方向,從左向右書寫或者從右向左書寫??

//NSWritingDirectionAttributeName 設置文字書寫方向,取值為以下組合? ? ? //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)]? //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)]? //@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding)]? //@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]? ? ??

NSDictionary *attrDict1 = @{ NSWritingDirectionAttributeName: @[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)],? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];? ? ??

NSDictionary *attrDict2 = @{ NSWritingDirectionAttributeName: @[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)],? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];? ? ??

NSDictionary *attrDict3 = @{ NSWritingDirectionAttributeName: @[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)],? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


?// NSVerticalGlyphFormAttributeName? 設置文字排版方向,取值為 NSNumber 對象(整數),0 表示橫排文本,1 表示豎排文本??

//在 iOS 中,總是使用橫排文本,0 以外的值都未定義? ? ??

NSDictionary *attrDict1 = @{ NSVerticalGlyphFormAttributeName: @(-10),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];? ? ? ??

?NSDictionary *attrDict2 = @{ NSVerticalGlyphFormAttributeName: @(0),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label02.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict2];? ? ??

NSDictionary *attrDict3 = @{ NSVerticalGlyphFormAttributeName: @(10),? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };??

_label03.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict3];


?// NSLinkAttributeName? ? ? ? ? ? ? ? 設置鏈接屬性,點擊后調用瀏覽器打開指定URL地址??

鏈接屬性點擊將啟動瀏覽器打開一個URL地址,中間用到一個代理函數,UILabel 和 UITextField 無法使用該屬性,所以只能用UITextView來做示例。

NSDictionary *attrDict1 = @{ NSLinkAttributeName: [NSURL URLWithString: @"http://www.baidu.com"],? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NSFontAttributeName: [UIFont systemFontOfSize:20] };? ? ?

_textview01.editable = NO;? ? ? ? //必須禁止輸入,否則點擊將彈出輸入鍵盤?

_textview01.scrollEnabled = NO;? //可選?

_textview01.delegate = self;? ? ? //必須設置,否則代理函數不會被回調? ? ?

_textview01.attributedText = [[NSAttributedString alloc] initWithString: originStr attributes: attrDict1];

代理函數

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange{? ? ??
NSLog(@"textView is clicked...");? ? ??
return YES;? }

// NSAttachmentAttributeName? ? ? ? ? 設置文本附件,取值為NSTextAttachment對象,常用于文字圖片混排??

NSTextAttachment *textAttachment01 = [[NSTextAttachment alloc] init];??

textAttachment01.image = [UIImage imageNamed: @"10000.jpeg"];? //設置圖片源??

textAttachment01.bounds = CGRectMake(0, 0, 30, 30);? ? ? ? ? //設置圖片位置和大小??

?NSMutableAttributedString *attrStr01 = [[NSMutableAttributedString alloc] initWithString: originStr];? ? ??

[attrStr01 addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 25] range: NSMakeRange(0, originStr.length)];? NSAttributedString *attrStr11 = [NSAttributedString attributedStringWithAttachment: textAttachment01];? ? ??

[attrStr01 insertAttributedString: attrStr11 atIndex: 2];? //NSTextAttachment占用一個字符長度,插入后原字符串長度增加1? ? ??

_label01.attributedText = attrStr01;? ? ? ??

?NSTextAttachment *textAttachment02 = [[NSTextAttachment alloc] init];??

textAttachment02.image = [UIImage imageNamed: @"10000.jpeg"];? //設置圖片源??

textAttachment02.bounds = CGRectMake(0, -10, 30, 40);? ? ? ? ? //設置圖片位置和大小??

?NSMutableAttributedString *attrStr02 = [[NSMutableAttributedString alloc] initWithString: originStr];? ? ??

[attrStr02 addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 25] range: NSMakeRange(0, originStr.length)];??

?NSAttributedString *attrStr12 = [NSAttributedString attributedStringWithAttachment: textAttachment02];? ? ??

[attrStr02 insertAttributedString: attrStr12 atIndex: 6];? ? ??

_label02.attributedText = attrStr02;? ? ??

NSTextAttachment *textAttachment03 = [[NSTextAttachment alloc] init];??

textAttachment03.image = [UIImage imageNamed: @"10000.jpeg"];? //設置圖片源??

textAttachment03.bounds = CGRectMake(0, -6, 50, 30);? ? ? ? ? //設置圖片位置和大小??

?NSMutableAttributedString *attrStr03 = [[NSMutableAttributedString alloc] initWithString: originStr];? ? ?

[attrStr03 addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 25] range: NSMakeRange(0, originStr.length)];??

?NSAttributedString *attrStr13 = [NSAttributedString attributedStringWithAttachment: textAttachment03];? ? ??

[attrStr03 insertAttributedString: attrStr13 atIndex: 8];? ? ??

_label03.attributedText = attrStr03;


// NSParagraphStyleAttributeName? ? ? 設置文本段落排版格式,取值為 NSParagraphStyle 對象

設置文本段落排版格式,取值為 NSParagraphStyle/NSMutableParagraphStyle 對象,可以設置如下屬性:

// alignment? ? ? ? ? ? ? 對齊方式,取值枚舉常量 NSTextAlignment??

enum { ? ? ? ?

NSTextAlignmentLeft? ? ? = 0,?

NSTextAlignmentCenter? ? = 1,

NSTextAlignmentRight? ? = 2,?

NSTextAlignmentJustified = 3,

NSTextAlignmentNatural? = 4,};?

?// firstLineHeadIndent? ? 首行縮進,取值 float ?

// headIndent? ? ? ? ? ? ? 縮進,取值 float??

// tailIndent? ? ? ? ? ? ? 尾部縮進,取值 float? 注意距離是從行首算起

// lineHeightMultiple? ? ? 可變行高,乘因數,取值 float? // maximumLineHeight? ? ? 最大行高,取值 float?

行高倍數因子,大于1行高變小,小于1行高變小,實際上字體大小不會改變,改變的時行間距

?// minimumLineHeight? ? ? 最小行高,取值 float??

//? manimumLineHeight? ? 最大行高,取值 float

最大行高,若其值小于默認行高,則行間距變小,若其值大于默認行高,則不會引起任何變化

最小行高,若其值大于默認行高,則行間距變大,若其值小于默認行高,則不會引起任何變化

?// lineSpacing? ? ? ? ? ? 行距,取值 float?

行距,取值為 float,可正可負,正值增加行距,負值減小行距

// paragraphSpacing? ? ? ? 段距,取值 float ?負值無效,取0值

?// paragraphSpacingBefore? 段首空間,取值 float??

?//baseWritingDirection? ? 句子方向,取值枚舉常量 NSWritingDirection?

enum { ? ? ? ?

NSWritingDirectionNatural = -1, ? ? ? ?

NSWritingDirectionLeftToRight =? 0,?

NSWritingDirectionRightToLeft =? 1 };

?// lineBreakMode? ? ? ? ? 斷行方式,取值枚舉常量 NSLineBreakMode??

enum {

NSLineBreakByWordWrapping = 0, //自動換行,單詞切斷?

NSLineBreakByCharWrapping,? ? //自動換行,字母切斷?

NSLineBreakByClipping,? ? ? ? //非自動換行,不切斷?

NSLineBreakByTruncatingHead,? //非自動換行,行首切斷?

NSLineBreakByTruncatingTail,? //非自動換行,行尾切斷?

NSLineBreakByTruncatingMiddle? //非自動換行,中間切斷 ?};

?// hyphenationFactor? ? ? 連字符屬性,取值 0 - 1

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,428評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,024評論 3 413
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,285評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,548評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,328評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,878評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,971評論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,098評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,616評論 1 331
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,554評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,725評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,243評論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 43,971評論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,361評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,613評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,339評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,695評論 2 370

推薦閱讀更多精彩內容

  • 與NSString類似,在iOS中AttributedString也分為NSAttributedString和 N...
    錢十六閱讀 783評論 0 0
  • - (void)loadView { [super loadView]; //1.UILable的大小自適應實例:...
    一杯冰可樂閱讀 9,063評論 0 7
  • 轉載:http://blog.csdn.net/u010330109/article/details/518821...
    F麥子閱讀 4,195評論 0 3
  • 明明已經相隔 十萬八千里, 可我的心里 總覺得 還有一個你 和我在一起, 你在背后默默地 注視著我, 給我勇氣, ...
    金賽月閱讀 305評論 2 2
  • 感恩爸爸做的好飯菜 感恩媽媽的提醒 感恩寶寶給我帶來快樂 感恩師父對我的教導 感恩同事的幫助 感恩朋友幫忙交話費 ...
    海清_3a07閱讀 149評論 0 1