美文网首页梦想者程序员的故事程序员
文本框 光标,键盘退出,间距之问题

文本框 光标,键盘退出,间距之问题

作者: 南镇s | 来源:发表于2016-01-12 16:51 被阅读331次

    1.textField明文暗文光标位置不对


    明文暗文光标不对.png
    解决方案:在切换明文暗文的方法上写入 
    [self.oldPwdTextField becomeFirstResponder];
    

    2.加入了scrollView导致,监听键盘退出失效

    解决方案:给scrollView增加一个分类,并重写以下几个方法
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        [[self nextResponder] touchesBegan:touches withEvent:event];
        [super touchesBegan:touches withEvent:event];
    }
    
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        [[self nextResponder] touchesMoved:touches withEvent:event];
        [super touchesMoved:touches withEvent:event];
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        [[self nextResponder] touchesEnded:touches withEvent:event];
        [super touchesEnded:touches withEvent:event];
    }
    

    3.限制UITextField内只能输入0~9的数字

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
        return [self validateNumber:string];
    }
    
    - (BOOL)validateNumber:(NSString*)number {
        BOOL res = YES;
        NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
        int i = 0;
        while (i < number.length) {
            NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
            NSRange range = [string rangeOfCharacterFromSet:tmpSet];
            if (range.length == 0) {
                res = NO;
                break;
            }
            i++;
        }
        return res;
    }
    
    当然还可以将键盘设置为
    textField.keyboardType = UIKeyboardTypeNumberPad;
    

    4.设置UITextView之间的行间距,上下左右间距

    行间距:
    -(void)textViewDidChange:(UITextView *)textView
    {
        //    textview 改变字体的行间距
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 5;// 字体的行间距
        
        NSDictionary *attributes = @{
                                     NSFontAttributeName:[UIFont systemFontOfSize:12],
                                     NSParagraphStyleAttributeName:paragraphStyle,
                                     NSForegroundColorAttributeName : XWColor(140, 140, 140)
                                     };
        textView.attributedText = [[NSAttributedString alloc] initWithString:textView.text attributes:attributes];
        
    }
    
    上下左右间距:
    textView.textContainerInset = UIEdgeInsetsMake(7, 7, 7, 7);
    

    相关文章

      网友评论

      本文标题:文本框 光标,键盘退出,间距之问题

      本文链接:https://www.haomeiwen.com/subject/tohckttx.html