Objective C label line spacing?

别等时光非礼了梦想. 提交于 2019-12-05 01:34:06

问题


Is there a way to set the distance of two lines within a UILabel? I tried to do it within Interface Builder but without success.


回答1:


The code you want will be something like this:

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;



回答2:


You can use NSAttributedString to add spacing between two lines within a 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

If you are using storyboard then you can control line spacing in the storyboard by selecting text type is attributed and add spacing value:




回答3:


Since iOS 6, Apple added NSAttributedString to UIKit, making it possible to use NSParagraphStyle to change the line spacing.

Alternatively, you can do this via Storyboards using Attributed Text and then clicking the ... symbol. See link below for screenshot.

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



来源:https://stackoverflow.com/questions/43498344/objective-c-label-line-spacing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!