Skip to content

Instantly share code, notes, and snippets.

@erzr
Last active December 21, 2015 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erzr/6330382 to your computer and use it in GitHub Desktop.
Save erzr/6330382 to your computer and use it in GitHub Desktop.
Really rough, but works well enough. Almost of an exact port of https://github.com/rodchile/UITextCurrencyField.
// Currency element, port of: https://github.com/rodchile/UITextCurrencyField
public class CurrencyElement : EntryElement
{
public CurrencyElement (string caption, string placeholder, string value)
: base(caption, placeholder, value)
{
}
private static string CleanString(string target)
{
string returnValue = string.Empty;
foreach (char c in target) {
if (char.IsDigit (c) || char.IsWhiteSpace (c)) {
returnValue += c;
}
}
return returnValue;
}
protected override UITextField CreateTextField (RectangleF frame)
{
NSLocale current = NSLocale.CurrentLocale;
string currencySymbol = current.CurrencySymbol;
string groupingSeperator = current.GroupingSeparator;
var formatter = CurrencyFormatter.Currency;
var basic = CurrencyFormatter.Basic;
UITextField field = base.CreateTextField (frame);
if (!string.IsNullOrEmpty (Value)) {
field.Text = Value;
} else {
string initialValue = formatter.StringFromNumber (0);
field.Text = initialValue;
}
field.ShouldChangeCharacters += delegate (UITextField textField, NSRange range, string replacementString) {
bool result = false;
if (string.IsNullOrEmpty(replacementString)) {
result = true;
} else {
string clean = CleanString(replacementString);
if (!string.IsNullOrEmpty(clean)) {
result = true;
}
}
if (result) {
string mstring = textField.Text;
if (!string.IsNullOrEmpty(replacementString)) {
mstring = mstring.Insert(range.Location, replacementString);
} else {
mstring = mstring.Remove(range.Location, range.Length);
}
string cleanString = mstring.Replace(groupingSeperator, "")
.Replace(currencySymbol, "");
if (formatter.MaximumFractionDigits > 0) {
string mutableCleanString = cleanString;
if (!string.IsNullOrEmpty(replacementString)) {
int index = mutableCleanString.IndexOf(".");
mutableCleanString = mutableCleanString.Replace(".", "");
mutableCleanString = mutableCleanString.Insert(index + 1, ".");
cleanString = mutableCleanString;
} else {
mutableCleanString = mutableCleanString.Insert(0, "0");
int index = mutableCleanString.IndexOf(".");
mutableCleanString = mutableCleanString.Replace(".", "");
mutableCleanString = mutableCleanString.Insert(index - 1, ".");
cleanString = mutableCleanString;
}
}
NSNumber number = basic.NumberFromString(cleanString);
string numberString = formatter.StringFromNumber(number);
textField.Text = numberString;
if (EditingChanged != null) {
EditingChanged(this, EventArgs.Empty);
}
}
return false;
};
return field;
}
}
@luiseduardohdbackup
Copy link

Did you have a CurrencyFormatter file or class?, or i dont know what .net namespace is.

@luiseduardohdbackup
Copy link

Oh, i can see what is:
NSNumberFormatter* currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setMaximumFractionDigits:0];
[currencyFormatter setLocale:[NSLocale currentLocale]];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment