iOS: как установить 0,5 пробела в строке при отображении UILabel? - PullRequest
0 голосов
/ 22 октября 2018

Мне нужно 0,5 пробела, показывая UILabel.

Как установить заголовок кнопки, как показано ниже:

enter image description here

Вот что я достиг:

5

Существует небольшая разница в расположении двух кнопок. Вот мой код:

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 места.

Есть ли способ достичь этого?

1 Ответ

0 голосов
/ 22 октября 2018

С 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];
...