#import "ViewController.h"ide
@interface ViewController ()atom
@property (nonatomic, strong) UILabel *lable;spa
@end.net
@implementation ViewControllerorm
- (void)viewDidLoad {ci
[super viewDidLoad];get
// Do any additional setup after loading the view, typically from a nib.string
self.lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 300, 200)];it
self.lable.numberOfLines = 0;io
_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;
}