Reputation: 767
when I get mytextfield.text
the result is this: "۱۲۳۴" which is a Persian number of "1234".
I have a method which converts Persian text to English text.
I want to get the English text when getting text using mytextfield.text
solution 1: I can realize when the user changes the textfield then I can change it to what I want. but its performance is not good because user might want to change it several time I just want to convert it when I want to get it.
Upvotes: 3
Views: 329
Reputation: 3060
You can do this as you want using category in Objective-c
, see the implementation below .
UITextField+Category.h
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UITextField (Category)
@end
NS_ASSUME_NONNULL_END
UITextField+Category.m
#import "UITextField+Category.h"
@implementation UITextField (Category)
- (NSString *)text {
return ConvertToEnglish(super.text); //Call your Util function From here
}
@end
Uses
#import "UITextField+Category.h"
- (void)whenYouWant {
//Just call txtName.text Call anywhere in the class :
NSLog(@"%@", self.txtName.text);
}
Upvotes: 0
Reputation: 4333
How about creating a sub-class of UITextField and overriding .text
?
In your .h file
#import <UIKit/UIKit.h>
@interface TranslatingTextField : UITextField
@end
In your .m file
@implementation TranslatingTextField
- (NSString *)text {
return MyTranslation(super.text); // Your translation is called here
}
@end
If you are using Interface Builder make sure to specify TranslatingTextField
as the class for the text fields you want to use this class for.
Upvotes: 3
Reputation: 15247
I probably do not understand your question, but I will try an answer anyway.
I assume your UITextField
is setup in storyboard, and you defined an outlet from your code to it, something like
@property(nonatomic, weak) IBOutlet UITextField *mytextfield;
So you can access the textField by program by its identifier.
Or if you did setup the mytextfield
by program anyway, you can also access it in the same way.
I assume, you know the times when you want to read mytextfield.text
to convert the text to English, and I assume you have a method to convert the text to English, something like
-(NSString)convertToEnglish:(NSString *)text {…}
Why don’t you simply read the textField text and feed it into this method, like
NSString *englishText = [self convertToEnglish: mytextfield.text];
But as I said, I probably did not understand your question…
Upvotes: 0
Reputation: 33
Have you considered adding a UIButton with a IBAction, and only when the button is tapped, you get the text ?
@IBAction func buttonTapped(_sender : AnyObject){ // Here you get the text and convert it}
Upvotes: 0