iOS中文本属性Attributes的用法
来源:互联网 发布:叶诗文 兴奋剂 知乎 编辑:程序博客网 时间:2024/06/08 00:43
iOS中用到文本属性Attributes的地方还是很多的,这里对文本属性做一些归纳,以免记不住用到的时候到处找资料:
如下是所有的文本属性:
NSFontAttributeName //设置字体大小NSParagraphStyleAttributeName //设置段落格式NSForegroundColorAttributeName //设置字体的颜色NSBackgroundColorAttributeName //设置背景的颜色NSLigatureAttributeName //设置连体字符NSKernAttributeName //设置文字之间的距离NSStrikethroughStyleAttributeName //设置删除线的样式NSUnderlineStyleAttributeName //设置下划线的格式NSStrikethroughColorAttributeName //设置删除线的颜色NSStrokeColorAttributeName //设置中空效果的填充颜色NSStrokeWidthAttributeName //设置中空效果的宽度NSShadowAttributeName //设置阴影效果NSTextEffectAttributeName //设置文本的特殊效果NSAttachmentAttributeName //设置文本附件NSLinkAttributeName //设置超链接NSBaselineOffsetAttributeName //设置基线偏移值NSUnderlineColorAttributeName //设置下划线的颜色NSObliquenessAttributeName //设置字体倾斜NSExpansionAttributeName //设置文本扁平化(横向拉伸)NSWritingDirectionAttributeName //设置文字的书写方向NSVerticalGlyphFormAttributeName //设置文字的排版方向
下面是详细用法
-1. NSFontAttributeName //设置字体大小
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];NSString *myString = @"这是一串测试用的字符串xxx";NSDictionary *myDic = @{ NSFontAttributeName:[UIFont boldSystemFontOfSize:20], };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-2. NSParagraphStyleAttributeName //设置段落格式
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];lable.numberOfLines = 0;lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串xxx";//段落样式NSMutableParagraphStyle *myStyle = [[NSMutableParagraphStyle alloc]init];//行间距myStyle.lineSpacing = 10;//段落间距myStyle.paragraphSpacing = 20;//对齐方式myStyle.alignment = NSTextAlignmentLeft;//指定段落开始的缩进像素myStyle.firstLineHeadIndent = 20;//调整全部文字的缩进像素myStyle.headIndent = 20;NSDictionary *myDic = @{ NSParagraphStyleAttributeName:myStyle, };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-3.NSForegroundColorAttributeName //设置字体的颜色
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.numberOfLines = 0;lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串xxx";NSDictionary *myDic = @{ NSForegroundColorAttributeName:[UIColor redColor], };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-4.NSBackgroundColorAttributeName //设置背景的颜色
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.numberOfLines = 0;lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串xxx";NSDictionary *myDic = @{ NSBackgroundColorAttributeName:[UIColor redColor], };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-5.NSLigatureAttributeName //设置连体字符
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.numberOfLines = 0;lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";//0 表示没有连体字符。1 表示使用默认的连体字符//这个设置了感觉没有什么变化,如果设置字体为futura后可以看到连体的效果,但这是设置字体后改变的NSDictionary *myDic = @{ NSLigatureAttributeName:@1,// NSFontAttributeName: [UIFont fontWithName: @"futura" size: 18], };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-6.NSKernAttributeName //设置文字之间的距离
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.numberOfLines = 0;lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";NSDictionary *myDic = @{ NSKernAttributeName:@20, };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-7.NSStrikethroughStyleAttributeName //设置删除线的样式
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";//在枚举NSUnderlineStyle中取值NSDictionary *myDic = @{ NSStrikethroughStyleAttributeName:@(NSUnderlineStyleDouble), };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-8.NSUnderlineStyleAttributeName //设置下划线的格式
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";//在枚举NSUnderlineStyle中取值NSDictionary *myDic = @{ NSUnderlineStyleAttributeName:@(NSUnderlineStyleDouble), };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-9.NSStrikethroughColorAttributeName //设置删除线的颜色
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";//在枚举NSUnderlineStyle中取值NSDictionary *myDic = @{ NSStrikethroughStyleAttributeName:@(NSUnderlineStyleSingle), NSStrikethroughColorAttributeName:[UIColor redColor], };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-10.NSStrokeWidthAttributeName //设置中空效果以及效果的宽度
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";NSDictionary *myDic = @{ NSStrokeWidthAttributeName:@2, };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-11.NSStrokeColorAttributeName //设置中空效果的填充颜色
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";NSDictionary *myDic = @{ NSStrokeWidthAttributeName:@2, NSStrokeColorAttributeName:[UIColor redColor], };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-12.NSShadowAttributeName //设置阴影效果
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";NSShadow *shadow = [[NSShadow alloc]init];shadow.shadowColor = [UIColor redColor];shadow.shadowBlurRadius = 1.0f;shadow.shadowOffset = CGSizeMake(1, 1);NSDictionary *myDic = @{ NSShadowAttributeName:shadow, };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-13.NSTextEffectAttributeName //设置文本的特殊效果
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";//只有一种印刷效果NSDictionary *myDic = @{ NSTextEffectAttributeName:NSTextEffectLetterpressStyle, };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-14.NSAttachmentAttributeName //设置文本附件
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.backgroundColor = [UIColor lightGrayColor];NSMutableAttributedString *myString = [[NSMutableAttributedString alloc] initWithString:@"这是一串测试用的字符串fhfkfbfjflfi"];NSTextAttachment *textAttachment = [[NSTextAttachment alloc]init];textAttachment.image = [UIImage imageNamed:@"dog"];textAttachment.bounds = CGRectMake(0, 0, 30, 30);NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:textAttachment];[myString appendAttributedString:imageStr]; lable.attributedText = myString;[self addSubview:lable];
显示效果:
-15.NSLinkAttributeName //设置超链接
//在 UILabel 和 UITextField 中是无法使用超链接的UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];myTextView.delegate = self;myTextView.editable = NO; //不设置是不能点击的myTextView.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";NSDictionary *myDic = @{ NSLinkAttributeName:[NSURL URLWithString:@"https://www.baidu.com"], };myTextView.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:myTextView];
记得遵循协议
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction { NSLog(@"点击了"); return YES;}
显示效果:
-16.NSBaselineOffsetAttributeName //设置基线偏移值
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";//正数上偏,负数下偏NSDictionary *myDic = @{ NSBaselineOffsetAttributeName:@10, };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-17.NSUnderlineColorAttributeName //设置下划线的颜色
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";NSDictionary *myDic = @{ NSUnderlineStyleAttributeName:@(NSUnderlineStyleDouble), NSUnderlineColorAttributeName:[UIColor redColor], };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-18.NSObliquenessAttributeName //设置字体倾斜
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";NSDictionary *myDic = @{ NSObliquenessAttributeName:@.5f, };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-19.NSExpansionAttributeName //设置文本扁平化(横向拉伸)
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";NSDictionary *myDic = @{ NSExpansionAttributeName:@1, };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-20.NSWritingDirectionAttributeName //设置文字的书写方向
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";//NSWritingDirectionAttributeName 设置文字的书写方向,取值为以下组合/* @[@(NSWritingDirectionLeftToRight | NSWritingDirectionEmbedding)] @[@(NSWritingDirectionLeftToRight | NSWritingDirectionOverride)] @[@(NSWritingDirectionRightToLeft | NSWritingDirectionEmbedding)] @[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)] */NSDictionary *myDic = @{ NSWritingDirectionAttributeName:@[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)], };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
显示效果:
-21.NSVerticalGlyphFormAttributeName //设置文字的排版方向
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 40)];lable.backgroundColor = [UIColor lightGrayColor];NSString *myString = @"这是一串测试用的字符串fhfkfbfjflfi";//0表示横排文本,1表示竖排文本 在iOS中只支持0NSDictionary *myDic = @{ NSVerticalGlyphFormAttributeName:@1, };lable.attributedText = [[NSAttributedString alloc] initWithString:myString attributes:myDic];[self addSubview:lable];
最后这个没有什么特别效果就不贴图了
- iOS中文本属性Attributes的用法
- 文本属性Attributes的用法
- 简述文本属性Attributes的用法
- 一篇关于ios文本属性Attributes的用法很详细的文章
- 给iOS开发新手送点福利,简述文本属性Attributes的用法
- 给iOS开发新手送点福利,简述文本属性Attributes的用法
- 给iOS开发新手送点福利,简述文本属性Attributes的用法
- iOS 文字属性Attributes
- iOS- 详解文本属性Attributes
- iOS- 详解文本属性Attributes
- iOS- 详解文本属性Attributes
- iOS- 详解文本属性Attributes
- iOS- 详解文本属性Attributes
- iOS- 详解文本属性Attributes
- iOS- 详解文本属性Attributes
- iOS- 详解文本属性Attributes
- iOS- 详解文本属性Attributes
- iOS- 详解文本属性Attributes
- IntelliJ IDEA 设置 编辑器字体大小背景色及快捷键
- C++Primer第五版 第六章习题答案(21~30)
- drupal8 入门
- 洛谷 P1048 采药
- JVM内存调优之GC算法
- iOS中文本属性Attributes的用法
- STL容器用法详解
- 算法训练 字符串变换
- MOS管防反接用法
- tomcat7源码(一) 导入eclipse
- 快速排序
- web开发的笔记
- 没那么难,不信你试试
- redis使用Windows版本