標籤:value contents 也會 table 高度 white .text system height
NSString *contentStr = @"總以為,在最初的地方,有一個最原來的我,就也會有一個最原來的你"; UILabel *tempLabel = [[UILabel alloc] init]; //設定背景顏色 tempLabel.backgroundColor = [UIColor redColor]; //設定內容 tempLabel.text = contentStr; //設定字型顏色 tempLabel.textColor = [UIColor whiteColor]; //設定字型大小 tempLabel.font = [UIFont systemFontOfSize:15.0]; //設定文本是否多行顯示 tempLabel.numberOfLines = 0; tempLabel.lineBreakMode = NSLineBreakByWordWrapping; //設定UILabel根據文字自適應高度 CGSize size = [tempLabel sizeThatFits:CGSizeMake(100, MAXFLOAT)]; //設定frame tempLabel.frame = CGRectMake(50, 100, 100, size.height); [self.view addSubview:tempLabel]; //設定label的行間距 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentStr]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; [paragraphStyle setLineSpacing:8]; [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [contentStr length])]; [tempLabel setAttributedText:attributedString]; [tempLabel sizeToFit];
iOS 設定UILabel的行間距並自適應高度