關(guān)于NSAttributedString只記得幾個常用的屬性,有時候要用得特殊的屬性的時候就得到處去翻,在這里記錄一下闲先,以后要用的時候就不用到處去找了羞酗。
一個簡單的例子
繪制不同顏色不同字體的一個AttributeString,如圖
代碼如下:
UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
[self.view addSubview:Label];
NSMutableAttributedString * firstPart = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
NSDictionary * firstAttributes = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName:[UIColor redColor],};
[firstPart setAttributes:firstAttributes range:NSMakeRange(0,firstPart.length)];
NSMutableAttributedString * secondPart = [[NSMutableAttributedString alloc] initWithString:@" Huang"];
NSDictionary * secondAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[UIColor blueColor],};
[secondPart setAttributes:secondAttributes range:NSMakeRange(0,secondPart.length)];
[firstPart appendAttributedString:secondPart];
Label.attributedText = firstPart;
先看看所有的Key
NSFontAttributeName; //字體腐宋,value是UIFont對象
NSParagraphStyleAttributeName;//繪圖的風(fēng)格(居中,換行模式整慎,間距等諸多風(fēng)格)脏款,value是NSParagraphStyle對象
NSForegroundColorAttributeName;// 文字顏色,value是UIFont對象
NSBackgroundColorAttributeName;// 背景色裤园,value是UIFont
NSLigatureAttributeName; // 字符連體撤师,value是NSNumber
NSKernAttributeName; // 字符間隔
NSStrikethroughStyleAttributeName;//刪除線,value是NSNumber
NSUnderlineStyleAttributeName;//下劃線拧揽,value是NSNumber
NSStrokeColorAttributeName; //描繪邊顏色剃盾,value是UIColor
NSStrokeWidthAttributeName; //描邊寬度腺占,value是NSNumber
NSShadowAttributeName; //陰影,value是NSShadow對象
NSTextEffectAttributeName; //文字效果痒谴,value是NSString
NSAttachmentAttributeName;//附屬衰伯,value是NSTextAttachment 對象
NSLinkAttributeName;//鏈接,value是NSURL or NSString
NSBaselineOffsetAttributeName;//基礎(chǔ)偏移量积蔚,value是NSNumber對象
NSUnderlineColorAttributeName;//下劃線顏色意鲸,value是UIColor對象
NSStrikethroughColorAttributeName;//刪除線顏色,value是UIColor
NSObliquenessAttributeName; //字體傾斜
NSExpansionAttributeName; //字體扁平化
NSVerticalGlyphFormAttributeName;//垂直或者水平尽爆,value是 NSNumber怎顾,0表示水平,1垂直
字體漱贱,顏色槐雾,背景色
涉及到的屬性
- NSFontAttributeName
- NSForegroundColorAttributeName
- NSBackgroundColorAttributeName
效果
代碼
UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
[self.view addSubview:Label];
NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
NSDictionary * attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSBackgroundColorAttributeName:[UIColor grayColor],NSFontAttributeName:[UIFont boldSystemFontOfSize:14]};
[mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
Label.attributedText = mutableAttriStr;
下劃線
涉及到的兩個屬性
NSUnderlineStyleAttributeName
NSUnderlineColorAttributeName
效果
代碼
UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
[self.view addSubview:Label];
NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
NSDictionary * attris = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName:[UIColor redColor], NSUnderlineStyleAttributeName:@(NSUnderlineStyleSingle), NSUnderlineColorAttributeName:[UIColor blueColor],};
[mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
Label.attributedText = mutableAttriStr;
描邊
涉及到的兩個屬性
NSStrokeColorAttributeName
NSStrokeWidthAttributeName
效果
代碼
UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
[self.view addSubview:Label];
NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
NSDictionary * attris = @{NSForegroundColorAttributeName:[UIColor whiteColor],NSStrokeColorAttributeName:[UIColor greenColor],NSStrokeWidthAttributeName:@(2)};
[mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
Label.attributedText = mutableAttriStr;
附屬屬性(例如圖片)
涉及到的屬性
NSAttachmentAttributeName
效果
代碼
UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
[self.view addSubview:Label];
//創(chuàng)建Attachment Str
NSTextAttachment * attach = [[NSTextAttachment alloc] init];
attach.image = [UIImage imageNamed:@"memoAccess"];
attach.bounds = CGRectMake(0, 0, 20, 20);
NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:attach];
//添加
NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
[mutableAttriStr appendAttributedString:imageStr];
Label.attributedText = mutableAttriStr;
繪制風(fēng)格
為什么要紅字?因?yàn)檫@個屬性真的很重要幅狮。
涉及到的屬性
NSMutableParagraphStyle
效果(看起來很奇怪募强,僅僅為了展示某些功能)
代碼
@interface TestView : UIView
@end
@implementation TestView
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (!self) {
return nil;
}
self.opaque = NO;
return self;
}
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
NSMutableAttributedString * attributeStr = [[NSMutableAttributedString alloc] initWithString:@"The anthor of this blog is wenchenhuang"];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentRight;
paragraphStyle.headIndent = 4.0;
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
paragraphStyle.lineSpacing = 2.0;
NSDictionary * attributes = @{NSParagraphStyleAttributeName:paragraphStyle};
[attributeStr setAttributes:attributes range:NSMakeRange(0, attributeStr.length)];
[attributeStr drawInRect:self.bounds];
}
TestView *test = [[TestView alloc] initWithFrame:CGRectMake(100, 100,100, 60)];
[self.view addSubview:test];
陰影
涉及到的屬性
NSShadowAttributeName
效果
UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
[self.view addSubview:Label];
NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blueColor];
shadow.shadowBlurRadius = 2.0;
shadow.shadowOffset = CGSizeMake(1.0, 1.0);
NSDictionary * attris = @{NSShadowAttributeName:shadow};
[mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
Label.attributedText = mutableAttriStr;
文字效果
相關(guān)屬性
NSTextEffectAttributeName
代碼
UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
[self.view addSubview:Label];
NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
NSDictionary * attris = @{NSTextEffectAttributeName:NSTextEffectLetterpressStyle};
[mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
Label.attributedText = mutableAttriStr;
鏈接
相關(guān)屬性
NSLinkAttributeName
例子
點(diǎn)擊打開鏈接
@interface ViewController ()<UITextViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
textView.scrollEnabled = NO;
textView.editable = NO;
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
textView.delegate = self;
[self.view addSubview:textView];
NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
NSDictionary * attris = @{NSLinkAttributeName:[NSURL URLWithString:@"http://www.baidu.com"]};
[mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
textView.attributedText = mutableAttriStr;
// Do any additional setup after loading the view, typically from a nib.
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange
{
return YES;
}
文字連體
涉及到的屬性
NSLigatureAttributeName
舉例
NSLigatureAttributeName 屬性位@(0) 和@(1)
字符間隔
相關(guān)屬性
NSKernAttributeName
舉例 字符間距拉大到4
對比下,默認(rèn)的
UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 120)];
[self.view addSubview:Label];
NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Wenchen"];
NSDictionary * attris = @{NSKernAttributeName:@(4),
NSFontAttributeName:[UIFont systemFontOfSize:30]};
[mutableAttriStr setAttributes:attris range:NSMakeRange(0,mutableAttriStr.length)];
Label.attributedText = mutableAttriStr;
baseline基礎(chǔ)偏移量
相關(guān)屬性
- NSBaselineOffsetAttributeName
效果
對比下偏移量為0 和20
代碼
NSDictionary * attris = @{NSBaselineOffsetAttributeName:@(0),
NSFontAttributeName:[UIFont systemFontOfSize:30]};
字體傾斜
相關(guān)屬性
NSObliquenessAttributeName
效果
代碼
NSDictionary * attris = @{NSObliquenessAttributeName:@(0.5),
NSFontAttributeName:[UIFont systemFontOfSize:30]};```
字體扁平化
相關(guān)屬性
NSExpansionAttributeName
效果
![](http://upload-images.jianshu.io/upload_images/3065850-aa885bf9c6346803.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
代碼
NSDictionary * attris = @{NSExpansionAttributeName:@(1.0),
NSFontAttributeName:[UIFont systemFontOfSize:30]};