String from Text Field - ios

I'm trying to get the title of a String from the content of a UITextField, so you can always edit it and save it.
I've already linked the String and the UITextField, but everything works just if you edit the content of the UITextField in the storyboard, and it doesn't work in the application (Simulator and Devices).
This is what I'd like to do:
const NSString *nameOfTheString = #"the content of the text field, that you can change when you want from the application"
Here there is the code that I have:
nameOfTheString = nameOfTheTextField.text;
I Hope You'll Help Me.
I'm using Xcode 6 (Objective-C)

You can't change nameOfTheString if it is a const (constant). If you want to change nameOfTheString, simply use NSString *nameOfTheString; to declare it.

Remove the const from the declaration (const means it can't be changed):
NSString *nameOfTheString;
Then use a delegate method of UITextField to update the value of the string whenever it is changed:
- (void)textFieldDidEndEditing:(UITextField *)textField {
nameOfTheString = textField.text;
}
UITextField delegate ref:
https://developer.apple.com/library/ios/documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

Related

UITextfield format ****user

I want to add masked and unmasked characters, both together in UITextfield.
e.g I have a UITextField called UserName "TestUser". Now I want to display username in the UITextField like "****User". Is it possible to add both text together? I want to do this thing while entering character in UITextfield.
It might be worth your while to look at the text field delegate method:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
It is called
"whenever the user types a new character in the text field or deletes an existing character"
So you can set the range 0-3 or whatever you like to "*" but you should hold the first characters somewhere else, which I believe to be possible before returning yes, with the textField parameter provided. I don't know if you tried this already or not and I haven't tried this myself, but let me know how it goes.
something I figure out and made below solution but still it is not exactly that I want. I want to mask/asterisks first four characters while entering values in UITextField. But here I am doing this thing on textFieldShouldReturn method.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// Store orignal string - we can use later
_strOriginalString = textField.text;
//Make temporary Masked String
NSRange replacementRange = NSMakeRange(0, 4);
_strMaskString = [NSMutableString new];
for (int i = 0; i<4; i++) {
[_strMaskString appendString:#"●"];
}
//Replace Orignal String's Range with Masked string
self.txtUsername.text = [_strOriginalString stringByReplacingCharactersInRange:replacementRange withString:_strMaskString];
NSLog(#"Textfield string - %#",self.txtUsername.text);
NSLog(#"orignal string - %#",_strOriginalString);
NSLog(#"masked string - %#",_strMaskString);
return NO;
}
I don't think, that this is possible. Why do you want to show the name like "****User" in the UITextfield? You can use a label and mask the first characters with *

How to identify UItextFields without using tag.

Im creating different UITextField for different type of input(date,picker etc).
Im already using tag to identify different textFields of each type.
how can i identify textfield without tag.
Can i assign string identifier to textFields?
You can add CATEGORY, Create any string method like stringTag etc. You problem is solved.
Or
You can use UITextField's attributedText, placeholder properties to store String Tag for identification. But make sure HIDE them after assigning "String TAG"
This is not recommended solution but it can help.
I think you can only identify them by using tags.
Maybe you can use 1000, 2000, 3000 for types, and 1, 2, 3, for identifying.
So tag 1001 is the first in category 1000, 1100 is the 100th in category 1000 etc...
You can also compare UITextField with their outlet if you have created. You can assign string identifier to textFields as bellow
#import <objc/runtime.h>
static char kTextFieldAssociatedKey;
objc_setAssociatedObject(yourTextField,
&kTextFieldAssociatedKey,
aStrKey,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSString *aStrKey = objc_getAssociatedObject(textField, &kTextFieldAssociatedKey);
objc_removeAssociatedObjects(textField);
NSLog(#"%#", aStrKey);
return [textField resignFirstResponder];
}
Another way to identify textfield without Tag is
NSArray *arySubViews=[self.view subviews];
for (int i=0;i<arySubViews.count; i++){
id subObj= [arySubViews objectAtIndex:i];
if ([subObj isKindOfClass:[UITextField class]]) {
/// At this place you identify your Text Field without tag
// you can identify through Title or etc.
UITextField *tfObj=(UITextField*)subObj;
}
}
Source code as image

ios check if text is too big for uiTextfield

This question is not about UITextView: It's about UITextField.
How do I check if my text would be too long for my UITextField? I need to dynamically change the Height of my UITextField so that it accepts multiple lines. Is this possible or must I use a UITextView for this? I prefer UITextField because of the convenience of a placeholder text.
You can use
sizeWithFont:
of NSString to avaluate the length of a string.
UITextField is not able to show multiple lines. For solutions take a look at
Objective C: How to create a multi-line UITextField?
Claus
Hi just use small trick for this code sample below.
+ (CGFloat)expectedHeightWithData:(MECommentData *)data
{
#autoreleasepool
{
NSString *comment = data.comment;
UIFont *commentSizeFont = [UIFont fontWithName:#"Miso-Bold" size: 15.0];
CGSize commentSize = [NSString sizeWithFont:commentSizeFont width:225 heitght:3000 text:comment];
return ceilf(commentSize.height);
}
}

Share image in iMessage using Custom Keyboard in iOS 8

I am working on iOS 8 custom keyboard, where i have designed keyboard using some images like smiley. i want this keyboard to be work with iMessage. when i am trying to send text its working properly but can't able to share image there. I have tried following code :
To share text : (its working properly)
-(void)shouldAddCharector:(NSString*)Charector{
if ([Charector isEqualToString:#"Clear"]) {
[self.textDocumentProxy deleteBackward];
} else if([Charector isEqualToString:#"Dismiss"]){
[self dismissKeyboard];
} else {
[self.textDocumentProxy insertText:Charector];
}
}
To add image : ( Not working)
-(void)shouldAddImage:(UIImage*)oneImage
{
UIImage* onions = [UIImage imageNamed:#"0.png"];
NSMutableAttributedString *mas;
NSTextAttachment* onionatt = [NSTextAttachment new];
onionatt.image = onions;
onionatt.bounds = CGRectMake(0,-5,onions.size.width,onions.size.height);
NSAttributedString* onionattchar = [NSAttributedString attributedStringWithAttachment:onionatt];
NSRange r = [[mas string] rangeOfString:#"Onions"];
[mas insertAttributedString:onionattchar atIndex:(r.location + r.length)];
NSString *string =[NSString stringWithFormat:#"%#",mas];
[self.textDocumentProxy insertText:string];
}
Is there any possibility to pass image to [self.textDocumentProxy insertText:string];
following attached image shows how exactly i want to use this image keyboard. i am surprised how emoji keyboard will work?
As far as I know, the behavior you are looking for is not possible as of iOS 8 beta 4.
Currently, the only way for iOS custom keyboards to interact with text is through <UITextDocumentProxy> and the only way to insert anything is via the insertText: method.
Here is the header for the insertText: method in <UITextDocumentProxy>:
- (void)insertText:(NSString *)text;
As you can see, it takes a plain NSString... not an NSAttributedString. This is why your attempt to insert an image doesn't work.
However, despite the fact that you can't add pictures, it is still very possible to insert emojis, since an emoticon is really just a Unicode character.
To add an emoji, just insert the proper Unicode character:
[self.textDocumentProxy insertText:#"\U0001F603"];
Useful links:
List of Unicode Emoji: http://apps.timwhitlock.info/emoji/tables/unicode
Unicode Characters as NSStrings: Writing a unicode character with NSString

Add url to OHAttributedLabel but shouldFollowLink delegate is not called

I add URL to attributed string in OHAttributedLabel ("forgot password" link to www.xxx.com ") . Everything looks perfect except that when I clicked on that string , nothing happen.
It should open that URL in safari. And then when I tried to add delegate method.
-(BOOL)attributedLabel:(OHAttributedLabel*)attributedLabel shouldFollowLink:(NSTextCheckingResult*)linkInfo
This delegate method is not called at all. The delegate is set properly because I tried another delegate method and it is working
-(UIColor*)attributedLabel:(OHAttributedLabel*)attributedLabel colorForLink:(NSTextCheckingResult*)linkInfo underlineStyle:(int32_t*)underlineStyle;
I setup UILabel via Interface builder and change its class to OHAtrributedLabel. And here is my code
//forgotLabel.text is "Forgot Password"
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:forgotLabel.text];
[attr setLink:[NSURL URLWithString:#"http://www.yahoo.com"] range:NSMakeRange(0, attr.length)];
//forgotLabel is OHAttributedLabel
forgotLabel.attributedText = attr
forgotLabel.delegate = self;
Can somebody help ? I don't want to use TTTAttributedLabel because in my project I used OHAttributedLabel in tablecell and it work perfectly. So I don't want to add another 3rd part compnonent that does almost exactly the same thing .
Thanks in advance.
I solved this myself by adding
forgotLabel.catchTouchesOnLinksOnTouchBegan = YES;

Resources