当uisng NSKernAttributeName时 - UIFieldEditorContentView比UITextField宽

时间:2016-02-03 15:27:45

标签: ios objective-c

我尝试将UITextField中的值格式化为每4个字符使用NSKernAttributeName对4个字符(如xxxx xxxx xxxx ....)进行分组

    NSMutableAttributedString *result = nil;    
    for (NSUInteger i=0; i<[stringToFormate length]-1; i++) {
                if (i>0 && (i+1)%4 == 0) {
                    [result addAttributes:@{NSKernAttributeName : @(4.0f)} range:NSMakeRange(i, 1)];
                }
                else {
                   [result addAttributes:@{NSKernAttributeName : @(0.0f)} range:NSMakeRange(i, 1)];
                }
            }
    }

问题是绘制文本的内容比文本框架宽。也许UITextField不知道如何使用kern属性。

任何建议,如何解决这个问题?谢谢!

enter image description here

1 个答案:

答案 0 :(得分:0)

我找到了一个解决方案。它有效,但它不是很软化...

唯一的方法我怎样才能说服内容区域更新本身就是左转并走向正确:

UITextPosition *beginning = textField.beginningOfDocument;
            UITextPosition *cursorLocation = [textField positionFromPosition:beginning offset:(range.location + string.length)];
            UITextPosition *cursorLocation2 = [textField positionFromPosition:beginning offset:(range.location - 1 + string.length)];

        if(cursorLocation) {
            // set start/end location to same spot so that nothing is highlighted
            [textField setSelectedTextRange:[textField textRangeFromPosition:cursorLocation2 toPosition:cursorLocation2]];
            [textField setSelectedTextRange:[textField textRangeFromPosition:cursorLocation toPosition:cursorLocation]];
        }

- 包括此修复程序的整个解决方案在这里:

    - (BOOL)textFieldShouldReturn:(UITextField *)textField //resign first responder for textfield
    {
        [textField resignFirstResponder];
        return YES;
    }


    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

        textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
        textField.attributedText = [self fourDigitFormattedIban:textField.text];

        UITextPosition *beginning = textField.beginningOfDocument;
        UITextPosition *cursorLocation = [textField positionFromPosition:beginning offset:(range.location + string.length)];
        UITextPosition *cursorLocation2 = [textField positionFromPosition:beginning offset:(range.location - 1 + string.length)];

        if(cursorLocation) {
            // set start/end location to same spot so that nothing is highlighted
            [textField setSelectedTextRange:[textField textRangeFromPosition:cursorLocation2 toPosition:cursorLocation2]];
            [textField setSelectedTextRange:[textField textRangeFromPosition:cursorLocation toPosition:cursorLocation]];
        }

        return NO;
    }


- (NSAttributedString*)fourDigitFormattedIban:(NSString*)string {

    NSString *stringWithoutSpaces = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSMutableAttributedString *result = nil;
    if (stringWithoutSpaces) {
        result = [[NSMutableAttributedString alloc] initWithString:stringWithoutSpaces attributes:nil];
        if ([stringWithoutSpaces length] > 0) {
            for (NSUInteger i=0; i<[stringWithoutSpaces length]-1; i++) {
                if (i>0 && (i+1)%4 == 0) {
                    [result addAttributes:@{NSKernAttributeName : @(4)} range:NSMakeRange(i, 1)];
                }
                else {
                    [result addAttributes:@{NSKernAttributeName : @(0.0f)} range:NSMakeRange(i, 1)];
                }
            }
        }
    }
    return result;
}