0

I need 0.5 space, showing a UILabel.

Like to set a button's title as the following image:

enter image description here

Here is what I achieved:

5

There is a small difference between the two button's layout. Here is my code:

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

There is 3 space in the text. I need 2.5 space.

Is there anyway to achieve it?

Dimple
  • 788
  • 1
  • 11
  • 27
dengST30
  • 3,643
  • 24
  • 25

1 Answers1

0

With NSKernAttributeName, Can control more precisely.

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];
dengST30
  • 3,643
  • 24
  • 25