rangeofstring的简单介绍

http://www.itjxue.com  2023-02-24 06:37  来源:未知  点击次数: 

ios金额输入小数点等问题

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

if([textField.textrangeOfString:@"."].location==NSNotFound)

{

isHaveDian=NO;

}

if([stringlength] 0)

{

unicharsingle = [stringcharacterAtIndex:0];//当前输入的字符

if((single ='0' single ='9') || single =='.')//数据格式正确

{

//首字母不能为0和小数点

if([textField.textlength] ==0)

{

if(single =='.')

{

//[self showMyMessage:@"亲,第一个数字不能为小数点!"];

[textField.textstringByReplacingCharactersInRange:rangewithString:@""];

returnNO;

}

//if (single == '0')

//{

//

////[self showMyMessage:@"亲,第一个数字不能为0!"];

//

//[textField.text stringByReplacingCharactersInRange:range withString:@""];

//

//return NO;

//

//}

}

//输入的字符是否是小数点

if(single =='.')

{

if(!isHaveDian)//text中还没有小数点

{

isHaveDian=YES;

returnYES;

}else{

//[self showMyMessage:@"亲,您已经输入过小数点了!"];

[textField.textstringByReplacingCharactersInRange:rangewithString:@""];

returnNO;

}

}else{

if(isHaveDian) {//存在小数点

//判断小数点的位数

NSRangeran = [textField.textrangeOfString:@"."];

if(range.location- ran.location=2) {

returnYES;

}else{

//[self showMyMessage:@"亲,您最多输入两位小数!"];

returnNO;

}

}else{

returnYES;

}

}

}else{//输入的数据格式不正确

//[self showMyMessage:@"亲,您输入的格式不正确!"];

[textField.textstringByReplacingCharactersInRange:rangewithString:@""];

returnNO;

}

}

else

{

returnYES;

}

}

swift为什么不使用string自带的rangeofstring

请不要使用String自带的rangeOfString方法,而是使用NSString的。

所以可以这么来:

var str: NSString = "abcd";

var loc = str.rangeOfString("bc").location

println("bc location is: \(loc)")

改变字符串中的字体和颜色

/**

*改变字符串中具体某字符串的颜色

*/

+?(void)messageAction:(UILabel*)theLabchangeString:(NSString*)changeandAllColor:(UIColor*)allColorandMarkColor:(UIColor*)markColorandMarkFondSize:(float)fontSize?{

NSString*tempStr?=?theLab.text;

NSMutableAttributedString*strAtt?=?[[NSMutableAttributedStringalloc]initWithString:tempStr];

[strAttaddAttribute:NSForegroundColorAttributeNamevalue:allColorrange:NSMakeRange(0,?[strAttlength])];

NSRange?markRange?=?[tempStrrangeOfString:change];

[strAttaddAttribute:NSForegroundColorAttributeNamevalue:markColorrange:markRange];

[strAttaddAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"HelveticaNeue"size:fontSize]range:markRange];

theLab.attributedText=?strAtt;

}

/*

*x*y

*改变字符start?和?end?之间的字符的颜色?和?字体大小

*/

+?(void)messageAction:(UILabel*)theLabstartString:(NSString*)startendString:(NSString*)endandAllColor:(UIColor*)allColorandMarkColor:(UIColor*)markColorandMarkFondSize:(float)fontSize?{

NSString*tempStr?=?theLab.text;

NSMutableAttributedString*strAtt?=?[[NSMutableAttributedStringalloc]initWithString:tempStr];

[strAttaddAttribute:NSForegroundColorAttributeNamevalue:allColorrange:NSMakeRange(0,?[strAttlength])];

//?'x''y'字符的范围

NSRange?tempRange?=?NSMakeRange(0,0);

if([selfjudgeStringIsNull:start])?{

tempRange?=?[tempStrrangeOfString:start];

}

NSRange?tempRangeOne?=?NSMakeRange([strAttlength],0);

if([selfjudgeStringIsNull:end])?{

tempRangeOne?=??[tempStrrangeOfString:end];

}

//?更改字符颜色

NSRange?markRange?=?NSMakeRange(tempRange.location+tempRange.length,?tempRangeOne.location-(tempRange.location+tempRange.length));

[strAttaddAttribute:NSForegroundColorAttributeNamevalue:markColorrange:markRange];

//?更改字体

//?[strAtt?addAttribute:NSFontAttributeName?value:[UIFont?fontWithName:@"HelveticaNeue-Bold"?size:20]?range:NSMakeRange(0,?[strAtt?length])];

[strAttaddAttribute:NSFontAttributeNamevalue:[UIFontfontWithName:@"HelveticaNeue"size:fontSize]range:markRange];

theLab.attributedText=?strAtt;

}

/**

*改变字符串中所有数字的颜色

*/

+?(void)setRichNumberWithLabel:(UILabel*)labelColor:(UIColor*)colorFontSize:(CGFloat)size?{

NSMutableAttributedString*attributedString?=?[[NSMutableAttributedStringalloc]initWithString:label.text];

NSString*temp?=nil;

for(inti?=0;?i??[attributedStringlength];?i++)?{

temp?=?[label.textsubstringWithRange:NSMakeRange(i,1)];

if([selfisPureInt:temp])?{

[attributedStringsetAttributes:[NSDictionarydictionaryWithObjectsAndKeys:

color,?NSForegroundColorAttributeName,

[UIFontsystemFontOfSize:size],NSFontAttributeName,nilnil]

range:NSMakeRange(i,1)];

}

}

label.attributedText=?attributedString;

}

/**

*此方法是用来判断一个字符串是不是整型.

*如果传进的字符串是一个字符,可以用来判断它是不是数字

*/

+?(BOOL)isPureInt:(NSString*)string?{

NSScanner*scan?=?[NSScannerscannerWithString:string];

intvalue;

return[scanscanInt:value]??[scanisAtEnd];

}

/**

*判断字符串是否不全为空

*/

+?(BOOL)judgeStringIsNull:(NSString*)string?{

if([[stringclass]isSubclassOfClass:[NSNumberclass]])?{

returnYES;

}

BOOLresult?=NO;

if(string?!=nil?string.length0)?{

for(inti?=0;?i??string.length;?i?++)?{

NSString*subStr?=?[stringsubstringWithRange:NSMakeRange(i,1)];

if(![subStrisEqualToString:@"?"]??![subStrisEqualToString:@""])?{

result?=YES;

}

}

}

returnresult;

}

(责任编辑:IT教学网)

更多
上一篇:没有了

推荐linux文章