2017-04-19 87 views
2

有沒有辦法在UILabel內設置兩條線的距離? 我試圖在Interface Builder中做,但沒有成功。目標C標籤行距?

+0

你可以通過'NSAttributedString'和'NSParagraphStyle'來設置它的'lineSpacing'。 – Larme

回答

3

你想要的代碼將是這樣的:

NSMutableAttributedString* attrString = [[NSMutableAttributedString alloc] initWithString:@"Sample text"]; 
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; 
[style setLineSpacing:24]; 
[attrString addAttribute:NSParagraphStyleAttributeName 
    value:style 
    range:NSMakeRange(0, strLength)]; 
uiLabel.attributedText = attrString; 
0

由於iOS 6中,蘋果加入NSAttributedString到的UIKit,從而能夠使用NSParagraphStyle改變行距。

或者,您可以通過使用Attributed Text的Storyboard進行此操作,然後單擊...符號。請參閱下面的鏈接截圖。

https://i.stack.imgur.com/aiNfR.png

1

可以使用NSAttributedString一個UILabel中的兩條線之間添加間距:

NSString *labelText = @"My String"; 
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText]; 
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 
[paragraphStyle setLineSpacing:20]; 
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])]; 
cell.label.attributedText = attributedString ; 

OR

如果您使用的故事板,那麼你可以通過控制情節板中的行距選擇文本類型歸屬並添加間距值: