How to set the cursor staring position in UITextField [duplicate] - iphone

This question already has answers here:
UITextField set cursor to start text position
(3 answers)
Closed 9 years ago.
I am developing one application.In that i set the text filed border width as 1o base don my requirement.So now the first character of text filed will be hidden by border color.SO please tell me how can i set the cursor staring position for UITextField.

add UIview with border and add UITextField inside UIView
UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0,0, frame.size.width, frame.size.height)];
[baseView setBackgroundColor:[UIColor clearColor]];
[baseView setUserInteractionEnabled:YES];
[baseView setClipsToBounds:NO];
baseView.layer.borderColor =[UIColor lightGrayColor].CGColor;// UIColorFromRGB(0Xcccccc).CGColor;
baseView.layer.borderWidth = 1.0f;
[self addSubview:baseView];
UITextField* txtField = [[UITextField alloc]initWithFrame:CGRectMake(1, 1, frame.size.width-2, frame.size.height-2)];
[txtField setBackgroundColor:[UIColor whiteColor]];
[baseView addSubview:txtField];

Make a subclass of UITextField and use that.
Override these methods in your subclass. They will add space from left side in your textfield.
// placeholder text reposition
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectInset(bounds, 10, 0);
}
// text reposition
- (CGRect)editingRectForBounds:(CGRect)bounds {
return CGRectInset(bounds, 10, 0);
}

Related

Is there anyway to create a UITableView with one universal footer?

I'm trying to create a UITableView with multiple sections. Each section has it's own header, but I am trying to make a universal footer for the entire table view that stays in one position...
Is this logic possible using the UITableViewDelegate methods? Or should I create a custom view and just try and add it as a subview to my table view? The footer I currently have contains a UIButton.
If anyone has some sample code that would be great.
Edit: This question is not the same as the one referenced. I am trying to make a universal footer that floats above the UITableView. The other question does not specify the location of the footer, only that a footer is desired.
read about UITableView's tableFooterView property.
And now some code: (using ARC)
UILabel *footer = [UILabel alloc] init];
footer.text = #"Some text" ;
footer.backgroundColor = [UIColor greenColor];
self.tableView.tableFooterView = footer;
Now at the bottom of entire tableview there is a UILabel that is green with some text.
I ended up creating a subview and adding my button to that view. I then made that view my "footer".
Here is the code that gave me the desired results.
- (void)viewDidLoad
{
[super viewDidLoad];
//self.tableView.delegate = self;
//self.tableView.dataSource = self;
//save current tableview, then replace view with a regular uiview
self.tableView = (UITableView*)self.view;
UIView *replacementView = [[UIView alloc] initWithFrame:self.tableView.frame];
self.view = replacementView;
[self.view addSubview:self.tableView];
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 370, 320, 45)];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//button.userInteractionEnabled = YES;
//the button should be as big as a table view cell
//width of the button can be set but the width of the view it is added to will always match the width of the tableView
[button setFrame:CGRectMake(60, 0, 200, 45)];
//set title, font size and font color
[button setTitle:#"Build" forState:UIControlStateNormal];
[button.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//set action of the button
[button addTarget:self action:#selector(buildThenSegue)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[footerView addSubview:button];
footerView.userInteractionEnabled = YES;
[self.view addSubview:footerView];
self.tableView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
}
Take note that this is in a subclass of UITableViewController.
I referenced this answer to another question: https://stackoverflow.com/a/9084267/1091868

UITextView is not scrolling

I want to display one paragraph in UITextView. I'm able to put the content of paragraph in Text view, but I'm unable to scroll UITextView (Some of the content is not visible as it's not scrolling).
What I'm trying to do is:
CGRect frame = CGRectMake(0, 0, 320, 480);
//allocate view
self.view = [[UIView alloc]initWithFrame:frame];textview = [[UITextView alloc]initWithFrame:CGRectMake(30, 200, 275, 400)];
[textview setFont:[UIFont fontWithName:#"Arial" size:12]];
[textview setScrollEnabled:YES];
[textview setUserInteractionEnabled:NO];
[textview setBackgroundColor:[UIColor clearColor]];
//[textview setText:#"Hi,I'm working fine with this space"];
[self.view addSubview:textview];
In this case, scrolling is enabled. Can any one tell me why it's not scrolling?
set [textview setUserInteractionEnabled:TRUE];and I think you dont want to keyboard to pop up then you can hide the keyboard by implementing the uitextview delegate methode -(void)textViewDidBeginEditing:(UITextView *)textView{[textView resignFirstResponder];
set YES for setUserInteractionEnabled property of UITextView
set this line
[textview setUserInteractionEnabled:YES];
instead of
[textview setUserInteractionEnabled:NO];
I have a ViewController that only has a text view with about 5 pages of text.
Here is what I did in Swift 4.0:
Very important: Make sure you have auto layout set on the text
view. In my ViewController, I set my constraints to the text view
to 8,8,8,8 with constrain to margins checked. The whole thing does
not scroll if auto layout is not set.
This is the code I used:
Swift 4.0
textForPrivacyPolicy.showsVerticalScrollIndicator = true
textForPrivacyPolicy.isEditable = false
textForPrivacyPolicy.isScrollEnabled = true
textForPrivacyPolicy.scrollRangeToVisible(NSMakeRange(0, 0))
textForPrivacyPolicy.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
Paste this code
CGRect frame = CGRectMake(0, 0, 320, 480);
//allocate view
self.view = [[UIView alloc]initWithFrame:frame];textview = [[UITextView alloc]initWithFrame:CGRectMake(30, 200, 275, 400)];
[textview setFont:[UIFont fontWithName:#"Arial" size:12]];
[textview setScrollEnabled:YES];
[textview setUserInteractionEnabled:YES];
[textview setBackgroundColor:[UIColor clearColor]];
//[textview setText:#"Hi,I'm working fine with this space"];
[self.view addSubview:textview];
Add this line : [textview setUserInteractionEnabled:YES]; in place of this:
[textview setUserInteractionEnabled:NO];
Set UserInteractionEnabled as YES.
I had the same issue and I had setscrollable and userinteractionenabled to TRUE. It turned out to be a problem with Constraints. In order to fix this, click on your view controller in ur storyboard (the battery icon on top right), then go to "Editor"->"Resolve Auto Layout Issues"->"Add Missing Constraints In ". Fixed the issue for me!
bapi rout , There is a property for the user interaction with textView. You have set it to No. If you want to perform scroll on TextView, You have to enable this property.
[textview setUserInteractionEnabled:YES];
This will help you.

UITextView with a heigh of 15, Only top of the text visible, bottom half getting clipped

My UITextView has only 1 line of text. But the bottom half of the text is being cut off, the text seems to be drawn to low, or the Text View seems to have some height that it needs to be which is larger than the 15 i am setting it too.
UIFont *font = [UIFont fontWithName:#"Marker Felt" size:13];
CGSize size = CGSizeMake(310, 1000.f);
CGSize stringSize = [[messagesArray objectAtIndex:indexPath.row]
sizeWithFont:font constrainedToSize:size];
NSLog(#"txtView height: %d",(int)stringSize.height);
UITextView *txtview = [[UITextView alloc] initWithFrame:CGRectMake(5, 0,
310, (int)stringSize.height)];
txtview.autoresizesSubviews = NO;
[txtview setBackgroundColor:[UIColor blueColor]];
[txtview setFont:[UIFont fontWithName:#"Marker Felt" size:13]];
txtview.textColor = [UIColor whiteColor];
[txtview setOpaque:NO];
txtview.editable = NO;
txtview.text = [messagesArray objectAtIndex:indexPath.row];
[cell addSubview:txtview];
This line
NSLog(#"txtView height: %d",(int)stringSize.height);
Always prints out the value of 15. The Font is off size 13, so I would have expected the text to be positioned vertically centered in the UITextView with 1 pixel free at the top and the bottom. 15-13 etc.
Is there something that I am completely missing regards to setting up UITextFields?
Many Thanks,
Code
There is a default padding on UITextView. Try this:
[txtView setContentInset:UIEdgeInsetsMake(-8,-8,0,0)];
Maybe you need to play with the values :)
The problem is that by default UITextView adds about 32 to the bottom as the contentInset. You can get around this easily by subclassing the UITextView and overriding the contentInset method to return what you want like so:
#interface EditItemText : UITextView {
}
#end
#implementation EditItemText
// override the contentInset method so the scrolling behaviour of the text view is normal
- (UIEdgeInsets) contentInset {
//I am padding with 4 but you could return UIEdgeInsetsZero;
return UIEdgeInsetsMake (4,4,4,4);
}
#end
Then instead of a UITextView you would use your new subclass:
EditItem *vwTxt = [[[EditItemText alloc] initWithFrame:CGRectMake(10, 8.0, 170, 70.0)] autorelease];

SetCenter problem for an UILabel

OK, I'm struggling to make this work but without success.
Basically I want to add a UILabel to an UIView and center it.
The code looks like this:
UIView *customTitleView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 44.0)];
[customTitleView setBackgroundColor:[UIColor clearColor]];
// Screen title
CGSize constraint = CGSizeMake(200.0, 44.0f);
CGSize size = [screenTitle sizeWithFont:[UIFont boldSystemFontOfSize:20.0]
constrainedToSize:constraint
lineBreakMode:UILineBreakModeCharacterWrap];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, size.width, size.height)];
[titleLabel setText:screenTitle];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20.0]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
titleLabel.center = customTitleView.center;
[customTitleView addSubview:titleLabel];
[titleLabel release];
self.navigationItem.titleView = customTitleView;
[customTitleView release];
I expected that the UILabel would be centered within the UIView.
Well, it isn't. It is somehow right aligned and not even close to the center of the UIView.
What am I doing wrong?
So here's what's going wrong:
self.navigationItem.titleView = customTitleView;
When you set customTitleView as the title view of the navigation item the frame of customTitleView will change. customTitleView won't be 320 pixels wide anymore, but the frame of titleLabel will remain the same. Thus it will no longer be centered.
Can't you just set titleLabel as titleView? I think it should be centered automatically. If not, I know a more complicated way to solve it. Drop a comment and I'll tell you how.
EDIT: So here is how you realign the titleLabel after customTitleView's frame has changed.
When the frame of a view is changed layoutSubviews is called on that view. So instead of letting customTitleView be a regular UIView you need to make it a custom view that inherits from UIView. In that custom view you override layoutSubviews and in your implementation of layoutSubviews you make sure everything is aligned the way you want based on the new frame (self.frame). I'll leave the implementation to you.

UIDate Picker background color [duplicate]

This question already has answers here:
How to change UIPicker Color ? iPhone
(6 answers)
Closed 8 years ago.
HI I would like to change the background color of the UIDatePicker from the current glossy image to clear just like in this example
(source: apple.com)
Any ideas how to do that? I tried
datepicker.backgroundColor = [UIColor clearColor];
but it didnt work.
UIDatePicker contains one subview. That subview, in turn, contains 15 other subviews that form UIDatePicker's appearance. The first and the last of them are the background, so to make it transparent just turn them off and set background color of main subview to clearColor:
// create the picker
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 320)];
// add it to some view first
[actionSheet addSubview:datePicker];
[datePicker release];
// clear the background of the main subview
UIView *view = [[datePicker subviews] objectAtIndex:0];
[view setBackgroundColor:[UIColor clearColor]];
// hide the first and the last subviews
[[[view subviews] objectAtIndex:0] setHidden:YES];
[[[view subviews] lastObject] setHidden:YES];