I have the following code to create a cell's background view as a rounded rectangle:
- (UIView *)createBackgroundView
{
CGRect background = CGRectMake(50, 25, self.frame.size.width - 60, 90);
UIView *backgroundView = [[UIView alloc] initWithFrame:background];
backgroundView.backgroundColor = [UIColor colorWithRed:0.78 green:0.96 blue:0.39 alpha:1];
[backgroundView.layer setCornerRadius:7.0f];
[backgroundView.layer setMasksToBounds:YES];
return backgroundView;
}
Later on in the program I want to update the backgroundView's backgroundColor, I've tried
cell.backgroundView.backgroundColor = [UIColor redColor];
in both cellForRowAtIndexPath and willDisplayCell and neither seems to update it.
The way you have to go about it is:
[cell setBackgroundView:[self createBackgroundView]];
Probably you should also add an argument to the method:
- (UIView *)createBackgroundView:(UIColor *)color;
I am trying to make a section footer of a UITableViewCell transparent and this is what I am doing right now:
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIColor *background = [UIColor clearColor];
UIImageView *imageView = [[UIImageView alloc] initWithImage:(UIImage *)background];
imageView.frame = CGRectMake(10,10,1,30);
return imageView;
}
But I am getting a problem from casting background to a UIImage. Is this possible in this way, is there a better way to cast it or am I simply going about this wrong? I am basically using the footer as a way to create a clear spacer between cells. Need some guidance on this.
This code will add clear footer to your section, where you can set the desired height to whatever you need. Below it has been set to 30, to match your code in the question.
-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
footerView.backgroundColor = [UIColor clearColor];
return footerView;
}
Instead of type casting the colour into the UIImage, use the backgroundColor property :
Try this :
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10,10,1,30)];
imageView.backgroundColor = [UIColor clearColor];
I have a view for which i want set a border for the whole view. Can anyone tell me how to do that?
First u need to add QuartzCore.framework for your project
that is projects->target->build Phaese->link binary with libraries->hear add the QuartzCore.framework
then in the .m or in .h file where ur view is crating import <import <QuartzCore/QuartzCore.h>
then u are able to access the layer properties then use the any of the answer posted below
for your view there is layer property borderWidth you can set it like
view.borderWidth = 2.5f; //any value
and there are lot other layer properties associated with the view u can also use them
you edited the question well use below code to achieve your requirement
lets try this separate project it is the simple way to achieve your goal
do all settings above wat i mentioned and
just copy and past it in viewDidLoad method and run
- (void)viewDidLoad
{
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
//i am guessing heare, suppose your view is somthing like this
UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(30, 40, 100, 100)];
myView.layer.borderWidth = 2.5f;
myView.layer.borderColor = [UIColor blackColor].CGColor;
myView.backgroundColor = [UIColor whiteColor];//hear u got the view with black border
//now you need to add that label at the top left
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(myView.frame.origin.x + 5, myView.frame.origin.y, 25, 4)];//place the label in the specified position suppose if u use the
label.text = #"Label text";
label.font = [UIFont systemFontOfSize:3.0f];//set the font to your requirement
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColor whiteColor];
//remember the order is important
[self.view addSubview:myView];//first add the view with boreder
[self.view addSubview:label];//then add the this label on top of border
}
hope this helps u .. :)
In Xib take a UIView and label connect them with their IBOutlets and place label on UiView where you want to show label.
and add QuartzCore framework in project.
In your .h file
#Property(nonatomic,strong)IBOutlet UIView *myView;
#Property(nonatomic,strong)IBOutlet UILabel *myLbl;
In Your .m file
myView.layer.borderWidth=3.0f;
myView.layer.borderColor = [[UIColor greenColor] CGColor];
myView.layer.backgroundColor=[UIColor whiteColor].CGColor;
myView.layer.cornerRadius=0.0f;
myLbl.text = #"ReEvaluate";
myLbl.backgroundColor = [UIColor whiteColor];
In your .h file
#Property(nonatomic,strong)UIView *myView;
In your .m file
import <QuartzCore/QuartzCore.h>
##synthesize myView;
self.myView.layer.borderWidth=2.0f;
self.myView.layer.borderColor = [UIColor Black].CGColor;
self.myView.layer.backgroundColor=[UIColor redColor].CGColor;
self.myView.layer.cornerRadius=20.0f;
Try this one , You have to add #import <QuartzCore/QuartzCore.h>
self.Yourview.layer.borderWidth=1.0;
self.Yourview.layer.borderColor = [UIColor lightGrayColor].CGColor;
self.Yourview.layer.backgroundColor=[UIColor whiteColor].CGColor;
self.Yourview.layer.cornerRadius=30.5; // if you want corner radius then do this otherwise comment it
EDIT:
AS per your Requirement :
UITextField with below Steps:
Step (1):
Take Xib or storyboard UITextField.
Step (2):
it More proper set the property on textfield:
Step (3):
Make sure it disable ,like below:
may it will help.
Happy coding..:)
You can set the border properties on the CALayer by accessing the layer property of the button.
First, add Quartz
#import <QuartzCore/QuartzCore.h>
Set properties:
[[myView layer] setBorderWidth:2.0f];
[[myView layer] setBorderColor:[UIColor greenColor].CGColor];
myView.layer.cornerRadius=20.0f;
See:
http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ/cl/CALayer
The CALayer in the link above allows you to set other properties like corner radius, maskToBounds etc...
You can use the layer of view for displaying border around your view.
myView.layer.borderwidth = 1.0;
myView.layer.borderColor = [[Uicolor blackcolor].CGColor];
I am using a text view as a comment composer.
In the properties inspector I can't find anything like a border style property so that I can make use a rounded rect, something like UITextField.
So, the question is: How can I style a UITextView like a UITextField with a rounded rect?
There is no implicit style that you have to choose, it involves writing a bit of code using the QuartzCore framework:
//first, you
#import <QuartzCore/QuartzCore.h>
//.....
//Here I add a UITextView in code, it will work if it's added in IB too
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 220, 200, 100)];
//To make the border look very close to a UITextField
[textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[textView.layer setBorderWidth:2.0];
//The rounded corner part, where you specify your view's corner radius:
textView.layer.cornerRadius = 5;
textView.clipsToBounds = YES;
It only works on OS 3.0 and above, but I guess now it's the de facto platform anyway.
this code worked well for me:
[yourTextView.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
[yourTextView.layer setBorderColor: [[UIColor grayColor] CGColor]];
[yourTextView.layer setBorderWidth: 1.0];
[yourTextView.layer setCornerRadius:8.0f];
[yourTextView.layer setMasksToBounds:YES];
Swift 3 Version
After setting up the text view in interface builder.
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.layer.cornerRadius = 5
textView.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
textView.layer.borderWidth = 0.5
textView.clipsToBounds = true
}
Swift 2.2 Version
#IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
textView.layer.cornerRadius = 5
textView.layer.borderColor = UIColor.grayColor().colorWithAlphaComponent(0.5).CGColor
textView.layer.borderWidth = 0.5
textView.clipsToBounds = true
}
Edit: You have to import
#import <QuartzCore/QuartzCore.h>
for using corner radius.
Try this it will work for sure
UITextView* txtView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 300, 100)];
txtView.layer.cornerRadius = 5.0;
txtView.clipsToBounds = YES;
As
Rob figured it out setting the if you want the border color to be similar as UITextField then you need to change the border width to 2.0 and color to gray by adding the following line
[textView.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor]];
[textView.layer setBorderWidth:2.0];
I wanted the real deal, so I add UIImageView as a subview of the UITextView. This matches the native border on a UITextField, including the gradient from top to bottom:
textView.backgroundColor = [UIColor clearColor];
UIImageView *borderView = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, textView.frame.size.width, textView.frame.size.height)];
borderView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
UIImage *textFieldImage = [[UIImage imageNamed:#"TextField.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 8, 15, 8)];
borderView.image = textFieldImage;
[textField addSubview: borderView];
[textField sendSubviewToBack: borderView];
These are the images I use:
One solution is to add a UITextField below the UITextView, make the UITextView background transparent and disable any user interaction on the UITextField. Then in code change the UITextField frame with something like that
self.textField.frame = CGRectInset(self.textView.frame, 0, -2);
You will have exactly the same look as a text field.
And as suggested by Jon, you should put this piece of code inside [UIViewController viewDidLayoutSubviews] on iOS 5.0+.
For the best effect you have to use a custom (stretchable) background image. This is also how the UITextField's rounded border is drawn.
One way I found to do it without programming is to make the textfield background transparent, then place a Round Rect Button behind it. Make sure to change the button settings to disable it and uncheck the Disable adjusts image checkbox.
You may want to check out my library called DCKit.
You'd be able to make a rounded corner text view (as well as text field/button/plain UIView) from the Interface Builder directly:
It also has many other useful features, such as text fields with validation, controls with borders, dashed borders, circle and hairline views etc.
I know there are already a lot of answers to this one, but I didn't really find any of them sufficient (at least in Swift). I wanted a solution that provided the same exact border as a UITextField (not an approximated one that looks sort of like it looks right now, but one that looks exactly like it and will always look exactly like it). I needed to use a UITextField to back the UITextView for the background, but didn't want to have to create that separately every time.
The solution below is a UITextView that supplies it's own UITextField for the border. This is a trimmed down version of my full solution (which adds "placeholder" support to the UITextView in a similar way) and was posted here: https://stackoverflow.com/a/36561236/1227119
// This class implements a UITextView that has a UITextField behind it, where the
// UITextField provides the border.
//
class TextView : UITextView, UITextViewDelegate
{
var textField = TextField();
required init?(coder: NSCoder)
{
fatalError("This class doesn't support NSCoding.")
}
override init(frame: CGRect, textContainer: NSTextContainer?)
{
super.init(frame: frame, textContainer: textContainer);
self.delegate = self;
// Create a background TextField with clear (invisible) text and disabled
self.textField.borderStyle = UITextBorderStyle.RoundedRect;
self.textField.textColor = UIColor.clearColor();
self.textField.userInteractionEnabled = false;
self.addSubview(textField);
self.sendSubviewToBack(textField);
}
convenience init()
{
self.init(frame: CGRectZero, textContainer: nil)
}
override func layoutSubviews()
{
super.layoutSubviews()
// Do not scroll the background textView
self.textField.frame = CGRectMake(0, self.contentOffset.y, self.frame.width, self.frame.height);
}
// UITextViewDelegate - Note: If you replace delegate, your delegate must call this
func scrollViewDidScroll(scrollView: UIScrollView)
{
// Do not scroll the background textView
self.textField.frame = CGRectMake(0, self.contentOffset.y, self.frame.width, self.frame.height);
}
}
One way I found to do it without programming is to make the textfield background transparent, then place a Round Rect Button behind it. Make sure to change the button settings to disable it and uncheck the Disable adjusts image checkbox.
Tried the Quartzcore code and found it caused lag on my old 3G (I use for testing). Not a big issue but if you want to be as inclusive as possible for different ios and hardware I recommend Andrew_L's answer above - or make your own images and apply accordingly.
There is a great background image that is identical to the UITextView used for sending text messages in iPhone's Messages app. You'll need Adobe Illustrator to get & modify it.
iphone ui vector elements
#import <QuartzCore/QuartzCore.h>
- (void)viewDidLoad{
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 220, 200, 100)];
textView.layer.cornerRadius = 5;
textView.clipsToBounds = YES;
[textView.layer setBackgroundColor: [[UIColor whiteColor] CGColor]];
[textView.layer setBorderColor: [[UIColor grayColor] CGColor]];
[textView.layer setBorderWidth: 1.0];
[textView.layer setCornerRadius:8.0f];
[textView.layer setMasksToBounds:YES];
[self.view addSubView:textview];
}
You can create a Text Field that doesn't accept any events on top of a Text View like this:
CGRect frameRect = descriptionTextField.frame;
frameRect.size.height = 50;
descriptionTextField.frame = frameRect;
descriptionTextView.frame = frameRect;
descriptionTextField.backgroundColor = [UIColor clearColor];
descriptionTextField.enabled = NO;
descriptionTextView.layer.cornerRadius = 5;
descriptionTextView.clipsToBounds = YES;
If you want to keep your controller code clean, you can subclass UITextView like below, and change the class name in the Interface Builder.
RoundTextView.h
#import <UIKit/UIKit.h>
#interface RoundTextView : UITextView
#end
RoundTextView.m
#import "RoundTextView.h"
#import <QuartzCore/QuartzCore.h>
#implementation RoundTextView
-(id) initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self.layer setBorderColor:[[[UIColor grayColor] colorWithAlphaComponent:0.333] CGColor]];
[self.layer setBorderWidth:1.0];
self.layer.cornerRadius = 5;
self.clipsToBounds = YES;
}
return self;
}
#end
Here is my solution:
- (void)viewDidLoad {
[super viewDidLoad];
self.textView.text = self.messagePlaceholderText;
self.textView.layer.backgroundColor = [[UIColor whiteColor] CGColor];
self.textView.layer.borderColor = [[[UIColor grayColor] colorWithAlphaComponent:0.3] CGColor];
self.textView.layer.borderWidth = 0.5;
self.textView.layer.cornerRadius = 5.5f;
self.textView.layer.masksToBounds = YES;
self.textView.textColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
}
- (void)textViewDidBeginEditing:(UITextView *)textView {
if (textView == self.tvMessage) {
// Delete placeholder text
if ([self.textView.text isEqualToString:self.messagePlaceholderText]) {
self.textView.text = #"";
self.textView.textColor = [UIColor blackColor];
}
}
}
- (void)textViewDidEndEditing:(UITextView *)textView {
if (textView == self.tvMessage) {
// Write placeholder text
if (self.textView.text.length == 0) {
self.textView.text = self.messagePlaceholderText;
self.textView.textColor = [[UIColor grayColor] colorWithAlphaComponent:0.4];
}
}
}
I don't think that it is possible. but you can do UITableView(grouped) with 1 section and 1 empty cell and use it as a container for your UITextView.
This is an old question, and I was also searched for this questions answer. luvieeres' answer is 100% correct and later Rob added some code. That is excellent, but I found a third party in another questions answer which seems very helpful to me. I was not only searched for similar look of UITextField over UITextView, I was also searched for multiline support. ChatInputSample satisfied both. Thats why I think this third party might be helpful to others. Also thanks to Timur, he mentioned this open source in here.
In iOS7 the following matches UITextField border perfectly (to my eye at least):
textField.layer.borderColor = [[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor];
textField.layer.borderWidth = 0.5;
textField.layer.cornerRadius = 5;
textField.clipsToBounds = YES;
There is no need to import anything special.
Thanks to #uvieere and #hanumanDev whose answers go me almost there :)
How about just:
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 20, 280, 32)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[self addSubview:textField];
A UITableViewCell comes "pre-built" with a UILabel as its one and only subview after you've init'ed it. I'd really like to change the background color of said label, but no matter what I do the color does not change. The code in question:
UILabel* label = (UILabel*)[cell.contentView.subviews objectAtIndex:0];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkGrayColor];
label.opaque = YES;
Your code snippet works fine for me, but it must be done after the cell has been added to the table and shown, I believe. If called from the initWithFrame:reuseIdentifier:, you'll get an exception, as the UILabel subview has not yet been created.
Probably the best solution is to add your own UILabel, configured to your standards, rather than relying on this (very rickety) path to the built-in one.
This doesn't work because the UITableViewCell sets its label backgroundColors in the layoutSubviews method.
If you want to change the color of the built-in textLabel or detailTextLabel, subclass the UITableViewCell and override layoutSubviews. Call the super implementation, THEN change the backgroundColor property to what you want.
- (void) layoutSubviews
{
[super layoutSubviews];
self.textLabel.backgroundColor = [UIColor redColor];
}
Add your own label to the contentView when you are allocating the cell, rather than relying on the extraction of the built in one. Then you can control all values:
UILabel* label = [[[UILabel alloc] init] autorelease];
label.textColor = [UIColor whiteColor];
label.backgroundColor = [UIColor darkGrayColor];
label.opaque = YES;
[cell.contentView addSubview:label];
for (UIView *views in views.subviews)
{
UILabel* temp = (UILabel*)[views.subviews objectAtIndex:0];
temp.textColor = [UIColor whiteColor];
temp.shadowColor = [UIColor blackColor];
temp.shadowOffset = CGSizeMake(0.0f, -1.0f);
}