开发中有时候会碰到字体间间距稍大的情况,如果是已知的文字,有比较少我们可以加个空格解决下,但是当文字数量比较多,或者文字未知的情况,我们要怎么调整文字之间的间距呢?这里提供一种方法,调整value的数值即可调整文字间距:
UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 100, 100, 30)]; [self.view addSubview:titleLabel]; NSString *title=@"账户名:"; NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString:title]; [attrStr addAttribute:NSKernAttributeName value:@(9.0) range:NSMakeRange(0, attrStr.length-2)]; titleLabel.attributedText = attrStr; UILabel *subLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 150, 120, 30)]; [self.view addSubview:subLabel]; NSString *subTitle=@"密码:"; NSMutableAttributedString* subAttrStr = [[NSMutableAttributedString alloc] initWithString:subTitle]; [subAttrStr addAttribute:NSKernAttributeName value:@(35.0) range:NSMakeRange(0, subTitle.length-2)]; subLabel.attributedText = subAttrStr;
工程文件在我的github中:https://github.com/codeliu6572/Label-space.git