标签:
//
// ViewController.m
// Label富文本
//
// Created by 大欢 on 16/1/19.
// Copyright © 2016年 bjsxt. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.lineSpacing = 10;
paragraph.paragraphSpacing = 50;
paragraph.firstLineHeadIndent = 50;
NSString * exampleString = @"根据名人慈善赛公布的名单,Drake带领的加拿大明星队将包括:NBA两届得分王麦蒂、前湖人队三冠王成员里克·福克斯、网球运动员米洛斯·劳尼克以及演员兼歌手吴亦凡。\n凯文·哈特领衔的美国明星队成员将包括:昌西·比卢普斯、“小虫”博格斯、著名主持人尼克·坎农、安东尼·安德森。";
NSDictionary * dictA = @{NSFontAttributeName:[UIFont systemFontOfSize:20],
NSForegroundColorAttributeName:[UIColor greenColor],
NSBackgroundColorAttributeName:[UIColor grayColor],
NSParagraphStyleAttributeName:paragraph
// NSObliquenessAttributeName:@0.5 //斜体
// NSStrokeColorAttributeName:[UIColor whiteColor],
// NSStrokeWidthAttributeName:@2,//描边
// NSKernAttributeName:@20,//字间距
// NSStrikethroughStyleAttributeName:@2,//删除线
// NSUnderlineStyleAttributeName:@1,//下划线
};
NSAttributedString * attribute = [[NSAttributedString alloc] initWithString:exampleString attributes:dictA];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, CGRectGetWidth(self.view.bounds) - 40, 500)];
label.attributedText = attribute;
label.backgroundColor = [UIColor blueColor];
label.numberOfLines = 0;
[self.view addSubview:label];
}
@end
/****************************************************************/
/****************************************************************/
//
// ViewController.m
// Label富文本2
//
// Created by ma c on 16/1/19.
// Copyright © 2016年 bjsxt. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// [self example1];
// [self example2];
[self example4];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)example2 {
NSString * string = @"@大欢,出来玩啊!";
NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc]initWithString:string];
NSRange range1 = {0, 3};
NSDictionary *dict1 = @{
NSForegroundColorAttributeName:[UIColor blueColor],
NSFontAttributeName:[UIFont systemFontOfSize:25],
};
[attributeString addAttributes:dict1 range:range1];
NSDictionary *dict2 = @{
NSForegroundColorAttributeName:[UIColor greenColor],
NSFontAttributeName:[UIFont systemFontOfSize:22],
};
NSAttributedString * string2 = [[NSAttributedString alloc] initWithString:@"好哒!" attributes:dict2];
[attributeString appendAttributedString:string2];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 100, CGRectGetWidth(self.view.frame)-20, 30)];
label.attributedText = attributeString;
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor yellowColor];
[self.view addSubview:label];
}
/*
使用方法:
为某一范围内文字设置多个属性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
为某一范围内文字添加某个属性
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
为某一范围内文字添加多个属性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
移除某范围内的某个属性
- (void)removeAttribute:(NSString *)name range:(NSRange)range;
常见的属性及说明
NSFontAttributeName 字体
NSParagraphStyleAttributeName 段落格式
NSForegroundColorAttributeName 字体颜色
NSBackgroundColorAttributeName 背景颜色
NSStrikethroughStyleAttributeName 删除线格式
NSUnderlineStyleAttributeName 下划线格式
NSStrokeColorAttributeName 删除线颜色
NSStrokeWidthAttributeName 删除线宽度
NSShadowAttributeName 阴影
*/
- (void)example1 {
NSString * exampleString = @"乔布斯---apple";
NSMutableAttributedString * mutableAttribute = [[NSMutableAttributedString alloc]initWithString:exampleString];
NSRange range1 = {0, 3};
NSRange range2 = {6, 5};
[mutableAttribute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50] range:range1];
NSDictionary * dict = @{
NSForegroundColorAttributeName:[UIColor magentaColor],
NSBackgroundColorAttributeName:[UIColor cyanColor]
};
[mutableAttribute addAttributes:dict range:range2];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 30, self.view.frame.size.width-20, 60)];
label.attributedText = mutableAttribute;
label.backgroundColor = [UIColor grayColor];
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
}
- (void)example3 {
NSString * string = @"一二三四五";
NSMutableAttributedString * mutableAttibute = [[NSMutableAttributedString alloc]initWithString:string];
NSRange range1 = NSMakeRange(0, 3);
[mutableAttibute addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:23] range:range1];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, CGRectGetWidth(self.view.frame), 50)];
label.textColor = [UIColor redColor];
label.textAlignment = NSTextAlignmentCenter;
label.attributedText = mutableAttibute;
[self.view addSubview:label];
}
- (void)example4 {
NSString * string = @"还记得你说家是唯一的城堡随着稻香河流继续奔跑微微笑 小时候的梦我知道不要哭让萤火虫带著你逃跑乡间的歌谣永远的依靠回家吧 回到最初的美好";
NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle alloc]init];
paragraph.lineSpacing = 4;
paragraph.paragraphSpacing = 7;
paragraph.firstLineHeadIndent = 25;
NSDictionary * dict = @{
NSFontAttributeName:[UIFont systemFontOfSize:20],
NSParagraphStyleAttributeName:paragraph,
};
NSMutableAttributedString * mutableAttribute = [[NSMutableAttributedString alloc]initWithString:string attributes:dict];
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, CGRectGetWidth(self.view.frame)-20, 0)];
label.attributedText = mutableAttribute;
label.numberOfLines = 0;
[label sizeToFit];
[self.view addSubview:label];
}
@end
标签:
原文地址:http://www.cnblogs.com/MrWuYindi/p/5146572.html