我有一个数字可以说0.00.
0.010.121.2312.34我怎么能用Swift做到这一点?
我试图在处理新的文本条目后,通过按键击键,将NSAttributedString样式应用于UITextField.问题是,每当我替换文本时,光标将在每次更改时跳到最后.这是我目前的做法......
立即接收并显示文本更改(当我返回false时同样的问题适用并手动执行文本替换)
func textField(textField: UITextField!, shouldChangeCharactersInRange range: NSRange, replacementString string: String!) -> Bool {
let range:NSRange = NSRange(location: range.location, length: range.length)
let newString = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string);
return true
}
Run Code Online (Sandbox Code Playgroud)
我订阅了UITextFieldTextDidChangeNotification通知.这会触发样式.当文本被更改时,我使用一些format()函数将其替换为格式化版本(NSAttributedString)(NSString).
func textFieldTextDidChangeNotification(userInfo:NSDictionary) {
var attributedString:NSMutableAttributedString = NSMutableAttributedString(string: fullString)
attributedString = format(textField.text) as NSMutableAttributedString
textField.attributedText = attributedString
}
Run Code Online (Sandbox Code Playgroud)
造型效果很好.但是,在每次文本替换后,光标会跳转到字符串的末尾.如何关闭此行为或手动将光标移回到开始编辑的位置?...或者在编辑时是否有更好的解决方案来设置文本样式?
下面的代码让我自动建议输入到UITextfield中的值,方法是将它与先前添加的字符串对象的数组进行比较,并在UITableview中显示它.这很好,但只适用于一个单词.
那么现在,我可以用这样的方式修改代码,即在用户输入逗号后再开始输入,我可以再次搜索相同的字符串数组以获取逗号后输入的字符的建议吗?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField.tag == tagTextFieldTag) //The text field where user types
{
textField.autocorrectionType = UITextAutocorrectionTypeNo;
autocompleteTableView.hidden = NO; //The table which displays the autosuggest
NSString *substring = [NSString stringWithString:textField.text];
substring = [substring stringByReplacingCharactersInRange:range withString:string];
if ([substring isEqualToString:@""])
{
autocompleteTableView.hidden = YES; //hide the autosuggest table if textfield is empty
}
[self searchAutocompleteEntriesWithSubstring:substring]; //The method that compares the typed values with the pre-loaded string array
}
return YES;
}
- (void)searchAutocompleteEntriesWithSubstring:(NSString *)substring { …Run Code Online (Sandbox Code Playgroud)