#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UILabel *lable;
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 300, 200)];
self.lable.numberOfLines = 0;
_lable.text = [NSString stringWithFormat:@"丁凯乐是《快乐星球1》[1-2] 主角。在《快乐星球2》中客串。丁凯乐(小名乐乐)是现实生活中阳光小学四年级的学生,像中国大多数10岁左右的孩子一样"];
_lable.textAlignment = NSTextAlignmentLeft;
_lable.font = [UIFont systemFontOfSize:14];
[self.view addSubview:_lable];
//设置行间距为9
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:9];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:_lable.text];
[attributedString addAttributes:@{NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(0, [_lable.text length])];
_lable.attributedText = attributedString;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(20, 500, 100, 50);
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
// 注:lable上的文字发生改变,必须重新设置行间距,否则改变后的文字的行间距依然没有变
-(void)click
{
self.lable.text = @"丁凯乐是《快乐星球1》[1-2] 主角。在《快乐星球2》中客串。丁凯乐(小名乐乐)是现实生活中阳光小学四年级的学生,像中国大多数10岁左右的孩子一样,他的生活平静、普通而且按部就班。但是由于他的性格内向、胆小怕事,在学校他经常被班里几个调皮的同学金刚、胖哥和小九九欺负,乐乐惹不起他们,只好忍气吞声。";
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:9];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:_lable.text];
[attributedString addAttributes:@{NSParagraphStyleAttributeName:paragraphStyle} range:NSMakeRange(0, [_lable.text length])];
_lable.attributedText = attributedString;
}