UITextView的NSKernAttributeName始终为0

时间:2013-07-25 20:32:53

标签: ios ios6 uitextview nsattributedstring uitextinput

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

1 个答案:

答案 0 :(得分:0)

来自NSAttributedString标题:

UIKIT_EXTERN NSString *const NSKernAttributeName NS_AVAILABLE_IOS(6_0);                //
  

包含浮点值的NSNumber,以磅为单位;等于修改默认字距调整。 0表示禁用字距调整。 (注意:iOS上不支持nil和0以外的值)