Background transparancy of a textfield - iphone

I have an application that contains a textfield.
Is there a way to change background transparency of this textfield in code?

Yes you can do it.
You can set transparency by setting its alpha. You can pic color code with digital color meter ( its a inbuilt application in mac).
if you set alpha 1 then there will be no transparency. set transparency according to your need (set alpha less then 1).
Try this:
textfield.backgroundColor=[[UIColor colorWithRed:208.0/255.0 green:15.0/255.0 blue:202.0/255.0 alpha:0.6] CGColor];

You can set with below mentioned code in Xamarin
txtNumber.BackgroundColor = UIColor.White;
txtNumber.Alpha = 0.5f;
Reference 1: transparent UITextView
Reference 2: How do I create a transparent UITextField?
Similary:
txtNumber.BackgroundColor = UIColor.Clear;

use Custom UITextField
UITextField *yourTextField= [[UITextField alloc] initWithFrame:frame];
yourTextField.borderStyle = UITextBorderStyleNone;
[yourTextField setBackgroundColor:[UIColor clearColor]];

Just put bellow code for white background
[yourTextField setBackgroundColor:[UIColor whiteColor]];
or if want to clear Color then bellow code
[yourTextField setBackgroundColor:[UIColor clearColor]];
so simple
:)

Related

Change UILabel background color

my code is
UILabel *pTeamAlreadybl=[[UILabel alloc]initWithFrame:CGRectMake(110, 275,180,30)];
pTeamAlreadybl.text=#" Team already?";
pTeamAlreadybl.font=[UIFont boldSystemFontOfSize:16];
pTeamAlreadybl.textAlignment=UITextAlignmentLeft;
pTeamAlreadybl.textColor=[UIColor colorWithRed:61.0/255.0 green:61.0/255.0 blue:61.0/255.0 alpha:0.6];
pTeamAlreadybl.shadowColor = [UIColor colorWithWhite:1.0 alpha:0.7];
How can I change label's background color?
You miss
pTeamAlreadybl.backgroundColor=[UIColor anycolor];
write this code
To change the background color of a UILabel, you need to set the backgroundColor property of the UILabel object. You can do so by using the following line of code:
[pTeamAlreadybl setBackgroundColor:[UIColor colorOfYourChoice]];
OR
pTeamAlreadybl.backgroundColor = [UIColor colorOfYourChoice];
pTeamAlreadybl.backgroundColor = [UIColor ...];
Doesn't the above code work?
[pTeamAlreadybl setBackgroundColor:[UIColor YourColor]];
YourColor as mentioned below
blackColor;
darkGrayColor;
lightGrayColor
whiteColor;
grayColor;
redColor;
greenColor;
blueColor;
cyanColor;
yellowColor;
magentaColor;
orangeColor;
purpleColor;
brownColor;
clearColor;
you can use customized color as well like this
[UIColor colorWithRed:61.0/255.0 green:61.0/255.0 blue:61.0/255.0 alpha:0.6]

Setting the background of a UILabel (transparency problem with .png files)

I'm trying to set the background of a UILabel by setting its property backgroundColor.
But I have a problem: the backgroundColor is created from a .png file (with transparency) and the result is that the transparent part appear in black.
Any solution without the need of custom subclasses?
Update
I tried setting the UILabel property opaque to NO but the problem is still there.
It sounds like you might have forgotten to change the opaque property of the UILabel:
label.opaque = NO;
Alternatively, you can set this property in the InterfaceBuilder view of Xcode.
I had the same problem, and the solution is:
[yourLabel setBackgroundColor:[UIColor clearColor]];
Changing opaque or alpha value didn't work.
Maybe you can try this one:
[your_label setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"yourImage.png"]]];
You'll also need to save it as a png24 or 24bit png file - at least that's the case when saving from PhotoShop
[footer setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"Favourite_商品文字背景.png"]]];
[footer setOpaque:NO];
it must be work,goodLuck!
Change its opaque property to NO.
If it doesn't help, you can add imageView behing your label:
[yourLabel setOpaque:NO];
[yourLabel setBackgroundColor:[UIColor clearColor]];
UIImageView *labelBkg = [[UIImageView alloc] initWithFrame:yourLabel.frame];
[labelBkg setImage:[UIImage imageNamed:#"yourImageName.png"]];
[self.view insertSubview:labelBkg belowSubview:yourLabel];
[labelBkg release];

iPhone - UIView's backgroundColor - using a png with transparency?

I'm setting the background color of my UIView (which is within a UIViewController) as follows:
myView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"myTransparentBG.png"]];
I've even tried messing with [myView setOpaque:NO]; but the image doesn't appear transparent. It has a black background to it. Am I abel to fix this programmatically? Otherwise, how are we supposed to set a transparent background to our view?
This seems like a question that should have been asked before, but I couldn't find an answer.
When using a transparent pattern, after setting the backgroundColor property, you need to set opaque property to NO on both view and its layer. Here's a sample:
UIView* paperView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,700)];
paperView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"paper.png"]];
[paperView.layer setOpaque:NO];
paperView.opaque = NO;
I haven't ever actually done this, but here's what I'd try.
Set the UIView's .backgroundColor to [UIColor clearColor]. Then put a UIImageView containing your PNG as the "lowest" item on the UIView's stack of subviews. Make sure the UIImageView's background color is also clear.
Use this: myView.backgroundColor = [UIColor clearColor];
This will set it to a transparent background.

How can i make a more rounded UITextField? Like the search field in Safari on iPad

HI I'm trying to get a UITextField to look like the google search field available in the safari app on iPad. The purpose of the field will be the same (a search box).
I know i could use a UISearchBar but I would have to use hackish code to get rid of the magnifying glass icon and the background and I don't want that.
I'm attaching an image with the TextField used by apple as their search box. How can I modify an UITextField to look and behave like the search field in this screenshot?
I tried to modified the UITextField's layer roundedCorners property but this doesn't work as expected.
Any help is very much appreciated!
Thank you!
You can set the Corner-Radius on any UIView-Subclass.
Add the QuartzCore.framework to your project
add #import <QuartzCore/QuartzCore.h> in your UIViewController Subclass (where you have access to the UITextfield instance of your choice
within viewDidLoad / viewWillAppear (or any other place where your UITextfield is allready instantiated call [yourTextField.layer setCornerRadius:14.0f];
play around with the cornerRadius value until it looks good
Use the below code.It works for me:
searchField= [[UITextField alloc] initWithFrame:CGRectMake(0,0,150,31)];
searchField.delegate = self;
searchField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
searchField.textColor = [UIColor colorWithRed:102.0/255 green:102.0/255 blue:102.0/255 alpha:1.0];
searchField.textAlignment = UITextAlignmentLeft;
searchField.font = [UIFont fontWithName:#"Helvetica" size:15];
searchField.autocorrectionType = UITextAutocorrectionTypeNo;
searchField.contentVerticalAlignment =UIControlContentVerticalAlignmentCenter;
searchField.clearsOnBeginEditing = NO;
searchField.autocapitalizationType = UITextAutocapitalizationTypeNone;
searchField.autocorrectionType = UITextAutocorrectionTypeNo;
searchField.keyboardType = UIKeyboardTypeURL;
searchField.returnKeyType = UIReturnKeySearch;
searchField.clearButtonMode = UITextFieldViewModeWhileEditing;
searchField.rightViewMode = UITextFieldViewModeUnlessEditing;
searchField.layer.cornerRadius = 17;
searchField.placeholder=#"Google Search";
searchField.borderStyle = UITextBorderStyleRoundedRect;//To change borders to rounded
searchField.layer.borderWidth = 1.0f; //To hide the square corners
searchField.layer.borderColor = [[UIColor grayColor] CGColor]; //assigning the default border color
UIBarButtonItem *searchFieldItem = [[UIBarButtonItem alloc] initWithCustomView:searchField];
Set background property of UITextField to an appropriate image, like this one:
Why don't you try a trick with making separate image of the input field you need and placing it under the real textField control. However, you will have to manipulate the colors of the textField you're placing over in order to make it transparent.
Use a graphic with the rounded edges you want and then position a textview on top.

iPhone development: How to create colored or translucent background for UIActionSheet?

When you try deleting a note in iPhone's Notes application, an UIActionSheet pops up. The sheet is translucent (but not black translucent). How is that achieved? Is it possible to make the background of UIActionSheet a certain color?
I usually implement the following delegate method:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
Just to make a sample. In this case I use a stretched png as a background:
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
UIImage *theImage = [UIImage imageNamed:#"detail_menu_bg.png"];
theImage = [theImage stretchableImageWithLeftCapWidth:32 topCapHeight:32];
CGSize theSize = actionSheet.frame.size;
// draw the background image and replace layer content
UIGraphicsBeginImageContext(theSize);
[theImage drawInRect:CGRectMake(0, 0, theSize.width, theSize.height)];
theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[actionSheet layer] setContents:(id)theImage.CGImage];
}
and this is the result:
alt text http://grab.by/4yF1
You can use the code below:
actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackOpaque;
or
actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
actionSheetObj.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
It's not too difficult. You can use the following code:
CGSize mySize = myActionSheet.bounds.size;
CGRect myRect = CGRectMake(0, 0, mySize.width, mySize.height);
UIImageView *redView = [[[UIImageView alloc] initWithFrame:myRect] autorelease];
[redView setBackgroundColor:[UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:0.5]];
[myActionSheet insertSubview:redView atIndex:0];
Just make sure you present the UIActionSheet before doing this or the size wont be set. That makes it kind of ugly, but you could do something like:
[myActionSheet showInView:self.view];
if (!redAdded) {
redAdded = YES;
//THE ABOVE CODE GOES HERE
}
You can definitely adjust the opacity by setting the alpha value. Interface Builder lets you edit the value directly, but in code I think you would do:
[[myActionSheet view] setOpaque:NO];
[[myActionSheet view] setAlpha:0.5];
I'm not sure if you need the setOpaque call or not - I think that is used to help optimize performance, as the iPhone won't try to render anything hidden by the opaque view.
It looks black to me (note: using 2.2.1). The only reason there's a color to it is because of the yellow behind it.
One option would be to use the black transparent background and find out the size and speed of the action sheet. Then create a view and animate it in at the same time you show the action sheet, just underneath it, to give it a tint different than the color naturally behind the action sheet. You would have to make the color view also translucent so you could see behind that as well.
I'm not sure if you can, but you might also try adjusting the opacity of the action sheet itself as well.