由于方法多種多樣,這里介紹筆者常用的一種設(shè)置方法,比較簡單格式較固定,故閑話不多說,直接上代碼:
UILabel *headerLabel = [UILabel new];
headerLabel.font = [UIFont systemFontOfSize:15];
NSString *titleContent = @"月光如流水一般,靜靜地瀉在這一片葉子和花上。薄薄的青霧浮起在荷塘里。葉子和花仿佛在牛乳中洗過一樣;又像籠著輕紗的夢。雖然是滿月,天上卻有一層淡淡的云,所以不能朗照;但我以為這恰是到了好處——酣眠固不可少,小睡也別有風(fēng)味的。月光是隔了樹照過來的,高處叢生的灌木,落下參差的斑駁的黑影,峭楞楞如鬼一般;彎彎的楊柳的稀疏的倩影,卻又像是畫在荷葉上。塘中的月色并不均勻;但光與影有著和諧的旋律,如梵婀玲上奏著的名曲。";
headerLabel.text = titleContent;
headerLabel.numberOfLines = 0;//多行顯示,計算高度
CGSize titleSize = [titleContent boundingRectWithSize:CGSizeMake(kScreenWidth * 0.8, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13]} context:nil].size;
// 調(diào)整行間距
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:titleContent];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:10];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [titleContent length])];
headerLabel.attributedText = attributedString;
[headerLabel sizeToFit];
[headerLabel setFrame:CGRectMake((kScreenWidth - titleSize.width) * 0.5,(kScreenHeight - titleSize.height) * 0.5 , titleSize.width, titleSize.height)];
[self.view addSubview:headerLabel];