I need to draw a rounded rectangle bar in my iOS application, but without using a background image. Is there any way to make a rounded rectangle view or label?
In addition to the other answer(s), don't forget to set the masksToBounds property.
#import <QuartzCore/QuartzCore.h>
label.layer.cornerRadius = 5.0;
label.layer.masksToBounds = YES;
Alternative if you're using the UI designer (Storyboard or nib file) you can set a User Defined Runtime Attribute.
Click on the view you want to have rounded corners, click Show the Identity Inspector (3rd tab, top right).
Then click the + in User Defined Runtime Attributes and enter the following:
Key Path: layer.cornerRadius
Type: Number
Value: whatever number, e.g. 5
Add Quartz core Framework ..
#import <QuartzCore/QuartzCore.h>
Then Set corner radius,
yourView_LabelName.layer.cornerRadius = 10.0;
It may be useful for you.
Step1 :add the quartzcore framework to your project frameworks.
In which file you want to write this code there, you have to use this.
#import <QuartzCore/QuartzCore.h>
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 50, 30)];
myLabel.text = #"text";
myLabel.layer.cornerRadius =8.0;
[self.view addSubview:myLabel];
Related
I have a subview on my main view which pops up using tap.
I set a background image to it but now the title is covered . what do I need to do to fix this? SHOULD I change anything in viewDidLoad?
here is how I have added the background image:
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"test.png"]];
imageView.frame = CGRectMake(0.0, ViewHeight, kDeviceWidth, 230.0);
container.opaque = NO;
container.backgroundColor = [UIColor clearColor];
[self.formView addSubview:imageView];
[self.formView addSubview:frame];
If you want to add title on top of background image then you need to add title to image view like this
[imageView addSubview title];
if you want to add title on top of form view then do this
[self.formView addSubview:imageView];
[self.formView addSubview:title];
Hope it helps.
Every layer has property called zPosition. By changing zPosition you can manipulate the order of drawing the layers.
CALayers with greater zPosition cover the CALayers, that have smaller zPosition. zPosition is by default 0, so setting -1 value for the background image layer should work.
To change this property you have to use QuartzCore framework: click on Project - Targets - ProjectName - Build Phases - Link Binary With Libraries - Plus - QuartzCore.
#include <QuartzCore/QuartzCore.h>
imageView.layer.zPosition = -1;
There are a few tutorial about showing how to add a shadow to a UINavigation bar, but is there any method which would best suit adding this shadow application wide, rather than in a single instance?
Or is my only option to simply have a sub-classed nab bar in every single view of my application? Thought there might be a quicker, easier way than doing that?
Thanks.
Create a category of UINavigationBar called UINavigationBar+dropshadow.m and put this in the file
#import <QuartzCore/QuartzCore.h>
#interface UINavigationBar (dropshadow)
-(void) applyDefaultStyle;
#end
#implementation UINavigationBar (dropshadow)
-(void)willMoveToWindow:(UIWindow *)newWindow{
[super willMoveToWindow:newWindow];
[self applyDefaultStyle];
}
- (void)applyDefaultStyle {
// add the drop shadow
self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowOffset = CGSizeMake(0.0, 3.0);
self.layer.shadowOpacity = 0.25;
self.layer.shouldRasterize = YES;
}
#end
If you're working with iOS6, you can use the Appearance proxy to do that.
Here's the Apple Class Reference: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html
EDIT 1 Fixed required iOS version (was mistakenly iOS5 previously)
EDIT 2
See this SO question: Appearance proxy - setShadowImage alternative for iOS 5? for a code snippet
I'd like to draw a simple inset line in Interface Builder to separate some items in a list and make the UI a bit more tidy. I don't see any "line" or similar objects in the objects library and can't seem to find any drawing commands in Interface builder.
I use a very narrow UIView with backgroundColor set to the appropriate color.
There are no lines in the iPhone UI library. This functionality on Max OS X was supplied by NSBox, but on the iPhone there is no corresponding UI element.
If you're afraid a UIView might affect performance, you can draw a line in code using CoreGraphics' CAShapeLayers.
Every UIView has a CALayer, so draw the line and add it to the views CALayer.
You can do this either in a custom UIView's drawRect or in your view controller:
(Make sure to add Quartz framework to your project)
UIBezierPath *linePath = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0,self.view.frame.size.width, 1)];
//shape layer for the line
CAShapeLayer *line = [CAShapeLayer layer];
line.path = [linePath CGPath];
line.fillColor = [[UIColor blackColor] CGColor];
line.frame = CGRectMake(xPosition, yPosition, self.view.frame.size.width,1);
[self.view.layer addSublayer:line];
In place of view, why not just use a label and set the appropriate background color?
I used a view and made it narrow & changed the color to make it look like a line. But my issue is I am using the line on a vertical scrollview and the lines also get scrolled.
Interface Builder does not allow you to draw the shadows essential for an inset line. If you work with Photoshop you know this.
The following code will draw an "inset" if the line is in front of a white/beige Background.
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 1.0f)];
line.backgroundColor = [UIColor colorWithRed:(200.0f/255.0f) green:(200.0f/255.0f) blue:(200.0f/255.0f) alpha:1.0f];
line.layer.shadowColor = [UIColor darkGrayColor].CGColor;
line.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
line.layer.shadowRadius = 0.5f;
line.layer.shadowOpacity = 0.4f;
line.layer.masksToBounds =NO;
[Background addSubview:line];
Remember to import < QuartzCore/QuartzCore.h>.
As an alliterative - in Interface Builder, add a view who's height is set to 2 points. Wire up a IBOutlet to that view and set its layer border color and width:
self.mySeparatorLine.layer.borderWidth = 1.0f;
self.mySeparatorLine.layer.borderColor = [UIColor lightGrayColor].CGColor;
You can use Progress View as an option for a horizontal line. Just change the tint color and progress to 1 which is 100%.
in many iPhone apps I could see UITextView with rounded corners. Is there a simple way to achieve this? or do I need to create a custom UITextView? any links to examples showing how to achieve this?
Thanks!
If you are using iOS 3+, you can do the following:
myTextView.clipsToBounds = YES;
myTextView.layer.cornerRadius = 10.0f;
Add QuartzCore framework to the app and then
#import <QuartzCore/QuartzCore.h>
UITextView* txtView = [[UITextView alloc] initWithFrame:CGRectMake(50, 50, 300, 100)];
txtView.layer.cornerRadius = 5.0;
txtView.clipsToBounds = YES;
Just a quick reminder: you can do it easily from the the stroyboard/xib:
Or create an extension, and set it programmatically or through the storyboard
extension UIView {
#IBInspectable var cornerRadius: CGFloat {
set {
layer.cornerRadius = newValue
layer.masksToBounds = newValue > 0
}
get {
return layer.cornerRadius
}
}
}
The easiest way I have found is to put the Rounded Rect button with no text under the UITextView, take the UITextView smaller than button.
Done.
Swift 4.2 🔸
myTextView.layer.cornerRadius = 20
You may want to check out my library called DCKit. It's written on the latest version of Swift.
You'd be able to make a rounded corner text view/button/text field from the Interface builder directly:
It also has many other cool features, such as text fields with validation, controls with borders, dashed borders, circle and hairline views etc.
I am trying to set an image as the background of a custom UIButton. I was able to set a background image for the "rounded rect" UIButton in interface builder, but now the borders have vanished. Is there any way to retain the borders? Also, I would like to make the borders show up rectangular instead of rounded.
#import <QuartzCore/QuartzCore.h> // don't forget to add this in your file
CALayer * layer = [yourUIButton layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:0.0]; //when radius is 0, the border is a rectangle
[layer setBorderWidth:1.0];
[layer setBorderColor:[[UIColor grayColor] CGColor]];
i know this is an old question, but still i would like to post my answer here. since i was looking for answer but got this resolved myself in an easy way.
Check out the cornerRadius, borderWidth and borderColor properties of CALayer. These properties are new as of iPhone OS 3.0.
You probably want to subclass UIButton and then set these properties in the constructor. Your button has a layer property you can access to set these border properties.
Here's an example:
- (id)initWithFrame:(CGRect)frame {
if(self = [super initWithFrame:frame]) {
[self.layer setBorderWidth:1.0];
[self.layer setCornerRadius:5.0];
[self.layer setBorderColor:[[UIColor colorWithWhite:0.3 alpha:0.7] CGColor]];
}
return self;
}
You could also "User Defined Runtime Attributes" in IB to set rounded corners like done in image below.
http://i.stack.imgur.com/uBkuB.png
I'd just like to add to the answers provided by nanshi and Jonathan.
In order to access the border and cornerRadius properties of the layer you will first need to import the QuartzCore framework.
#import <QuartzCore/QuartzCore.h>
Otherwise you will not be able to access the properties.
When you set a background image in a button, the button outlines go away. Include them in your image, if you need different sizes look at the UIImage
stretchableImageWithLeftCapWidth:topCapHeight:
method.
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:
[[myButton layer] setBorderWidth:2.0f];
[[myButton layer] setBorderColor:[UIColor greenColor].CGColor];