我将 NSKernAttributeName 设置为 iOS 6 上 UITextView 的属性文本属性上的不同浮点值,并且在特定范围内。每次使用属性文本的 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;