我需要0.5个空格,显示一个UILabel
。
就像将按钮的标题设置为下图所示:
这是我实现的目标:
两个按钮的布局之间存在很小的差异。 这是我的代码:
UIButton * tmpBtn = [[UIButton alloc] initWithFrame: CGRectMake(200, 200, 84, 26)];
[tmpBtn setBackgroundImage: [UIImage imageNamed: @"btnBgImage"] forState: UIControlStateNormal];
[tmpBtn setTitle: @"1 跳过" forState: UIControlStateNormal];
// there is 3 space
tmpBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
tmpBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
[self.view addSubview: tmpBtn];
文本中有3个空格。我需要2.5个空间。
反正有实现目标吗?
答案 0 :(得分:0)
使用NSKernAttributeName
,可以进行更精确的控制。
UIButton * tmpBtn = [[UIButton alloc] initWithFrame: CGRectMake(200, 200, 84, 26)];
[tmpBtn setBackgroundImage: [UIImage imageNamed: @"btnBgImage"] forState: UIControlStateNormal];
tmpBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
tmpBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 8, 0, 0);
NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString: @"1 跳过"];
// 3 spaces
[str setAttributes: @{NSKernAttributeName: @(-1)} range: NSMakeRange(2, 2)];
[tmpBtn setAttributedTitle: str forState: UIControlStateNormal];
[self.view addSubview: tmpBtn];