内容简介:写在前面在实际开发中,Label的默认行间距大小一般都是满足不了UI设计师设计的行间距大小的。于是乎,就需要我们开发人员手动调整Label的行间距大小。先上效果图:
写在前面
在实际开发中,Label的默认行间距大小一般都是满足不了UI设计师设计的行间距大小的。于是乎,就需要我们开发人员手动调整Label的行间距大小。
然而,UILabel并没有提供直接修改行间距大小的属性
但是我们可以用Label的attributedText属性来设置
先上效果图:
默认Label效果
行间距效果
话不多说上代码:
NSString *str = @"在实际开发中,Label的默认行间距大小一般都是满足不了UI设计师设计的行间距大小的。于是乎,就需要我们开发人员手动调整Label的行间距大小。然而,UILabel并没有提供直接修改行间距大小的属性,但是我们可以用Label的attributedText属性来设置. 在实际开发中,Label的默认行间距大小一般都是满足不了UI设计师设计的行间距大小的。于是乎,就需要我们开发人员手动调整Label的行间距大小。然而,UILabel并没有提供直接修改行间距大小的属性,但是我们可以用Label的attributedText属性来设置"; NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:str]; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; paragraphStyle.lineSpacing = 8.0; // 设置行间距 paragraphStyle.alignment = NSTextAlignmentJustified; //设置两端对齐显示 [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedStr.length)]; UILabel *lineSpaceLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, [UIScreen mainScreen].bounds.size.width - 40, 0)]; lineSpaceLabel.backgroundColor = [UIColor cyanColor]; lineSpaceLabel.numberOfLines = 0; lineSpaceLabel.attributedText = attributedStr; [lineSpaceLabel sizeToFit]; [self.view addSubview:lineSpaceLabel];
UITextView一样可以设置的哟
更新:我刚发布了文章就有人私信问怎么改变字间距 (每行字与字之间的间距)
其实很简单哟
加上这句代码就可以了
[attributedStr addAttribute:NSKernAttributeName value:@(10) range:NSMakeRange(0, attributedStr.length)];
效果如图:
作者:陈_Chen
链接:https://www.jianshu.com/p/f4924b70a57e
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
The Algorithm Design Manual
Steven S Skiena / Springer / 2011-11-14 / GBP 55.07
....The most comprehensive guide to designing practical and efficient algorithms.... Written by a well-known algorithms researcher who received the IEEE Computer Science and Engineering Teaching Aw......一起来看看 《The Algorithm Design Manual》 这本书的介绍吧!