UITextField border color - ios

I have really great wish to set my own color to UITextField border. But so far I could find out how to change the border line style only.
I've used background property to set background color in such way:
self.textField.backgroundColor = textFieldColor;
But I have to change color of UITextField border too. And my question was about how to change the border color.

Import QuartzCore framework in you class:
#import <QuartzCore/QuartzCore.h>
and for changing the border color use the following code snippet (I'm setting it to redColor),
textField.layer.cornerRadius=8.0f;
textField.layer.masksToBounds=YES;
textField.layer.borderColor=[[UIColor redColor]CGColor];
textField.layer.borderWidth= 1.0f;
For reverting back to the original layout just set border color to clear color,
serverField.layer.borderColor=[[UIColor clearColor]CGColor];
in swift code
textField.layer.borderWidth = 1
textField.layer.borderColor = UIColor.whiteColor().CGColor

Try this:
UITextField *theTextFiels=[[UITextField alloc]initWithFrame:CGRectMake(40, 40, 150, 30)];
theTextFiels.borderStyle=UITextBorderStyleNone;
theTextFiels.layer.cornerRadius=8.0f;
theTextFiels.layer.masksToBounds=YES;
theTextFiels.backgroundColor=[UIColor redColor];
theTextFiels.layer.borderColor=[[UIColor blackColor]CGColor];
theTextFiels.layer.borderWidth= 1.0f;
[self.view addSubview:theTextFiels];
[theTextFiels release];
and import QuartzCore:
#import <QuartzCore/QuartzCore.h>

Import the following class:
#import <QuartzCore/QuartzCore.h>
//Code for setting the grey color for the border of the text field
[[textField layer] setBorderColor:[[UIColor colorWithRed:171.0/255.0
green:171.0/255.0
blue:171.0/255.0
alpha:1.0] CGColor]];
Replace 171.0 with the respective color number as required.

Update for swift 5.0
textField.layer.masksToBounds = true
textField.layer.borderColor = UIColor.blue.cgColor
textField.layer.borderWidth = 1.0

this question shows up pretty high on a Google search and worked for the most part! I did find that Salman Zaidi's answer was partially correct for iOS 7.
You need to make a modification to the "reverting" code. I found that the following for reverting worked perfectly:
textField.layer.cornerRadius = 0.0f;
textField.layer.masksToBounds = YES;
textField.layer.borderColor = [[UIColor blackColor] CGColor];
textField.layer.borderWidth = 0.0f;
I understand that this is most likely due to changes in iOS 7.

To simplify this actions from accepted answer, you can also create Category for UIView (since this works for all subclasses of UIView, not only for textfields:
UIView+Additions.h:
#import <Foundation/Foundation.h>
#interface UIView (Additions)
- (void)setBorderForColor:(UIColor *)color
width:(float)width
radius:(float)radius;
#end
UIView+Additions.m:
#import "UIView+Additions.h"
#implementation UIView (Additions)
- (void)setBorderForColor:(UIColor *)color
width:(float)width
radius:(float)radius
{
self.layer.cornerRadius = radius;
self.layer.masksToBounds = YES;
self.layer.borderColor = [color CGColor];
self.layer.borderWidth = width;
}
#end
Usage:
#import "UIView+Additions.h"
//...
[textField setBorderForColor:[UIColor redColor]
width:1.0f
radius:8.0f];

If you use a TextField with rounded corners use this code:
self.TextField.layer.cornerRadius=8.0f;
self.TextField.layer.masksToBounds=YES;
self.TextField.layer.borderColor=[[UIColor redColor]CGColor];
self.TextField.layer.borderWidth= 1.0f;
To remove the border:
self.TextField.layer.masksToBounds=NO;
self.TextField.layer.borderColor=[[UIColor clearColor]CGColor];

borderColor on any view(or UIView Subclass) could also be set using storyboard with a little bit of coding and this approach could be really handy if you're setting border color on multiple UI Objects.
Below are the steps how to achieve it,
Create a category on CALayer class. Declare a property of type UIColor with a suitable name, I'll name it as borderUIColor .
Write the setter and getter for this property.
In the 'Setter' method just set the "borderColor" property of layer to the new colors CGColor value.
In the 'Getter' method return UIColor with layer's borderColor.
P.S: Remember, Categories can't have stored properties. 'borderUIColor' is used as a calculated property, just as a reference to achieve what we're focusing on.
Please have a look at the below code sample;
Objective C:
Interface File:
#import <QuartzCore/QuartzCore.h>
#import <UIKit/UIKit.h>
#interface CALayer (BorderProperties)
// This assigns a CGColor to borderColor.
#property (nonatomic, assign) UIColor* borderUIColor;
#end
Implementation File:
#import "CALayer+BorderProperties.h"
#implementation CALayer (BorderProperties)
- (void)setBorderUIColor:(UIColor *)color {
self.borderColor = color.CGColor;
}
- (UIColor *)borderUIColor {
return [UIColor colorWithCGColor:self.borderColor];
}
#end
Swift 2.0:
extension CALayer {
var borderUIColor: UIColor {
set {
self.borderColor = newValue.CGColor
}
get {
return UIColor(CGColor: self.borderColor!)
}
}
}
And finally go to your storyboard/XIB, follow the remaining steps;
Click on the View object for which you want to set border Color.
Click on "Identity Inspector"(3rd from Left) in "Utility"(Right side of the screen) panel.
Under "User Defined Runtime Attributes", click on the "+" button to add a key path.
Set the type of the key path to "Color".
Enter the value for key path as "layer.borderUIColor". [Remember this should be the variable name you declared in category, not borderColor here it's borderUIColor].
Finally chose whatever color you want.
You've to set layer.borderWidth property value to at least 1 to see the border color.
Build and Run.
Happy Coding. :)

extension UIView {
func addBorder(_ width: CGFloat = 1, color: UIColor = .black, cornerRadius: CGFloat = 4) {
layer.borderWidth = width
layer.borderColor = color.cgColor
layer.cornerRadius = cornerRadius
}
}
Call this like:
email.addBorder(1.0, color: .blue, cornerRadius: 5).

Related

UITextField overflow

I made a simple subclass for UITextField and it works as intended. The only problem I'm encountering is when the text value gets too large, it overflows into the clear button.
I can't seem to find out how to only alter the right side of the text to have some padding to not intersect with the clear button.
#import "TextField.h"
#import <QuartzCore/QuartzCore.h>
IB_DESIGNABLE
#implementation TextField
- (void)awakeFromNib {
self.layer.borderColor = [[[UIColor grayColor] colorWithAlphaComponent:0.5] CGColor];
[self.layer setBorderWidth:0.6];
self.layer.cornerRadius = 4;
self.layer.masksToBounds = YES;
}
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectInset(bounds, 12, 0);
}
- (CGRect)editingRectForBounds:(CGRect)bounds {
return [self textRectForBounds:bounds];
}
#end
I've managed to move BOTH sides of the text so it doesn't overflow into the button, but then the left side looks strange because it has extra spacing. How can I only add padding to the right side of the text or the left side of the clear button?
Just shift your rect over a bit.
- (CGRect)textRectForBounds:(CGRect)bounds {
return CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width - 10, bounds.size.height);
}
By extension your editing rect should update too since you're basing it off the textRect.

Create circle button in iOS7 without images

I would like to recreate the circular buttons found in the Clock app in iOS7. The buttons are basically circles with different appearance depending on the button states (green border, red border, grey fill).
I could of course achieve this using a simple UIButton with images for the different states.
However I am looking for a solution which draws the circle programmatically, so I can easily change radius, stroke width, etc.
As far as I can see UIButton only allows me to define an UIImage for each state, so I cannot modify the layers per state directly (e.g. provide a layer with cornerRadius). Is there another way?
Creating a custom button may be helpful.
in the .h file;
#import <UIKit/UIKit.h>
#interface CircleLineButton : UIButton
- (void)drawCircleButton:(UIColor *)color;
#end
in the .m file;
#import "CircleLineButton.h"
#interface CircleLineButton ()
#property (nonatomic, strong) CAShapeLayer *circleLayer;
#property (nonatomic, strong) UIColor *color;
#end
#implementation CircleLineButton
- (void)drawCircleButton:(UIColor *)color
{
self.color = color;
[self setTitleColor:color forState:UIControlStateNormal];
self.circleLayer = [CAShapeLayer layer];
[self.circleLayer setBounds:CGRectMake(0.0f, 0.0f, [self bounds].size.width,
[self bounds].size.height)];
[self.circleLayer setPosition:CGPointMake(CGRectGetMidX([self bounds]),CGRectGetMidY([self bounds]))];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame))];
[self.circleLayer setPath:[path CGPath]];
[self.circleLayer setStrokeColor:[color CGColor]];
[self.circleLayer setLineWidth:2.0f];
[self.circleLayer setFillColor:[[UIColor clearColor] CGColor]];
[[self layer] addSublayer:self.circleLayer];
}
- (void)setHighlighted:(BOOL)highlighted
{
if (highlighted)
{
self.titleLabel.textColor = [UIColor whiteColor];
[self.circleLayer setFillColor:self.color.CGColor];
}
else
{
[self.circleLayer setFillColor:[UIColor clearColor].CGColor];
self.titleLabel.textColor = self.color;
}
}
#end
And in the view controller, call [self.myCircleButton drawCircleButton:[UIColor myColor]]
There are lot of ways you could accomplish this, for example:
Use CAShapedLayer
Subclass UIView and use the drawRect: method to draw a circle
Just have a square UIView and use the layer.cornerRadius
property.
Depending on your needs, something as simple as creating normal UIButton and calling
myButton.layer.cornerRadius = myButton.bounds.size.width / 2.0;
could work (you'll need to include the Quartz Framework)
The pattern I've used to achieve this kind of thing is:
Subclass UIButton and implement drawRect to draw the button as you want, customising colours based on the selected and highlighted properties of the button.
Then override setSelected and setHighlighted to force redraws like so:
-(void)setHighlighted:(BOOL)highlighted {
[super setHighlighted:highlighted];
[self setNeedsDisplay];
}
Add this code
imagePreview.layer.cornerRadius = (imagePreview.bounds.size.height/2);
where imagePreview is UIview/UIimageview/UIButton
don't forget to add
#import <QuartzCore/QuartzCore.h>
Solution with a swift Extension :
extension UIView{
func asCircle(){
self.layer.cornerRadius = self.frame.size.width / 2;
self.layer.masksToBounds = true
}
}
Just call
myView.asCircle()
Can work with any type of view, not only a button
You can use this control, it's subclass from UIButton.
https://github.com/alvaromb/AMBCircularButton
I think it work good.
override func viewDidLoad() {
super.viewDidLoad()
var button = UIButton.buttonWithType(.Custom) as UIButton
button.frame = CGRectMake(160, 100, 50, 50)
button.layer.cornerRadius = 0.5 * button.bounds.size.width
button.setImage(UIImage(named:"thumbsUp.png"), forState: .Normal)
button.addTarget(self, action: "thumbsUpButtonPressed", forControlEvents: .TouchUpInside)
view.addSubview(button)
}
func thumbsUpButtonPressed() {
println("thumbs up button pressed")
}
In storyboard, Make square UIButton (eg. 100 * 100 )
Link outlet (eg. #property (Strong, nonatomic) UIButton IBOutlet * myButton; )
In your code write this:
self.myButton.layer.cornerRadius = 50;
// 50 is half of square's length of one side.

Circular UIImageView in UITableView without performance hit?

I have a UIImageView on each of my UITableView cells, that display a remote image (using SDWebImage). I've done some QuartzCore layer styling to the image view, as such:
UIImageView *itemImageView = (UIImageView *)[cell viewWithTag:100];
itemImageView.layer.borderWidth = 1.0f;
itemImageView.layer.borderColor = [UIColor concreteColor].CGColor;
itemImageView.layer.masksToBounds = NO;
itemImageView.clipsToBounds = YES;
So now I have a 50x50 square with a faint grey border, but I'd like to make it circular instead of squared. The app Hemoglobe uses circular images in table views, and that's the effect I'd like to achieve. However, I don't want to use cornerRadius, as that degrades my scrolling FPS.
Here's Hemoglobe showing circular UIImageViews:
Is there any way to get this effect? Thanks.
Simply set the cornerRadius to half of the width or height (assuming your object's view is square).
For example, if your object's view's width and height are both 50:
itemImageView.layer.cornerRadius = 25;
Update - As user atulkhatri points out, it won't work if you don't add:
itemImageView.layer.masksToBounds = YES;
To Add border
self.ImageView.layer.borderWidth = 3.0f;
self.ImageView.layer.borderColor = [UIColor whiteColor].CGColor;
For Circular Shape
self.ImageView.layer.cornerRadius = self.ImageView.frame.size.width / 2;
self.ImageView.clipsToBounds = YES;
Refer this link
http://www.appcoda.com/ios-programming-circular-image-calayer/
Use this code..
This will be helpful..
UIImage* image = ...;
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 1.0);
// Add a clip before drawing anything, in the shape of an rounded rect
[[UIBezierPath bezierPathWithRoundedRect:imageView.bounds
cornerRadius:50.0] addClip];
// Draw your image
[image drawInRect:imageView.bounds];
// Get the image, here setting the UIImageView image
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
// Lets forget about that we were drawing
UIGraphicsEndImageContext();
It works fine for me. :)
Here is a more up to date method in swift using IBDesignable and IBInspectable to subclass UIImageView
#IBDesignable class RoundableUIImageView: UIImageView {
private var _round = false
#IBInspectable var round: Bool {
set {
_round = newValue
makeRound()
}
get {
return self._round
}
}
override internal var frame: CGRect {
set {
super.frame = newValue
makeRound()
}
get {
return super.frame
}
}
private func makeRound() {
if self.round == true {
self.clipsToBounds = true
self.layer.cornerRadius = (self.frame.width + self.frame.height) / 4
} else {
self.layer.cornerRadius = 0
}
}
override func layoutSubviews() {
makeRound()
}
}
Yes, it is possible to give layer.cornerRadius (need to add
#import <QuartzCore/QuartzCore.h>)
for create circular any control but in your case instead of set layer of UIImageView it is The
Best Way to Create Your Image as circular and add it on UIImageView Which have backGroundColor is ClearColor.
Also refer this Two Source of code.
https://www.cocoacontrols.com/controls/circleview
and
https://www.cocoacontrols.com/controls/mhlazytableimages
This might be helpful in your case:
Set the height & width of the of the UIImageView to be the same e.g.:Height=60 & Width = 60, then the cornerRadius should be exactly the half.I have kept it 30 in my case.It worked for me.
self.imageView.layer.cornerRadius = 30;
self.imageView.layer.borderWidth = 3;
self.imageView.layer.borderColor = [UIColor whiteColor].CGColor;
self.imageView.layer.masksToBounds = YES;
Usable solution in Swift using extension:
extension UIView{
func circleMe(){
let radius = CGRectGetWidth(self.bounds) / 2
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
}
}
Usage:
self.venueImageView.circleMe()
If use some autolayout and different cells heights, u better do it this way:
override func layoutSubviews() {
super.layoutSubviews()
logo.layer.cornerRadius = logo.frame.size.height / 2;
logo.clipsToBounds = true;
}
I use a Round Image View class…
So I just use it instead of UIImageView, and have nothing to tweak…
This class also draws an optional border around the circle. There is often a border around rounded pictures.
It is not a UIImageView subclass because UIImageView has its own rendering mechanism, and does not call the drawRect method..
Interface :
#import <UIKit/UIKit.h>
#interface MFRoundImageView : UIView
#property(nonatomic,strong) UIImage* image;
#property(nonatomic,strong) UIColor* strokeColor;
#property(nonatomic,assign) CGFloat strokeWidth;
#end
Implementation :
#import "MFRoundImageView.h"
#implementation MFRoundImageView
-(void)setImage:(UIImage *)image
{
_image = image;
[self setNeedsDisplay];
}
-(void)setStrokeColor:(UIColor *)strokeColor
{
_strokeColor = strokeColor;
[self setNeedsDisplay];
}
-(void)setStrokeWidth:(CGFloat)strokeWidth
{
_strokeWidth = strokeWidth;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGPathRef path = CGPathCreateWithEllipseInRect(self.bounds, NULL);
CGContextAddPath(ctx, path);
CGContextClip(ctx);
[self.image drawInRect:rect];
if ( ( _strokeWidth > 0.0f ) && _strokeColor ) {
CGContextSetLineWidth(ctx, _strokeWidth*2); // Half border is clipped
[_strokeColor setStroke];
CGContextAddPath(ctx, path);
CGContextStrokePath(ctx);
}
CGPathRelease(path);
}
#end
Creating circular image view and thats quite easy with the below SO link, just have custom cells for your table view instead of allocating every thing in your cellForRowAtIndexPath method.
how to make button round with image background in IPhone?
The above link gives you example for buttons just use it for your image views.
If you showing the images over a solid background color, an easy solution could be to use an overlay image with a transparent circle in the middle.
This way you can still use square images and add the circle image above them to get the circular effect.
If you don't need to manipulate your images or show them on a complex background color, this can be a simple solution with no performance hit.
In swift, inside your viewDidLoad method, with the userImage outlet:
self.userImage.layer.borderWidth = 1;
self.userImage.layer.borderColor = UIColor.whiteColor().CGColor;
self.userImage.layer.cornerRadius = self.userImage.frame.size.width / 2;
self.userImage.clipsToBounds = true;
In Swift use this Extension for CircularImageView or RoundedImageView :
extension UIView {
func circular(borderWidth: CGFloat = 0, borderColor: UIColor = UIColor.whiteColor()) {
let radius = CGRectGetWidth(self.bounds) / 2
self.layer.cornerRadius = radius
self.layer.masksToBounds = true
self.layer.borderWidth = borderWidth
self.layer.borderColor = borderColor.CGColor
}
func roundedCorner(borderWidth: CGFloat = 0, borderColor: UIColor = UIColor.whiteColor()) {
let radius = CGRectGetWidth(self.bounds) / 2
self.layer.cornerRadius = radius / 5
self.layer.masksToBounds = true
self.layer.borderWidth = borderWidth
self.layer.borderColor = borderColor.CGColor
}
}
Usage:
self.ImageView.circular()
self.ImageView.roundedCorner()
For circular imageview, use the below code.
self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2;
self.imageView.clipsToBounds = true
Don't forget to change the cornerRadius in viewDidLayoutSubviews method of your UIView.
Example:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.imageView.layer.cornerRadius = self.imageView.frame.size.width / 2;
self.imageView.clipsToBounds = true
}

Artifacts when animating shapes in C4 - c4framework

When animating a shape in C4 the undefined preset characteristics of the shape, such as line width and fillcolour, also animate. Is there a work around or solution to this problem?
C4Shapes are setup to use the default colors C4Red (for the strokeColor) and C4Blue (for the fillColor). Also, the default animationDuration is set to 0.25 seconds.
Technically, what's happening, is the C4Shape is constructing itself with C4Red / C4Blue colors and then initiates an animation from those to any new colors as soon as it hits the canvas.
To get around these, and to generate your own settings you can subclass C4Shape and add the coloring / timing / other property changes to your class's own init method.
In the implementation (.m file) of a MyShape class I have:
#implementation MyShape
-(id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if(self != nil) {
self.animationDuration = 0.0f;
self.fillColor = [UIColor purpleColor];
self.strokeColor = [UIColor greenColor];
}
return self;
}
#end
... and my C4WorkSpace.m looks like this:
#import "C4WorkSpace.h"
#import "MyShape.h"
#implementation C4WorkSpace
-(void)setup {
MyShape *ms = [MyShape new];
[ms ellipse:CGRectMake(100, 100, 100, 100)];
[self.canvas addShape:ms];
}
#end
I know this is a bit crude at the moment, but we haven't worked through setting the default colors before the object hits the canvas.

setting a background color of an UIView from an UIViewController

I have a UIView subclass that draws a simple rectangle with this code:
- (void)drawRect:(CGRect)rect {
//Get the CGContext from this view
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef myColor = [UIColor colorWithHue:0 saturation:1 brightness:0.61 alpha:1].CGColor;
//Draw a rectangle
CGContextSetFillColorWithColor(context, myColor);
//Define a rectangle
CGContextAddRect(context, CGRectMake(0, 0, 95.0, 110.0));
//Draw it
CGContextFillPath(context);
}
then, I have a separate UIViewController that has a UISlider in it
-(IBAction) sliderChanged:(id) sender{
UISlider *slider = (UISlider *)sender;
int sliderValue = (int)[slider value];
float sliderFloat = (float) sliderValue;
NSLog(#"sliderValue ... %d",sliderValue);
NSLog(#"sliderFloat ... %.1f",sliderFloat / 100);
}
here, in sliderChanged, I would like to be able to dynamically change the background color of the rectangle being drawn in the UIView subclass. how should i go about implementing this ?
thank you!
Create a property of your UIView-Subclass which holds a UIColor (or CGColor) value:
In the header:
#interface MySub : UIView {
NSColor* rectColor;
}
#property (retain) NSColor* rectColor;
#end
In the implementation file:
#implementation MySub
#synthesize rectColor
#end
You can now set the color with myViewInstance.rectColor = SomeNSColor;
After you've set the color, you have to redraw the view in order to draw the rect with the new background color:
[myViewInstance setNeedsDisplay];

Resources