UITextField Black Pixels at Edges - iphone

I've created a UITextField programmatically with the following code:
self._maxPriceField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, labelWidth, labelHeight)];
self._maxPriceField.borderStyle = UITextBorderStyleRoundedRect;
self._maxPriceField.clearButtonMode = UITextFieldViewModeWhileEditing;
self._maxPriceField.font = fieldFont;
self._maxPriceField.delegate = self;
The problem I'm having is that my UITextField ends up having these strange black pixels on the edges. This happens both on the device and in the simulator. You can see in the screenshot below:
When I create the same UITextField using IB, with the same specs and background, I have no problem. Unfortunately I need to create this UITextField programmatically.
Has anybody seen this before? What to do?

It seems this is the way textfields are drawn on the screen. I played around with your code a little bit, and if I set the text field height lower than about 20, you start to see a noticeable "shadow" that is clearly not drawn correctly.
My suggestion is to either use a height of 20 or higher for the text field, or to use a different style, such as Bezel or Line and set the background to white.
Here is a screenshot to demonstrate:
And here is the code I used to draw these:
int labelWidth = 100;
int labelHeight = 10;
_maxPriceField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, labelWidth, labelHeight)];
_maxPriceField.borderStyle = UITextBorderStyleRoundedRect;
_maxPriceField.clearButtonMode = UITextFieldViewModeWhileEditing;
//_maxPriceField.font = fieldFont;
//_maxPriceField.delegate = self;
[self.view addSubview:_maxPriceField];
UITextField *secondField = [[UITextField alloc] initWithFrame:CGRectMake(10, 40, labelWidth, labelHeight + 10)];
secondField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:secondField];
[secondField release];
UITextField *thirdField = [[UITextField alloc] initWithFrame:CGRectMake(10, 70, labelWidth, labelHeight + 20)];
thirdField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:thirdField];
[thirdField release];
UITextField *fourthField = [[UITextField alloc] initWithFrame:CGRectMake(10, 110, labelWidth, labelHeight + 30)];
fourthField.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:fourthField];
[fourthField release];
UITextField *noRoundFirst = [[UITextField alloc] initWithFrame:CGRectMake(10, 160, labelWidth, labelHeight)];
noRoundFirst.borderStyle = UITextBorderStyleBezel;
noRoundFirst.backgroundColor = [UIColor whiteColor];
[self.view addSubview:noRoundFirst];
[noRoundFirst release];
Hope this helps.
Mk

Related

UITextView auto-complete bubble location

I am confused regarding auto-complete bubble location while entering data in UITextView.
What i have observed is that depending upon its frame's origin , auto-complete bubble either comes on top or bottom. There is no fixed location, provided scrollEnabled is set to NO.
Here are the two links. Code is written in init()
http://www.flickr.com/photos/26021742#N00/6975525835/
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 150, 320, 41)] autorelease];
view.backgroundColor = [UIColor grayColor];
[self.view addSubview:view];
UITextView *growingTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 10, 200, 27)];
growingTextView.font = [UIFont fontWithName:#"Helvetica" size:13];
growingTextView.scrollEnabled = NO;
[view addSubview:growingTextView];
http://www.flickr.com/photos/26021742#N00/6975525727/
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 41)] autorelease];
view.backgroundColor = [UIColor grayColor];
[self.view addSubview:view];
UITextView *growingTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 10, 200, 27)];
growingTextView.font = [UIFont fontWithName:#"Helvetica" size:13];
growingTextView.scrollEnabled = NO;
[view addSubview:growingTextView];
Can anyone explain this observed behavior ???
I know that popovers and bubbles will always automatically adjust it's position for ease of use. It's already on Apple's code and in both images the bubble position makes sense.

ContainerView hide UILabel

I want to use UILabel in ContainerView.
So I am using this code for that.
UILabel *myLabel = [[[UILabel alloc] initWithFrame:CGRectMake(16, 60, 300, 150)] autorelease];
myLabel.numberOfLines = 0;
myLabel.font = [UIFont systemFontOfSize:13.5];
myLabel.text = [theQuiz objectAtIndex:row+3] ;
myLabel.lineBreakMode = UILineBreakModeWordWrap;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.layer.cornerRadius = 8.0;
[myLabel sizeToFit];
[self.view addSubview:myLabel];
//ContainerView
UIView *ChallengeView = [[UIView alloc] initWithFrame:CGRectMake(8, 55, 300, 10 + Challenge.frame.size.height)];
ChallengeView.layer.borderColor = [[UIColor purpleColor ] CGColor];
[ChallengeView setBackgroundColor:[UIColor whiteColor]];
ChallengeView.layer.cornerRadius = 8 ;
ChallengeView.layer.borderWidth = 1.5;
[self.view addSubview:ChallengeView];
[ChallengeView release];
Now problem is that when i set background color for ContainerView it hides the text of myLabel
Any Solution ??
What is happening is that you containerView is being added above label either you add label after containerView or do this:
[self.view bringSubviewToFront:myLabel];
You add the ChallengeView first , Then add the myLabel.
Otherwise u can do as like #xs2bush said,
[self.view bringSubviewToFront:myLabel];
Bcz the ChallengeView hides the label.

Change the Width of UISearchBars on a TableView

I am required to create two UISearchBars in my tableView. I want both of them of equal width on top of the table (side-by-side).
I have created two outlets of UISearchBar, and property and de alloc for them. I am finding it hard to place (I mean fit) both of them in the view. I get only one search bar expanding to the FULL width of the screen. How do I fit two ?
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 4, 45)];
zipSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(4, 0, 4, 45)];
searchBar.placeholder = #"School Name";
zipSearchBar.placeholder=#"Zip";
searchBar.delegate = self;
zipSearchBar.delegate = self;
self.tableView.tableHeaderView= zipSearchBar;
self.tableView.tableHeaderView= searchBar;
searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
You are simply reassigning it when you do self.tableView.tableHeaderView= searchBar;. You will have to build a view that contains both the search bars and then set it to the table's header view. Something like this,
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 200, 45)];
zipSearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(200, 0, 120, 45)];
searchBar.placeholder = #"School Name";
zipSearchBar.placeholder=#"Zip";
searchBar.delegate = self;
zipSearchBar.delegate = self;
UIView * comboView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
[comboView addSubview:searchBar];
[comboView addSubview:zipSearchBar];
self.tableView.tableHeaderView= comboView;
[comboView release];

Text Adjust to Fit in code?

Is there an equivalent of Adjust to Fit for UILabel text created dynamically in Xcode?
ie:
UILabel *someLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 35, 18)];
someLabel.font = [UIFont boldSystemFontOfSize:14];
someLabel.FOO_SIZETOFIT = YES; //FOO CODE
[someLabel release];
add this line
someLabel.adjustsFontSizeToFitWidth = YES;

How might I go about adding a label into a UIScrollView?

Gentle programmers, here for your inspection I present code that creates a scrolling view and populates it with buttons:
UIScrollView * testScroll =
[[UIScrollView alloc]initWithFrame:CGRectMake(10, 20, 80, 90)];
CGFloat y = 0;
for (int i = 0;i<5;i++) {
UIButton *mybut = [[UIButton alloc] initWithFrame:CGRectMake(0, y, 75, 25)];
NSString *title = [NSString stringWithFormat:#"button: %d",i]
[mybut setTitle:title forState:UIControlStateNormal];
[mybut addTarget:self action:#selector(Ok:)
forControlEvents:UIControlEventTouchUpInside];
y = y+35;
}
This is all well and good, for those that like scrolling lists of buttons. However, I wish to have a scrolling list of labels! How might I adjust this code to achieve this, my ultimate goal?
Well, if I could understand it right, you want something like that:
UIScrollView * testScroll =
[[UIScrollView alloc]initWithFrame:CGRectMake(10, 20, 80, 90)];
CGFloat y = 0;
for (int i = 0;i<5;i++) {
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, y, 75, 25)];
NSString *text = [NSString stringWithFormat:#"Label: %d",i]
[myLabel setText:title];
[testScroll addSubview:myLabel];
[myLabel release];
y = y+35;
}
Let me know if it is what you want.
Cheers,VFN
PS: Don't you prefer to use a table to do this, in the case you have many labels?