我正在开发一个 iPhone 应用程序,我想在 UILabel 中设置字距调整。我写的代码(可能围绕kCTKernAttributeName
)似乎有错误。我该如何解决这个问题?
NSMutableAttributedString *attStr;
NSString *str = @"aaaaaaa";
CFStringRef kern = kCTKernAttributeName;
NSNumber *num = [NSNumber numberWithFloat: 2.0f];
NSDictionary *attributesDict = [NSDictionary dictionaryWithObject:num
forKey:(NSString*)kern];
[attStr initWithString:str attributes:attributesDict];
CGRect frame1 = CGRectMake(0, 0, 100, 40);
UILabel *label1 = [[UILabel alloc] initWithFrame:frame1];
label1.text = attStr
[self.view addSubview:label1];
老问题,但你现在就可以(很容易)做到。
NSMutableAttributedString *attributedString;
attributedString = [[NSMutableAttributedString alloc] initWithString:@"Please get wider"];
[attributedString addAttribute:NSKernAttributeName value:@5 range:NSMakeRange(10, 5)];
[self.label setAttributedText:attributedString];
2013 年 11 月,为了扩展这个伟大的答案,这里有一些完全典型的代码。通常你也会设置字体。请注意评论中使用普通旧 .text 的老式方式。希望它可以帮助某人
NSString *yourText = @"whatever";
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];
// simple approach with no tracking...
// label.text = yourText;
// [label setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:24]];
NSMutableAttributedString *attributedString;
attributedString = [[NSMutableAttributedString alloc] initWithString:yourText];
[attributedString addAttribute:NSKernAttributeName
value:[NSNumber numberWithFloat:2.0]
range:NSMakeRange(0, [yourText length])];
[attributedString addAttribute:NSFontAttributeName
value:[UIFont fontWithName:@"HelveticaNeue-Light" size:24]
range:NSMakeRange(0, [yourText length])];
label.attributedText = attributedString;
label.textColor = [UIColor blackColor];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentCenter;
[label sizeToFit];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)