Coder Social home page Coder Social logo

decimaltextfield's Introduction

DecimalTextField

用来限制用户输入小数点限制的封装类继承自UITextField

使用注意

使用DecimalTextFieldDelegate时,父类UITextFieldDelegate代理通过DecimalTextFieldDelegate方法实现,UITextFieldDelegate将使用无效(代理不能够继承)

使用方法

  1. 声明代理
  2. 设置代理
  3. 实现代理方法
//MARK: --DecimalTextFieldDelegate 实现此代理方法用户限制用户输入小数
- (BOOL)decimalTextField:(DecimalTextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string hasDot:(BOOL)hasDot{


if (string.length > 0) {
// 小数点后最多能输入几位
if (hasDot) {
NSRange ran = [textField.text rangeOfString:@"."];
// 由于range.location是NSUInteger类型的,所以这里不能通过(range.location - ran.location)>2来判断
int scale = 0;
if (range.location > ran.location) {
if (textField == self.textField) {
scale = ScaleLimit;
}
if ([textField.text pathExtension].length >= scale) {

NSLog(@"只能输入%d位小数",scale);


return NO;
}
}
}
}
return YES;
}

  1. 精简写法
self.textField.decimalScale = 3;//设置精确3位小数

decimaltextfield's People

Contributors

samuncle avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.