{iOS 6.0} UITextField - restrict user to enter WhiteSpace - ios6

For iOS 6.0 and later UITextField does not work as expected, whenever I try to restrict user to enter a WhiteSpace, it enters the characters available in row above the Space bar (e.g. 'C', 'V', 'B', 'N', 'M').
I am using following code of lines to avoid WhiteSpace however the same code works perfect in iOS 5.0
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
BOOL flag = YES;
NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string];
NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
if ([resultingString rangeOfCharacterFromSet:whitespaceSet].location == NSNotFound)
flag = YES;
else
flag = NO;
return flag;
}
Any suggestion?

Try This
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([string isEqualToString:#" "])
return NO ;
return YES ;
}

Related

Is there something that avoids some characters (1, f, x, and b, specifically) to be validated from an input?

I have a field to validate repeated questions. I must convert special characters into normal letters using a method, and then I use a method to skip spaces and other characters to validate if the new text is equal to the existing questions. In my local everything is working fine, but in a Production instance, it skips characters 1, f, x and b. My project in Production is mounted in Linux.
My two methods are:
replaceSpecialCharacters(output){
return output = output.replace(/á|é|í|ó|ú|ñ|ä|ë|ï|ö|ü|à|è|ì|ò|ù/ig,function (str,offset,s) {
var str = str=="á"?"a":str=="é"?"e":str=="í"?"i":str=="ó"?"o":str=="ú"?"u":str=="ñ"?"n":str;
str = str=="Á"?"A":str=="É"?"E":str=="Í"?"I":str=="Ó"?"O":str=="Ú"?"U":str=="Ñ"?"N":str;
str = str=="à"?"a":str=="è"?"e":str=="ì"?"i":str=="ò"?"o":str=="ù"?"u":str;
str = str=="À"?"A":str=="È"?"E":str=="Ì"?"I":str=="Ò"?"O":str=="Ù"?"U":str;
str = str=="ä"?"a":str=="ë"?"e":str=="ï"?"i":str=="ö"?"o":str=="ü"?"u":str;
str = str=="Ä"?"A":str=="Ë"?"E":str=="Ï"?"I":str=="Ö"?"O":str=="Ü"?"U":str;
return (str);
});
}
validateRepeatedQuestion(question): void {
var questionToCheck = this.replaceSpecialCharacters(question).replace(/[(\s)+(\¿)+(\?)+(\¡)+(\!)+(\")+(\')+]/g, "");
this.setState({isRepeated: false});
for (let i = 0; i < this.props.allQuestions.length; i++) {
var questionFromArray = this.replaceSpecialCharacters(this.props.allQuestions[i].text).replace(/[(\s)+(\¿)+(\?)+(\¡)+(\!)+(\")+(\')+]/g, "");
if(this.props.allQuestions[i].position != this.props.question.position){
if(questionFromArray.toLocaleLowerCase() == questionToCheck.toLocaleLowerCase()){
this.setState({isRepeated: true});
}
}
}
}
If I have a question like "How old are you?" and I try to add a question like "How old are you?1111" I expect the validation to take the new question as different, but the actual output says that the question is repeated, even if I'm adding numbers 1. It must be different.

AngularJS -- See which character was deleted by the backspace or delete key without comparing

I am creating an application that needs to take user input from a text input. If, at any point, the character that was inputted was incorrect (ie: a number when it should only be a-z), the input box will become invalid. I then take that character and place it in an array of "bad characters" that are in the input. The input box should become valid again if the character is deleted.
I am aware that I can take the input with every new key press and check that, but then that is going through each character to make sure it is allowed.
I am curious if there is anyway to see what character was deleted with a backspace press.
I cannot post my code, I'm sorry.
I am using ng-keydown and mg-model in html. When the confirm button is pressed, the input should be valid from the ng-model.
Thanks -- I am teaching myself Angularjs, so I am not the best with it yet.
To get the deleted characters you can use plain old javascript:
var caret_get_position = function(element){
var pos = 0;
var posEnd = 0;
if('selectionStart' in element){
pos = element.selectionStart;
posEnd = element.selectionEnd;
}else if('selection' in document){
element.focus();
var Sel = document.selection.createRange();
var SelLength = document.selection.createRange().text.length;
Sel.moveStart('character', -element.value.length);
pos = Sel.text.length-SelLength;
posEnd = Sel.text.length;
}
// return both selection start and end;
return [pos, posEnd];
};
//get deletet character
element.addEventListener('keydown', function(event){
var keycode = ('which' in event)?event.which:event.keyCode;
caret_positions = caret_get_position(element);
var val = this.value;
if(keycode == 8){
if(caret_positions[0] == caret_positions[1]){
if(caret_positions[0] == 0){
deleted = '';
}else{
deleted = val.substr(caret_positions[0]-1, 1);
}
}else{
deleted = val.substring(caret_positions[0], caret_positions[1]);
}
}else if(keycode == 46){
if(caret_positions[0] == caret_positions[1]){
if(caret_positions[0] === val.length){
deleted = '';
}else{
deleted = val.substr(caret_positions[0], 1);
}
}else{
deleted = val.substring(caret_positions[0], caret_positions[1]);
}
}
});
So the hell am i doing here:
caret_get_position is pretty much self-explanatory. It will get the current position of the caret (the blinking line in your inputfield). Even with a selected range (this blue stuff) if you select more than one character.
In the keydown event it will check the pressed key and some more checks where the caret is.
In the end the deleted characters are in the variable deleted. This is from my code so you need to adjust it for your needs.

Dynamic AutoComplete

When I type a location starting with 'W', the related locations are listed below. But if I erase the already typed location and then type in a different one starting with 'L', then the list shows the previously listed options for the old location first(locations starting with 'W') then the options related to new location are listed.
Because of this the autocomplete list displays the locations starting with 'W' and then the locations starting with 'L',both.
I also tried placing options.removeAll(); as first statement in the filter method.
AutoCompleteTextField ac = new AutoCompleteTextField(options) {
protected boolean filter(String add) {
options.removeAll();
if(add.length() == 0) {
return false;
}
String[] l = searchLocations(add);
if(l == null || l.length == 0) {
return false;
}
for(String s : l) {
options.addItem(s);
}
return true;
}
};
//ac.setMinimumElementsShownInPopup(1);
ac.setMinimumLength(1);
Container c = stateMachine.findContainer(form);
AutoCompleteTextField oldac = (AutoCompleteTextField) stateMachine.findAddress(c);
c.replace(oldac, ac, null);
Is there a way to rectify this?
Thanks!!
Check out this live sample, this issue doesn't occur here so I'm guessing the problem with the preexisting results is related to the way you modified the the model.

Looking for a nice way to split an array

I've been looking for a method similar to String.split in Scala Array, but I've not been able to find it.
What I want to do is to split an array by a separator.
For example, separating the following array:
val array = Array('a', 'b', '\n', 'c', 'd', 'e', '\n', 'g', '\n')
using the '\n' separator, should result in:
List(Array(a, b), Array(c, d, e), Array(g))
I know that I can convert the Array to String, and apply split there:
array.mkString.split('\n').map(_.toArray)
but I would prefer to skip the conversion.
The solution I have so far involves using span recursively and is a bit too boilerplate:
def splitArray[T](array: Array[T], separator: T): List[Array[T]] = {
def spanRec(array: Array[T], aggResult: List[Array[T]]): List[Array[T]] = {
val (firstElement, restOfArray) = array.span(_ != separator)
if (firstElement.isEmpty) aggResult
else spanRec(restOfArray.dropWhile(_ == separator), firstElement :: aggResult)
}
spanRec(array, List()).reverse
}
I'm sure there must be something in Scala I'm missing. Any idea?
thanks,
Ruben
This is not the most concise implementation, but it should be fairly performed and preserves the array type without resorting to reflection. The loop can of course be replaced by a recursion.
Since your question doesn't explicitly state what should be done with the separator I assume, that they should not cause any entry in the output list (see the test cases below).
def splitArray[T](xs: Array[T], sep: T): List[Array[T]] = {
var (res, i) = (List[Array[T]](), 0)
while (i < xs.length) {
var j = xs.indexOf(sep, i)
if (j == -1) j = xs.length
if (j != i) res ::= xs.slice(i, j)
i = j + 1
}
res.reverse
}
Some tests:
val res1 =
// Notice the two consecutive '\n'
splitArray(Array('a', 'b', '\n', 'c', 'd', 'e', '\n', '\n', 'g', '\n'), '\n')
println(res1)
// List([C#12189646, [C#c31d6f2, [C#1c16b01f)
res1.foreach(ar => {ar foreach print; print(" ")})
// ab cde g
// No separator
val res2 = splitArray(Array('a', 'b'), '\n')
println(res2)
// List([C#3a2128d0)
res2.foreach(ar => {ar foreach print; print(" ")})
// ab
// Only separators
val res3 = splitArray(Array('\n', '\n'), '\n')
println(res3)
// List()
A borrowed the arguments from sschaef's solution:
def split[T](array : Array[T])(where : T=>Boolean) : List[Array[T]] = {
if (array.isEmpty) Nil
else {
val (head, tail) = array span {!where(_)}
head :: split(tail drop 1)(where)
}
} //> split: [T](array: Array[T])(where: T => Boolean)List[Array[T]]
val array = Array('a', 'b', '\n', 'c', 'd', 'e', '\n', 'g', '\n')
split(array){_ =='\n'} //> res2: List[Array[Char]] = List(Array(a, b), Array(c, d, e), Array(g))
def splitByNewLines(array : Array[Char]) = split(array){_ =='\n'}
splitByNewLines(array) //> res3: List[Array[Char]] = List(Array(a, b), Array(c, d, e), Array(g))
You can use the span method to split the array into two parts and then call your split method recursively on the second part.
import scala.reflect.ClassTag
def split[A](l:Array[A], a:A)(implicit act:ClassTag[Array[A]]):Array[Array[A]] = {
val (p,s) = l.span(a !=)
p +: (if (s.isEmpty) Array[Array[A]]() else split(s.tail,a))
}
This is not very efficient though, since it has quadratic performance. If you want something fast, a simple tail recursive solution will probably be the best approach.
With lists instead of arrays you would get linear performance and wouldn't need reflection.
This is a short formulation that should do the job:
def split(array:Array[Char], sep:Char) : Array[Array[Char]] = {
/* iterate the list from right to left and recursively calculate a
pair (chars,list), where chars contains the elements encountered
since the last occurrence of sep.
*/
val (chars, list) = array.foldRight[(List[Char],List[Array[Char]])]((Nil,Nil))((x,y) => if (x == sep) (Nil, (y._1.toArray)::y._2) else (x::y._1, y._2) );
/* if the last element was sep, do nothing;
otherwise prepend the last collected chars
*/
if (chars.isEmpty)
list.toArray
else
(chars.toArray::list).toArray
}
/* example:
scala> split(array,'\n')
res26: Array[Array[Char]] = Array(Array(a, b), Array(c, d, e), Array(g), Array())
*/
If we use List instead of Array, we can generalize the code a bit:
def split[T](array:List[T], char:T) : List[List[T]] = {
val (chars, list) = array.foldRight[(List[T],List[List[T]])]((Nil,Nil))((x,y) => if (x == char) (Nil, (y._1)::y._2) else (x::y._1, y._2) )
if (chars.isEmpty) list else (chars::list)
}
/* example:
scala> split(array.toList, '\n')
res32: List[List[Char]] = List(List(a, b), List(c, d, e), List(g), List())
scala> split(((1 to 5) ++ (1 to 5)).toList, 3)
res35: List[List[Int]] = List(List(1, 2), List(4, 5, 1, 2), List(4, 5))
*/
If this solution is considered as elegant or unreadable is left to the reader and her preference for functional programming :)
Simple way of doing it using foldLeft
val f = array.foldLeft((Array[Char](),List[Array[Char]]()))(
(acc, char: Char) => {
char match {
case '\n' => (Array(),acc._1 :: acc._2)
case _ => (acc._1 :+ char,acc._2)
}
}
)._2.reverse
I came up with a solution that aims at the following:
is generic: you should be able to split an Array just like a Vector, and a collection of Chars just like a collection of arbitrary objects
preserves the types of the inputs: an Array[A] gets split in an Array[Array[A]], a Vector[A] gets split in a Vector[Vector[A]]
allows to use a lazy approach if needed (via an Iterator)
exposes a compact interface for most cases (just call a split method on your collection)
Before getting to the explanation, note that you can play with the code that follows here on Scastie.
The first step is implementing an Iterator that chunks your collection:
import scala.language.higherKinds
import scala.collection.generic.CanBuildFrom
final class Split[A, CC[_]](delimiter: A => Boolean, as: CC[A])(
implicit view: CC[A] => Seq[A], cbf: CanBuildFrom[Nothing, A, CC[A]])
extends Iterator[CC[A]] {
private[this] var it: Iterator[A] = view(as).iterator
private def skipDelimiters() = {
it = it.dropWhile(delimiter)
}
skipDelimiters()
override def hasNext: Boolean = it.hasNext
override def next(): CC[A] = {
val builder = cbf()
builder ++= it.takeWhile(!delimiter(_))
skipDelimiters()
builder.result()
}
}
I'm using a predicate instead of a value to be more elastic in how the collection gets split, especially when splitting a collection of non-scalar values (like Chars).
I'm using an implicit view on the collection type to be able to apply this to all kinds of collection that can be seen as a Seq (like Vectors and Arrays) and a CanBuildFrom to be able to build the exact type of collection I'm receiving as an input.
The implementation of the Iterator simply makes sure to drop delimiters and chunk the rest.
We can now use an implicit class to offer a friendly interface and add the split method to all the collections, both allowing a predicate or a value to be defined as delimiters:
final implicit class Splittable[A, CC[_]](val as: CC[A])(implicit ev1: CC[A] => Seq[A], ev2: CanBuildFrom[Nothing, A, CC[A]], ev3: CanBuildFrom[Nothing, CC[A], CC[CC[A]]]) {
def split(delimiter: A => Boolean): CC[CC[A]] = new Split(as)(delimiter).to[CC]
def split(delimiter: A): CC[CC[A]] = new Split(as)(_ == delimiter).to[CC]
}
Now you can use your method freely on collection of Chars
val a = Array('a', 'b', '\n', 'c', 'd', 'e', '\n', 'g', '\n')
val b = List('\n', '\n', '\n')
val c = Vector('\n', 'c', 'd', 'e', '\n', 'g', '\n')
val d = Array('a', 'b', 'c', 'd', 'e', 'g', '\n')
val e = Array('a', 'b', 'c', 'd', 'e', 'g', '\n')
a.split('\n')
b.split('\n')
c.split('\n')
d.split('\n')
e.split('\n')
and arbitrary objects alike:
final case class N(n: Int, isDelimiter: Boolean)
Vector(N(1, false), N(2, false), N(3, true), N(4, false), N(5, false)).split(_.isDelimiter)
Note that by using the iterator directly you use a lazy approach, as you can see if you add a debug print to the next method and try to execute the following:
new Split(Vector('\n', 'c', 'd', 'e', '\n', 'g', '\n'))(_ == '\n'}).take(1).foreach(println)
If you want, you can add a couple of methods to Splittable that return an Iterator, so that you can expose the lazy approach as well directly through it.
I don't know of any build-in method, but I came up with a simpler one than yours:
def splitOn[A](xs: List[A])(p: A => Boolean): List[List[A]] = xs match {
case Nil => Nil
case x :: xs =>
val (ys, zs) = xs span (!p(_))
(x :: ys) :: splitOn(zs.tail)(p)
}
// for Array
def splitOn[A : reflect.ClassTag](xs: Array[A])(p: A => Boolean): List[Array[A]] =
if (xs.isEmpty) List()
else {
val (ys, zs) = xs.tail span (!p(_))
(xs.head +: ys) :: splitOn(zs.tail)(p)
}
scala> val xs = List('a', 'b', '\n', 'c', 'd', 'e', '\n', 'g', '\n')
xs: List[Char] =
List(a, b,
, c, d, e,
, g,
)
scala> splitOn(xs)(_ == '\n')
res7: List[List[Char]] = List(List(a, b), List(c, d, e), List(g))
How about this? No reflection, and not recursive either but tries to use as much of the scala library as possible.
def split[T](a: Array[T], sep: T)(implicit m:ClassManifest[T]): Array[Array[T]] = {
val is = a.indices filter (a(_) == sep)
(0 +: (is map (1+))) zip (is :+ (a.size+1)) map {
case(from,till) => a.slice(from, till)
}
}
Probably slow, but just for fun. :-)
The indices filter gives you the indices (is) of where your separator was found.
In your example, that's 2,6,8. I think this is O(n).
The next line transforms that into (0,2), (3,6), (7,8), (9, 10). So k separators yield k+1 ranges.
These are handed to slice, which does the rest of the work. The transformation is also O(n) where n is the number of separators found.
(This means an input of Array[Char]() will yield Array(Array()) and not the more intuitive Array() but that's not too interesting).
The array appending/prepending (:+, +:) is wasteful using arrays, but nothing that can't be solved by using an appropriate collection that lets you have O(1) appends/prepends.
You can also accomplish this using fold:
def splitArray[T](array: Array[T], separator: T) =
array.foldRight(List(List.empty[T])) { (c, list) =>
if (c == separator) Nil :: list
else (c :: list.head) :: list.tail
}.filter(!_.isEmpty).map(_.reverse).toArray
which was already mentioned by lambda.xy.x, but for some reason it was a bit less readable then necessary ;)
Pimped version of generic sequence / array split -
implicit def toDivide[A, B <% TraversableLike[A, B]](a : B) = new {
private def divide(x : B, condition: (A) => Boolean) : Iterable[B] = {
if (x.size > 0)
x.span(condition) match {
case (e, f) => if (e.size > 0) Iterable(e) ++ divide(f.drop(1),condition) else Iterable(f)
}
else
Iterable()
}
def divide(condition: (A) => Boolean): Iterable[B] = divide(a, condition)
}
Almost a one-liner:
val it = array.iterator
List.range(0, array.count(_ == '\n')).map(_ => it.takeWhile(_ != '\n').toArray)
For a given array, this makes use of an Iterator version of the Array in order to call .takeWhile as many times as there are occurrences of the separator.
Another version of the same a bit shorter although less readable, using List.tabulate which is a map over a range:
val it = array.iterator
List.tabulate(array.count(_ == '\n'))(_ => it.takeWhile(_ != '\n').toArray)
This can be made a generic equivalent of array.mkString.split("\n", -1).map(_.toArray) by pimping Array with:
implicit class ArrayExtensions[T: ClassTag](array: Array[T]) {
def split(sep: T): List[Array[T]] = {
val it = array.iterator
List.range(0, array.count(_ == sep)).map(_ => it.takeWhile(_ != sep).toArray)
}
}
and used this way:
Array('\n', '\n', 'a', 'b', '\n', 'c', 'd', 'e', '\n', 'g', '\n').split('\n')
// List(Array(), Array(), Array(a, b), Array(c, d, e), Array(g), Array())
To get rid of empty sub-arrays resulting from 2 successive occurrences of the separator, one can pipe the result with .filter(_.nonEmpty).

Get last 3 characters during user typing

I need get last three character from richTextBox during when the user writes some text in richTextbox.
I bind property on Text property of richTextBox from Extended WPF toolkit.
public string RtbText
{
get { return _rtbText; }
set
{
_rtbText = value;
NotifyPropertyChanged("RtbText");
}
}
I use Reactive Extensions for .NET (Rx) and make Observer on property RtbText
Observable.FromEvent<PropertyChangedEventArgs>(this, "PropertyChanged")
.Where(e => e.EventArgs.PropertyName == "RtbText")
.Select(_ => this.RtbText)
.Where(text => text.Length > 1)
.Do(AddSmiles)
.Throttle(TimeSpan.FromSeconds(1))
.Subscribe(GetLastThreeChars);
private void GetLastThreeChars(string text)
{
if (text.Length > 3)
{
string lastThreeChars = text.Substring(text.Length - 2, text.Length);
}
}
But if I start typing in richTextBox I get this exception:
Index and length must refer to a location within the string.
Parameter name: length
at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
at System.String.Substring(Int32 startIndex, Int32 length)
at WpfApplication1.MainWindow.GetLastThreeChars(String text) in C:\Users\Jan\Documents\Visual Studio 2010\Projects\C#\Pokec_Messenger\ver.beta\IoC.Get\Pokec_Messenger\ver.beta\Pokec_Messenger\WpfApplication1\MainWindow.xaml.cs:line 97
at System.Linq.Observable.<>c_DisplayClass389`1.<>c_DisplayClass38b.b_388(TSource x)
If text.Length > 3 (say it's 4) then:
text.Length - 2 = 2
So you're code is:
string lastThreeChars = text.Substring(2, 4);
This will fail as you are asking for four characters in the substring, which puts it out of range.
String.Substring Method (Int32, Int32)
Retrieves a substring from this instance. The substring starts at a specified character position and has a specified length.
Additionally your test and starting position are incorrect. Don't forget that C# arrays and strings are zero indexed. Checking for the case of length strictly greater than 3 you'll miss the case when the user has entered exactly three characters when you want to return the entire string.
Your code needs to be:
if (text.Length > 2)
{
string lastThreeChars = text.Substring(text.Length - 3, 3);
}
If fact you don't need to specify the length:
if (text.Length > 2)
{
string lastThreeChars = text.Substring(text.Length - 3);
}
will return the last three characters in the string.
This is another form. Which gets all the characters from some start position up to the end
string lastThreeChars = text.Substring(text.Length - 3);
maybe text.Length - 2. Untested

Resources