原创Blog,转载请注明出处
blog.csdn.net/hello_hwc
欢迎关注我的iOS SDK详解专栏
http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html
前言:这篇文章算是整理吧,因为最近逛Stackoverflow的时候,发现自己这部分掌握的不是很好。
绘制不同颜色不同字体的一个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;
NSFontAttributeName; //字体,value是UIFont对象
NSParagraphStyleAttributeName;//绘图的风格(居中,换行模式,间距等诸多风格),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;//基础偏移量,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;
涉及到的两个属性
代码
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;
涉及到的两个属性
代码
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;
涉及到的属性
UILabel * Label = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 60)];
[self.view addSubview:Label];
//创建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;
为什么要红字?因为这个属性真的很重要。
涉及到的属性
@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];
涉及到的属性
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;
相关属性
代码
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;
相关属性
例子
点击打开链接
@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 属性位@(0) 和@(1)
相关属性
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;
相关属性
- NSBaselineOffsetAttributeName
代码
NSDictionary * attris = @{NSBaselineOffsetAttributeName:@(0),
NSFontAttributeName:[UIFont systemFontOfSize:30]};
相关属性
代码
NSDictionary * attris = @{NSObliquenessAttributeName:@(0.5),
NSFontAttributeName:[UIFont systemFontOfSize:30]};
相关属性
代码
NSDictionary * attris = @{NSExpansionAttributeName:@(1.0),
NSFontAttributeName:[UIFont systemFontOfSize:30]};