我在iOS 6上的UITextView的attributedText属性上,在特定范围内,将NSKernAttributeName设置为不同的浮点值。每次使用attributedText的enumerateAttribute选项块方法检索值时,Kerning都设置为0。代码如下。
检索
NSAttributedString *currentText = _textView.attributedText;
[currentText enumerateAttribute:NSKernAttributeName inRange:NSMakeRange(0, currentText.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id value, NSRange range, BOOL *stop){
float value = [(NSNumber *)value floatValue];
//Do something with value, but logs show value is always 0
}];
存储
NSMutableAttributedString *updatedText = self.textView.attributedText.mutableCopy;
_kernValue += 0.1f;
NSDictionary *attributes = @{
NSForegroundColorAttributeName: UIColor.linkColor,
NSFontAttributeName: [UIFont systemFontOfSize:14.0],
NSKernAttributeName: [NSNumber numberWithFloat:_kernValue]
};
NSMutableAttributedString *replacementString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ ", myString] attributes:attributes];
[updatedText replaceCharactersInRange:myRange withAttributedString:replacementString];
self.textView.attributedText = updatedText;
来自NSAttributedString
标头:
UIKIT_EXTERN NSString *const NSKernAttributeName NS_AVAILABLE_IOS(6_0); //
NSNumber包含浮点值,以点为单位;修改默认紧排的数量。0表示紧排已禁用。(注意:iOS不支持nil和0以外的值)