I don't know if I have chosen right title for my question.
I have developed an app and now I want to process text before assigning text to UILabel or UIView text property.
instead of
myLabel.text = story.text
do this:
myLabel.text = [story.text substituteCharactersOfText];
substituteCharactersOfText is a method of Category I have added to NSString class
so if I have a lot of label or another views, it will be difficult or errorProne to manually call this category method. (maybe I forgot one for anotherLabel.text)
so is there anyway to call this method automatically before assigning text to UILabel.text?
I think maybe there is way in objective-c I don't aware of (maybe an special use of delegate)!!
Subclass UILabel and override setText:
#implementation HALabel
- (void)setText:(NSString *)text {
[super setText:[text substituteCharactersOfText]];
}
#end
Related
I'm actually in big troubles on how to get the Text from a TextField (and optionally a TextView ).
I'll try to make it simple: I have my PostIt.xib which is composed of 2 labels (which I don't really care about) and also one TextField and one TextView. Here is how I tried to get the text from these:
First, in my PostIt.h :
#interface PostIt : UIView {
IBOutlet UITextField *titre;
IBOutlet UITextView *commentaire; }
Then secondly, in my PostIt.m : (the real action of this method is that it close a view and normally throw back the information I want to get to another view, here: parent )
-(IBAction)doubleTap:(UITapGestureRecognizer *)recognizer{
[_parent setTitre:titre.text];
[_parent setCommentaire:commentaire.text];
[_parent setIsEdited:true];
[self removeFromSuperview]; }
My problem here is, when I call a NSLog (for example) to show me the Strings which are caught (probably a mistake here? sorry) it show me every time : (null)
I have been looking and trying a lot of answer i found but no one seems to be able to solve my problem...
If someone could help me it will be really nice, thanks in advance :)
Is there any more code pertaining to the UITextField?
Based on what I see here you need you first convert the input from the UITextField
into a string and then you can set the string where you want.
Updated to add the conversion code,
NSString *stringFromTextField = [yourTextField text];
Here is some more details,
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITextField *yourTextField;
#end
Your Action,
- (IBAction)yourAction:(id)sender {
//Converting UItextfields into strings
NSString *stringFromTextField = [self.yourTextField text];
}
Here is a sample project I made for you on GitHub -
stringFromTextView
You should try to input to your code an object part if that's not already done, where you could directly pick up the data that you need and that's also from here that you should update your view.
Let's assume we have a custom lib with a class that inherits from UILabel:
//MyLibCustomLabel.h
#interface MyLibCustomLabel : UILabel
MyLibCustomLabel is linked to a UILabel in a .xib file, and text is filled in .xib.
This custom lib is integrated in a project that has a Category on UILabel class, which has a method to modify UIlabel's text
//UILabel+UILabelAdditions.h
#interface UILabel (UILabelAdditions)
//UILabel+UILabelAdditions.m
#implementation UILabel (UILabelAdditions)
- (void)awakeFromNib {
[super awakeFromNib];
[self prependText];
}
-(void)prependText {
NSString *newText = [NSString stringWithFormat:#"blabla + %#", self.text];
self.text = newText;
}
In the end, there is a modification non-desired in MyLibCustomLabel.
In a situation of both custom class and category are used in a Class, is there a way to protect MyLibCustomLabel from any Category on UILabel ?
So that MyLibCustomLabel can not be altered in an undesired way, and so that there is no modification to do in the project that integrates it.
Thanks for your help !
There is nothing that can be done to "protect" a class from a possible category being defined.
But please note that the example UILabel category you show isn't valid. A category must never attempt to override an existing method nor attempt to call a super method. Such behavior isn't defined and isn't guaranteed to work as hoped.
In other words, the category's awakeFromNib method is a bad idea and shouldn't be done. Such a thing should only be attempted in a base class, not a category.
I started creating all elements programmatically and I repeat most of the code for styling the fields (e.g. borderStyle, keyboardType, keyboardAppearance, spellCheckingType, etc.).
What is the most acceptable way of doing this from one place?
Analogue would be CSS - I would define the general styles in one place, and all generated elements would use them automatically.
Thanks!
If I kept wanting UITextFields with a certain setup, my preferred approach would be to add a factory method to UITextField:
#implementation UITextField (DHFactory)
+ (instancetype)dh_textField {
UITextField *textField = [[self alloc] init];
[textField setKeyboardAppearance:UIKeyboardAppearanceAlert];
[textField setSpellCheckingType:UITextSpellCheckingTypeNo];
return textField;
}
#end
I prefixed the category method name, following the advice in Appleās Programming with Objective-C.
This may sound silly, but read on...
I want to set the text of a UILabel from outside of a UIViewController that is instantiated by a storyboard. I need to make sure that the label property of the view controller is set when I set its text otherwise the label's text won't be set(because it won't be loaded yet to receive a text value).
Here's my current solution:
// Show pin entry
if (!self.pinViewController) {
// Load pin view controller
self.pinViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"pinScreen"];
self.pinViewController.delegate = self;
if (!self.pinViewController.view) {
// Wait for pin screen to fully load
}
[self.pinViewController setMessageText:#"Set a pin for this device"];
}
Initially I had a while loop that looped until the value of view was not nil, But it seems the very act of checking the view loads it(as mentioned here: http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006926-CH3-SW37)
I tried using the isViewLoaded method with no success. It just looped forever.
I've gone forward with the above code as my current solution, but it feels wrong.
Is there a better way ensure a UIView has loaded?
I want to propose an alternative way where you don't have to rely on the availability of the view.
If you need to wait for the view to load before you can call other methods on your viewController you break encapsulation, because the viewController that calls your PinViewController has to know about the inner workings of your PinViewController. That's usually not a good idea.
But you could save objects like NSStrings in the PinViewController instance, and when the view of the PinViewController will appear you set its views according to the properties you have set before.
If you need to change the text of an label from outside your viewController you can also create a custom setter that sets the label.text for you.
Your .h
#interface PinViewController : UIViewController
#property (copy, nonatomic) NSString *messageText;
// ...
#end
And your .m
#implementation PinViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.messageLabel.text = self.messageText;
}
// optional, if you want to change the message text from another viewController:
- (void)setMessageText:(NSString *)messageText {
_messageText = messageText;
self.messageLabel.text = messageText;
}
// ...
#end
viewDidLoad should solve this I guess.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIViewController_Class/Reference/Reference.html
I would rather see you change your logic and do it the way that #MatthiasBauch shows in his answer. However, to answer your actual question, you can simply set a view property in order to force it to load:
self.pinViewController.view.hidden = NO;
I have made an application in which user may generate as many UITextField as he/she wants and those will be automatically placed over a UIView. Functionality is that user may drag the any of the UITextField at any point of screen.Till this part every thing is quite working. Now if he wants to edit the UITextField he taps(2 times) on UITextField and edits.This part of mine is only working for the recent generated UITextField not for all. How can i do this? I fnay one wants my previous code i can post. Plese respond it sonn. Thanks in advance.
add textFieldArray to .h file then specify <UITextFieldDelegate> and
sythesize that array.
while creating the each textfield
specify delegate like this
mytextF1.delegate=self;
mytextF2.delegate=self;
.....
after that add all text field object to an textFieldArray which should be declared .h file and synthesize them .(dont forget to alloc and init this array in ur viewdidload).
self.textFieldArray=[[NSMutableArray alloc]init];
then add each text field to this array
[self.textFieldArray addObject:mytextF1];
[self.textFieldArray addObject:mytextF2];
.......
then use this delegate methode to update
- (void)textFieldDidBeginEditing:(UITextField *)textField{
for (UITextField *textF in self.textFieldArray) {
[textF setText:[textField text]];
}
}