UITextField won't show clear button - cocoa-touch

I have a UITextField in which I want to display a clear button. The UITextField's cornerRadius has been modified and has no border. I have also added padding to the left and right of the text area. The following code leaves me with no clear button.
searchField.layer.cornerRadius = 15;
searchField.clearButtonMode = UITextFieldViewModeAlways;
UIView *padding = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 20)];
searchField.leftView = padding;
searchField.leftViewMode = UITextFieldViewModeAlways;
searchField.rightView = padding;
searchField.rightViewMode = UITextFieldViewModeAlways;
[padding release];
[searchField addTarget:self action:#selector(textFieldDidChange) forControlEvents:UIControlEventEditingChanged];
searchField.userInteractionEnabled = YES;
Does anyone know why I'm unable to get the button to show?
I'd appreciate any help.
Thanks!

Maybe you can try this
[textFieldAddress_ setLeftView:buttonBookmark_];
[textFieldAddress_ setLeftViewMode: UITextFieldViewModeAlways];
[textFieldAddress_ setRightView:buttonReloadAndCancel_];
[textFieldAddress_ setRightViewMode: UITextFieldViewModeUnlessEditing];
textFieldAddress_.clearButtonMode = UITextFieldViewModeWhileEditing;

I think just by giving as
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
you 'll get clear button while editing and setting any corner radius does not affect it.

Related

Resizing UITextView automatically

I'm a beginner in iOS development, and I have trouble resizing automatically a UITextView.
I created a Master-Detail Application. On my DetailViewController.xib I added a ScrollView that encompass a Label for the title, an image and under this image, a TextView in order to put a description that the user posted.
The problem is that the description depends on what my WebService returns, so I need it to be resized automatically.
Here's my code:
- (void)configureView
{
...
self.detailDescriptionTextView.text = [self.detailItem valueForKey:#"adDescription"];
CGRect frame = self.detailDescriptionTextView.frame;
frame.size.height = self.detailDescriptionTextView.contentSize.height;
self.detailDescriptionTextView.frame = frame;
)
The problem is that the TextView gets the content correctly but it doesn't resize at all.. I looked for an answer for a while and most of answers are what I tried...
Thanks for your help.
EDIT : I realized something. In order to put some elements under the description, I've put a very small TextView for the description in the Interface Builder, is it a problem? If it is, how could I put other elements under this TextView, because there would be no more space on the Interface Builder no?
EDIT 2 : I finally succeeded. It seems like the problem was that I created the TextView by Interface Builder. By creating it programmatically, it worked perfectly. Here's my code if it can help someone:
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 195, 280, 10)];
textView.text = [self.detailItem valueForKey:#"adDescription"];
textView.font = [UIFont fontWithName:#"Hevlatica" size:14];
[self.scrollView addSubview:textView];
CGRect descriptionFrame = textView.frame;
descriptionFrame.size.height = textView.contentSize.height;
textView.frame = descriptionFrame;
Thanks for your help.
First calculate the content height using
NSString *content = #"Hello how are you.";
CGSize size = [content sizeWithFont:[UIFont systemFontOfSize:14]
constrainedToSize:CGSizeMake(yourWidth, MAX_HEIGHT)
lineBreakMode:UILineBreakModeWordWrap];
and then set the frame of textView
[self.textView setFrame:CGRectMake(5, 30, 100, size.height + 10)];
Or
Add the content to your textview and then try this
CGRect frame = self.textView.frame;
frame.size.height = self.textView.contentSize.height;
self.textView.frame = frame;
Hope this Helps !!!
CGSize constraint = CGSizeMake(690.0, 2000.0);
int h=10;
CGSize size_txt_overview1 = [[self.detailItem valueForKey:#"adDescription"] sizeWithFont:[UIFont fontWithName:#"Helvetica" size:18] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
frame.size.height = size_txt_overview1.height;
self.detailDescriptionTextView.frame = frame;

title of a custom button

I am creating a custom UIButton like this :
U
IButton *buttonDetailCarte = [UIButton buttonWithType:UIButtonTypeCustom];
buttonDetailCarte.frame = CGRectMake(8, 223, 304, 44);
[buttonDetailCarte setBackgroundImage:[UIImage imageNamed:#"cellule.png"] forState:UIControlStateNormal];
buttonDetailCarte.titleLabel.text = #" my Text";
buttonDetailCarte.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:17];
buttonDetailCarte.titleLabel.textColor = [UIColor colorWithRed:84.0/255 green:84.0/255 blue:84.0/255 alpha:1.0f];
buttonDetailCarte.backgroundColor = [UIColor clearColor];
buttonDetailCarte.titleLabel.textAlignment = UITextAlignmentLeft;
the problem that my Text don't appear in my button and when i create the same button with IB, it appear ? what's the problem
You should use the setTitle:forControlState: method to change the button text, not manipulate the button's titleLabel directly.
Manipulating other properties of the titleLabel, as you are doing, is also no recommended. Try using the setTitleColor: instead. However, other properties like font of the titleLabel are fine to change. It's confusing at first but you get used to it!

How do I make my iAd banner appear at bottom of screen?

I have the following code that allows me to position it on the top. I am wanting it to appear at the bottom.
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, -50);
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[self.view addSubview:adView];
Any assistance will be appreciated.
Update—#larsacus pointed out this is for 4.2+:
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
CGRect adFrame = adView.frame;
adFrame.origin.y = self.view.frame.size.height-adView.frame.size.height;
adView.frame = adFrame;
[self.view addSubview:adView];
My first answer on SO :)
Why not use the 'center' property of the adView? If the dimensions of your main view are viewWidth and viewHeight:
//at the bottom of view,centered
adView.center=CGPointMake(viewWidth/2, viewHeight-adView.frame.size.height);
//at the top, right corner ;)
adView.center=CGPointMake(viewWidth-adView.frame.size.width/2, adView.frame.size.height);
Cheers
This is what helped me move the view to the bottom:
adFrame.origin.y = self.view.frame.size.height;
adView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
Using Storyboards, then, ignore that the standard iAd Banner View does not reach the edges of the screen.
Set the iAd banner to be centred, then move it to as close to the bottom as you want. No other width settings (no leading or trailing settings)
Mine looked ok with 8 pixels from the bottom of the superview, but I went with - pixels between the iAd banner and the UIController View.

UIkeyboard popup with uitextfield on top (pic included)

I am trying to achieve this type of effect where a barbutton is pressed and uikeyboard pops up and right above it brings a uitextfield with it. Please see image attached. Can someone point me to the right about how I can do this?
Thanks.
Use UIToolbar or UIActionSheet
Setup a textField as an inputAccessoryView for the keyboard.
UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
keyboardDoneButtonView.barStyle = UIBarStyleBlack;
keyboardDoneButtonView.translucent = YES;
keyboardDoneButtonView.tintColor = nil;
[keyboardDoneButtonView sizeToFit];
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(30, 10, 260, 30)];
[keyboardDoneButtonView setItems:[NSArray arrayWithObjects:textField, nil]];
masterTextField.inputAccessoryView = keyboardDoneButtonView;
// where masterTextField is the textField is the one you tap, and the keyboard rises up along with the small textField.
masterTextField.returnKeyType = UIReturnKeyDone;
[masterTextField setKeyboardType:UIKeyboardTypeDefault];
update:
For a button, put the above code in the (IBAction) of the button.
And instead of masterTextField, create a UIView and add it as a SubView to your view.

How to programmatically add text to a UIView

I have a UIView that I'd like to add several bits of text to. I have used a UITextView but I think that's overkill as it doesn't need to be editable. I thought about using a UILabel or a UITextField, but I don't see how you tell the superview where to position the UILabel or UITextField within itself. I want the lowest footprint object that will let me put text of a font/color/size of my choosing in my UIView where I want it. Not too much to ask, eh?
The simplest approach for you would be:
UILabel *yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
[yourLabel setTextColor:[UIColor blackColor]];
[yourLabel setBackgroundColor:[UIColor clearColor]];
[yourLabel setFont:[UIFont fontWithName: #"Trebuchet MS" size: 14.0f]];
[yourSuperView addSubview:yourLabel];
Building or populating Views in your code will probably require you to use CGRectMake a lot.
As its name says, it creates a rectangle that you can use to set the relative position (relative to the borders of your superview) and size of your UIView-Subclass (in this case a UILabel).
It works like this:
yourLabel.Frame = CGRectMake(x, y, width, height); //x,y,width,height are float values.
x defines the spacing between the left hand border of the superview and the beginning of the subview your about to add, same applies to y but relating to the spacing between top-border of your superview.
then width and height are self-explanatory i think.
Hope this gets you on the track.
Instead of finding a way to tell the view where to position the UILabel, you can tell the UILabel where to position itself in the view by using "center".
E.g.
myLabel.center = CGPointMake(0.0, 0.0);
Hope you'll be able to use UILabel, for me it's the basic form of a flexible non editable text.
For Swift:
let yourLabel = UILabel(frame: CGRectMake(100, 100, 100, 100))
yourLabel.textColor = UIColor.whiteColor()
yourLabel.backgroundColor = UIColor.blackColor()
yourLabel.text = "mylabel text"
yoursuperview.addSubview(yourLabel)
This question is old, but for a pure UIView text option without using UILabel or UITextField (as all the other answers describe, but the question is how to do it without them), drawRect in a subclassed UIView works for me. Like so:
- (void)drawRect:(CGRect)rect{
NSString *string = #"Hello World!";
[string drawAtPoint:CGPointMake(100, 100) withFont:[UIFont boldSystemFontOfSize:16.0]];
}
This routine displays a text at a X-Y position
-(void)placeText:(NSString *)theText:(int)theX:(int)theY {
UILabel *textLabel;
// Set font and calculate used space
UIFont *textFont = [UIFont fontWithName:#"Helvetica" size:14];
CGSize textStringSize = [theText sizeWithFont:textFont constrainedToSize:CGSizeMake(300,50) lineBreakMode:NSLineBreakByTruncatingTail];
// Position of the text
textLabel = [[UILabel alloc] initWithFrame:CGRectMake(theX+OFFSETIMAGEX-(textStringSize.width/2), theY+OFFSETIMAGEY-(textStringSize.height/2), textStringSize.width,textStringSize.height)];
// Set text attributes
textLabel.textColor = [UIColor blackColor];
textLabel.backgroundColor = [UIColor orangeColor];
textLabel.font = textFont;
textLabel.text = theText;
// Display text
[self.view addSubview:textLabel];
}
It might be late but here is what I use:-
CGRect labelFrame = CGRectMake(120,300, 530, 100);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];
//If you need to change the color
[myLabel setTextColor:[UIColor whiteColor]];
//If you need to change the system font
[myLabel setFont:[UIFont fontWithName:NULL size:23]];
//If you need alignment
[myLabel setTextAlignment:NSTextAlignmentCenter];
// The label will use an unlimited number of lines
[myLabel setNumberOfLines:0];
//Add label view to current view
[self.view addSubview:myLabel];
NSString *someString = #"Sample String, Yarp!";
myLabel.text = someString;
add a UILabel to your View. then override the View's layoutSubviews method.