I am developing an app for my company but i am no expert on obj-C programming language. I've tried to find some answers on the internet but no success at all. The solution to the problem may be simple, but i am unable to solve it.
I want to limit the text so it fits inside the text box. What is happening right now is as i start typing and it flows on the first line forever and do not change drop to the second line when it hits the box boundaries.
otherdetails = [[UITextField alloc] initWithFrame:otherdetailsf];
otherdetails.text = otherdetailstxt;
[otherdetails.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[otherdetails.layer setBorderWidth:2.0];
otherdetails.layer.cornerRadius = 5;
otherdetails.clipsToBounds = YES;
otherdetails.delegate = self;
otherdetails.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
otherdetails.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
Thats what happens when i type a lot on the text field....
How can i solve this problem?
Thanks!!
Use TextViewinstead of text field if you need to support multiple lines of text whenever the text reaches the end of a line.
From Documentation:
The UITextView class implements the behavior for a scrollable,
multiline text region. The class supports the display of text using
custom style information and also supports text editing. You typically
use a text view to display multiple lines of text, such as when
displaying the body of a large text document.
So your code should be:
otherDetails=[[UITextView alloc]initWithFrame:otherdetailsf];
otherDetails.text=#"";
[otherDetails.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[otherDetails.layer setBorderWidth:2.0];
otherDetails.layer.cornerRadius = 5;
otherDetails.clipsToBounds = YES;
otherDetails.delegate = self;
// otherDetails.contentMode=UIControlContentVerticalAlignmentTop;
try this code:
in viewcontroller.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UITextViewDelegate>
#property (strong,nonatomic)IBOutlet UITextView *mytextview;
#end
in viewcontroller.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize mytextview;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)addTextView
{
[mytextview setText:#"Lorem ipsum dolor sit er elit lamet"];
mytextview.delegate = self;
[self.view addSubview:mytextview];
mytextview.delegate = self;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:
(NSRange)range replacementText:(NSString *)text{
if ([text isEqualToString:#"\n"]) {
[textView resignFirstResponder];
}
return YES;}
-(void)textViewDidBeginEditing:(UITextView *)textView{
NSLog(#"Did begin editing");
}
-(void)textViewDidChange:(UITextView *)textView{
NSLog(#"Did Change");
}
-(void)textViewDidEndEditing:(UITextView *)textView{
NSLog(#"Did End editing");
}
-(BOOL)textViewShouldEndEditing:(UITextView *)textView{
[textView resignFirstResponder];
return YES;
}
#end
Related
I am using UISearchBar for search purposes using Objective C. I need the placeholder text of search bar to be aligned to the center of the search bar programatically and when user starts typing, text must be aligned left.
Thanks in Advance
I had a same problem like yours before. I couldn't find any workaround and after all I just used UILabel and UISearchBarDelegate methods.
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UISearchBarDelegate>
#end
ViewController.m:
#import "ViewController.h"
#interface ViewController () {
UISearchBar *srchBar;
UILabel *customPlaceHolderForSearchBar;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CGRect frm = CGRectMake(0, 0, self.view.frame.size.width, 80);
srchBar = [[UISearchBar alloc] initWithFrame:frm];
srchBar.delegate = self;
[self.view addSubview:srchBar];
customPlaceHolderForSearchBar = [[UILabel alloc] initWithFrame:frm];
customPlaceHolderForSearchBar.text = #"PlaceHolder text";
customPlaceHolderForSearchBar.textColor = [UIColor grayColor];
customPlaceHolderForSearchBar.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:customPlaceHolderForSearchBar];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
customPlaceHolderForSearchBar.hidden = YES;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
if (searchBar.text.length < 1) {
customPlaceHolderForSearchBar.hidden = NO;
}
}
#end
I want to detect which character is removed on back space click event on keyboard in UITextView.
So if anybody knows solution please help.
Thanks in advance.
you should implement the protocol UITextViewDelegate method
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
and just get the text in range
try this to have a log (it's empty in case it's not a replacement but a new input)
- (BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSLog(#"deleting this string: |%#|", [textView.text substringWithRange:range]);
return YES;
}
ps
remember to set your class as the delegate of your UITextView
Here's what you want. This method detects whenever a character is removed, and prints it to the console.
#interface ViewController ()<UITextFieldDelegate>
{
UITextField *textField;
NSString *currentText;
}
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, self.view.frame.size.width-40, 30)];
[textField addTarget:self action:#selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
textField.delegate = self;
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:textField];
}
-(void)textChanged:(UITextField *)sender{
if (!currentText) {
currentText = sender.text;
}
if (![currentText isEqualToString:sender.text]) {
//The text that is in the textField at the moment is shorter than it was the last time the textfield was editted... This shows that a backspace was pressed
if (currentText.length > sender.text.length) {
NSLog(#"Character: %#", [currentText substringFromIndex:sender.text.length]);
}
}
currentText = sender.text;
}
#end
In my app I want to programmatically add textfield below another if required on click of a button. I already had provided two textFields. if a user want to add another textfield he can do so by clicking a button. I have already written code to obtain the textfield but the problem is that it overlaps the already designed textFields. How can I do it?
Is there any way through which I can get the x and Y co-ordinates of already designed textfield so that I can place new textField relative to those co-ordinates.
This code add textField to view dynamically when every click action on button
ExampleViewController.h
#import <UIKit/UIKit.h>
#interface ExampleViewController :UIViewController<UITextFieldDelegate>
#property int positionY;
#property int fieldCount;
#property (strong,nonatomic) UIScrollView *scroll;
#end
ExampleViewController.m
#import "ExampleViewController.h"
#interface ExampleViewController ()
#end
#implementation ExampleViewController
#synthesize positionY;
#synthesize fieldCount;
#synthesize scroll;
- (void)viewDidLoad {
[super viewDidLoad];
scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.backgroundColor = [UIColor whiteColor];
[self.view addSubview:scroll];
UIButton *clickToCreateTextField = [[UIButton alloc] initWithFrame:CGRectMake(40, 80, self.view.frame.size.width-80, 75)];
[clickToCreateTextField setTitle:#"Create Text Field" forState:UIControlStateNormal];
[clickToCreateTextField addTarget:self action:#selector(clickedButton) forControlEvents:UIControlEventTouchUpInside];
[clickToCreateTextField setBackgroundColor:[UIColor blackColor]];
[scroll addSubview:clickToCreateTextField];
positionY = clickToCreateTextField.center.y;
fieldCount = 0;
// Do any additional setup after loading the view.
}
-(void) clickedButton{
//add text field programmitacally
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(40, positionY, self.view.frame.size.width-80, 75)];
textField.delegate = self;
//give a tag to determine the which textField tapped
textField.tag = fieldCount;
textField.placeholder = [NSString stringWithFormat:#"Your dynamically created textField: %d", fieldCount ];
[scroll addSubview:textField];
//check if the textFields bigger than view size set scroll size and offset
if (positionY>= self.view.frame.size.height) {
scroll.contentOffset = CGPointMake(0, positionY);
scroll.contentSize = CGSizeMake(scroll.frame.size.width, scroll.frame.size.height+positionY);
}
fieldCount++;
//increase the position with a blank place
positionY = positionY+textField.frame.size.height+20;
}
#pragma mark TextField Delegate Methods
-(void) textFieldDidBeginEditing:(UITextField *)textField{
//Do what ever you want
}
-(void) textFieldDidEndEditing:(UITextField *)textField{
[textField resignFirstResponder];
//do anything
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
You can do any additional changes on this code.
I think this example explain your answer.
Hope it helps.
Use a counter and calculate y like this counter*texfield.frame.size.height.
I have used the below code to display a colored placeholder within iOS Textfield. This works fine in iOS 5 and 6. But in iOS 7 a line appears in the middle of the Textfield.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (void) drawPlaceholderInRect:(CGRect)rect {
[SEARCH_FIELD_FONT_COLOR setFill];
if (self.placeholderFont) {
[[self placeholder] drawInRect:rect withFont:SEARCH_FIELD_FONT];
}else {
[[self placeholder] drawInRect:rect withFont:self.placeholderFont];
}
}
#end
Try with following code for change color of placeholder of textFiled.
[self.myTextField setValue:[UIColor darkGrayColor] forKeyPath:#"_placeholderLabel.textColor"];
This could work a bit better. Also, you can change the font using and attributed placeholder.
- (void) drawPlaceholderInRect:(CGRect)rect {
[SEARCH_FIELD_FONT_COLOR setFill];
[super drawPlaceholderInRect:rect];
}
I have a custom view, i want display a keyboard as my input and intercommunicate with it.
thanks guys~
In your custom view you must add a zero-sized UITextField and then set your view as its delegate. Then in your custom view define a gesture recognizer (e.g. a single tap) and in the gesture recognizer recognition event set the text field as first responder.
As soon as you tap the view the keyboard will popup. Then write the delegate method:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
to manage the interaction with the typed string. Don't forget to call setNeedsDisplay if you need to update your view on the user keyboard typing.
The example below works. You must instantiate the custom view in your view controller of course. With the example, just type a letter in the keyboard and it will be displayed on the view.
//
// MyView.h
//
#import
#interface MyView : UIView {
UITextField *tf;
}
#property (nonatomic,copy) NSString *typed;
#end
//
// MyView.m
//
#import "MyView.h"
#implementation MyView
#synthesize typed;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor=[UIColor redColor];
tf = [[UITextField alloc] initWithFrame:CGRectZero];
tf.delegate=self;
[self addSubview:tf];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
[self addGestureRecognizer:tap];
}
return self;
}
// Only override drawRect: if you perform custom drawing.
- (void)drawRect:(CGRect)rect
{
// Draw the "typed" string in the view
[[UIColor blackColor] setStroke];
if(self.typed) {
[self.typed drawAtPoint:CGPointZero withFont:[UIFont systemFontOfSize:50]];
}
}
-(void)tap:(UIGestureRecognizer *)rec {
if(rec.state==UIGestureRecognizerStateEnded) {
[tf becomeFirstResponder];
}
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
self.typed=[string substringToIndex:1]; // extract the first character of the string
[self setNeedsDisplay]; // force view redraw
}
#end
I think the solution could be as simple as implementing the UIKeyInput protocol in your class.
Read more about the UIKeyInput Protocol Reference.