広告
UILabel を使って複数行の文字列を表示する際の行間を調整する場合は NSAttributedString を使って一行の高さを指定して調整します。
NSMutableParagraphStyle のインスタンスに setMinimumLineHeight と setMaximumLineHeight で行の高さを指定し、それを元に NSAttributedString を生成して UILabel に指定します。
広告
行の高さ16 サンプル
- (void)sampleUILabelSetAttributedTextLineHeight16 { CGFloat lineHeight = 16.0 ; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init] ; [paragraphStyle setMinimumLineHeight:lineHeight] ; [paragraphStyle setMaximumLineHeight:lineHeight] ; NSString *string = @"Sample Specific Line Height Text 16.0 Sample Specific Line Height Text 16.0 Sample Specific Line Height Text 16.0" ; NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:paragraphStyle,NSParagraphStyleAttributeName,nil] ; NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:string attributes:attributes] ; UILabel *label = [[UILabel alloc] initWithFrame:viewFrame] ; [label setNumberOfLines:3] ; [label setAttributedText:attributedText] ; [self.view addSubview:label] ; }
行の高さ16 表示結果

行の高さ24 サンプル
- (void)sampleUILabelSetAttributedTextLineHeight24 { CGFloat lineHeight = 24.0 ; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init] ; [paragraphStyle setMinimumLineHeight:lineHeight] ; [paragraphStyle setMaximumLineHeight:lineHeight] ; NSString *string = @"Sample Specific Line Height Text 24.0 Sample Specific Line Height Text 24.0 Sample Specific Line Height Text 24.0" ; NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:paragraphStyle,NSParagraphStyleAttributeName,nil] ; NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:string attributes:attributes] ; UILabel *label = [[UILabel alloc] initWithFrame:viewFrame] ; [label setNumberOfLines:3] ; [label setAttributedText:attributedText] ; [self.view addSubview:label] ; }
行の高さ24 表示結果

広告
広告