Right aligned UITextField spacebar does not advance cursor in iOS 7

前端 未结 14 1687
执笔经年
执笔经年 2020-12-07 23:04

In my iPad app, I noticed different behavior between iOS 6 and iOS 7 with UITextFields.

I create the UITextField as follows:

UIButton *theButton = (U         


        
14条回答
  •  既然无缘
    2020-12-07 23:32

    I've came up with a solution that subclasses the UITextField class and performs the swap, without the need of copying and pasting code everywhere. This also avoids using method sizzle to fix this.

    @implementation CustomTextField
    
    -(id) initWithCoder:(NSCoder *)aDecoder {
        self = [super initWithCoder:aDecoder];
    
        if( self ) {
    
            [self addSpaceFixActions];
        }
    
        return self;
    }
    
    - (void)addSpaceFixActions {
        [self addTarget:self action:@selector(replaceNormalSpaces) forControlEvents:UIControlEventEditingChanged];
        [self addTarget:self action:@selector(replaceBlankSpaces) forControlEvents:UIControlEventEditingDidEnd];
    }
    
    
    //replace normal spaces with non-breaking spaces.
    - (void)replaceNormalSpaces {
        if (self.textAlignment == NSTextAlignmentRight) {
            UITextRange *textRange = self.selectedTextRange;
            self.text = [self.text stringByReplacingOccurrencesOfString:@" " withString:@"\u00a0"];
            [self setSelectedTextRange:textRange];
        }
    }
    
    //replace non-breaking spaces with normal spaces.
    - (void)replaceBlankSpaces {
        self.text = [self.text stringByReplacingOccurrencesOfString:@"\u00a0" withString:@" "];
    }
    

提交回复
热议问题