UISegmentedControl 的 NSAttributedstring

NSAttributed string for UISegmentedControl

我正在尝试为我的 UISegmented 控件设置自定义间距。如何定义 NSKernAttributeNamevalue 属性来定义字母之间的自定义间距?

[myUIControl setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName: [UIFont fontWithName:myFont size:13]} forState:UIControlStateSelected];

这是 UIButton

的处理方式
[attributedString addAttribute:NSKernAttributeName
                         value:@(dinSpacing)
                         range:NSMakeRange(0, [myString length])];

您可以在下面的代码中设置段控件的字符间距。你做的是正确的,例如,只需将 NSKernAttributeName 添加为带有你为按钮所做的数字值的键。

[self.aSegMentCntrl setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:@(5),NSKernAttributeName,[UIColor redColor],NSForegroundColorAttributeName,[UIFont fontWithName:@"Helvetica Neue" size:13],NSFontAttributeName ,nil] forState:UIControlStateSelected];

您的词典版本

[self.aSegMentCntrl setTitleTextAttributes:@{ NSKernAttributeName:@(5),NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont fontWithName:@"Helvetica Neue" size:13]} forState:UIControlStateSelected];