set label border in iphone - ios

How can I set label's border which is dynamically generated (Not from Interface Builder)?

you can do it by
Label.layer.borderColor = [UIColor whiteColor].CGColor;
Label.layer.borderWidth = 4.0;
before this you need to import a framework QuartzCore/QuartzCore.h

Swift version
Set label border
label.layer.borderWidth = 2.0
Set border color
label.layer.borderColor = UIColor.blueColor().CGColor
Use rounded corners
label.layer.cornerRadius = 8
Make background color stay within rounded corners
label.layer.masksToBounds = true

You can also try to subclass your label and override the drawRect: method to draw or a border or whatever you like:
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor blackColor] setStroke];
CGContextStrokeRect(context, self.bounds);
}

I'm not sure you can by default with UILabel. You might want to consider using a read-only (field.editing = NO) UITextField and setting it's borderStyle (which can be done programmatically using a UITextBorderStyle). That may be a little 'heavy' though. Another option may be to sub-class UILabel to draw your border.
Alternatively, and depending on your needs this may be better, use the backing CALayer and draw a border using it's borderColor and borderWidth properties.

Related

How to set border color of UIView at runtime [duplicate]

This question already has answers here:
Is it possible to set UIView border properties from interface builder?
(9 answers)
Closed 6 years ago.
I have required to set border color UIView using User Defined Runtime Attributesin identity inspector. By default Color isBlackColor`. If any constraints is available in iOS then tell me.
This will work -
UIView *view = [[UIView alloc] init];
...
//Add a Your Color border
view.layer.borderColor = [UIColor colorWithWhite:1.0f alpha:1.0f].CGColor;
view.layer.borderWidth = 1.0f; //make border 1px thick
you can set border color like,
yourView.layer.borderColor = [UIColor orangeColor].CGColor;
yourView.layer.borderWidth = 1.0; //you have to give width to make border visible
Update according to comment :
you can set runtime attributes for view like below screenshot,
But it will display black border because layer uses cgcolor and you can't get cgcolor reference in interface builder. so you can't set layer color directly as runtime attribute. It's better to set from code as i have mentioned above.
If you hardly want to set color from runtime attribute only then you can try Peter Deweese's answer.
try this code it will help you:
yourViewNAme.layer.borderWidth = 1;
yourViewNAme.layer.borderColor = [UIColor DesiredColour].CGColor;
please try this code, ViewController.h
declare #property (weak, nonatomic) IBOutlet UIView *searchView; In ViewController.m.
viewDidLoad Method:
searchView.layer.cornerRadius = 8.0f;
searchView.layer.borderColor = [UIColor lightGrayColor].CGColor;
searchView.layer.borderWidth = 0.8f;
searchView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
searchView.layer.shadowOpacity = 0.8f;
searchView.layer.shadowRadius = 3.0f;
searchView.layer.shadowOffset =CGSizeMake(2.0f, 2.0f);

How to give #3f51b5 color code programmatically for shadow color in ios

I want to make shadow color with #3f51b5.
How can I do that. hope your help. this type cannot use to do that.
cell.layer.shadowColor = [UIColor purpleColor].CGColor;
you need to set shadow first
cell.layer.shadowRadius=8;
cell.layer.shadowOffset=CGSizeMake(-1, -1);// this make top left shadow
cell.layer.shadowOpacity=1;
cell.layer.shadowColor = [UIColor colorWithRed:0.2471 green:0.3176 blue:0.7098 alpha:1.0].CGColor;
after setting the color give shadow radius,offset and opacity, convert the color to rgba and use this
cell.layer.shadowColor = [UIColor colorWithRed:0.2471 green:0.3176 blue:0.7098 alpha:1.0].CGColor;
cell.layer.shadowRadius=10;
cell.layer.shadowOffset=CGSizeMake(1, 1);
cell.layer.shadowOpacity=1;
if you want to use hex color only use this
How can I create a UIColor from a hex string?

What is UILabel default shadowRadius?

I use QuartzCore to set UILabel shadows in UITableViewCell:
cell.textLabel.layer.shadowColor = [[UIColor orange] CGColor];
cell.textLabel.layer.shadowOffset = CGSizeMake(0.0, 1.0);
cell.textLabel.layer.shadowRadius = 0.0;
cell.textLabel.layer.masksToBounds = NO;
But because of it slow performance, I have to implement -(void)drawRect:(CGRect)rect method.
Recently I found out that shadow for UILabel can be set with it properties:
cell.textLabel.shadowColor = [UIColor orangeColor];
cell.textLabel.shadowOffset = CGSizeMake(0.0, 1.0);
Now I want delete drawRect: implementation and just set shadows through properties.
QuartzCore CALayer shadowRadius default value is 3.0.
Though UILabel has not such property, is it mean that shadow radius equauls 0.0?
The documentation for UILabel says:
Text shadows are drawn with the specified offset and color and no blurring.
So, yes, the shadow radius is 0.
just a advise - setting shadow using QuartzCore in a UITableView cell impacts scroll performance. it would definitely not be smooth scrolling.
I suggest you use a UIImageView just below the UILabel to give the same effect. Its probably more work to keep the shadow-image updated with changes in design but if its a trade-off between reducing developer effort or improving user-experience; I would incline towards the latter 9 out of 10 times...

Dotted / Dashed border for UITextView / UITextField

I want to set a Dotted / Dashed border for my UITextField and UITextView.
How can I do that? I know that, I can set border with this line of code:
[self.textFieldCardTitle.layer setBorderWidth:1.0];
[self.textFieldCardTitle.layer setBorderColor:[[UIColor whiteColor] CGColor]];
Notice: I already have the idea to add a UIImageView behind the UITextView and set there an image with dashed border. But I don't want to solve it that way.
You could try, for example, next approach:
1) Create image that will represent your border (for example: one dot and space)
2) Add image to project.
3) Set border (as in code in your question) and set color with pattern:
[self.textFieldCardTitle.layer setBorderWidth:6.0];
[self.textFieldCardTitle.layer setBorderColor:[[UIColor colorWithPatternImage:[UIImage imageNamed:#"dashed_white.png"]] CGColor]];
As border is drawn along 4 sides (left, right, bottom, top) you should use square image: for example, pixel in middle is black and pixels around it are transparent. So copies of that image will be placed around the view.
Just use following code for Dotted / Dashed Border around UIView or UITextField or any other view:-
CAShapeLayer * _border = [CAShapeLayer layer];
_border.strokeColor = [UIColor redColor].CGColor;
_border.fillColor = nil;
_border.lineDashPattern = #[#4, #2];
[YOURVIEW.layer addSublayer:_border];
//for a square effect
_border.path = [UIBezierPath bezierPathWithRect:YOURVIEW.bounds].CGPath;
//for a rounded effect
//_border.path = [UIBezierPath bezierPathWithRoundedRect:YOURVIEW.bounds cornerRadius:txtUserName.frame.size.height / 2].CGPath;
_border.frame = YOURVIEW.bounds;
For more details, Refer this Answer.
Hope, this is what you're looking for. Any concern get back to me. :)
Here's what I did with Swift:
self.textFieldCardTitle.layer.borderWidth = 3
self.textFieldCardTitle.layer.borderColor = (UIColor(patternImage: UIImage(named: "dot")!)).CGColor
Free bonus, I attached the pics below. Unless StackOverflow changes its background, you probably won't see them as they are white squares on a white background, so you'll find the urls below as well.
http://i.stack.imgur.com/X7PfM.png -> rename it to dot.png
http://i.stack.imgur.com/IemhF.png -> rename it to dot#2x.png
http://i.stack.imgur.com/CSjZT.png -> rename it to dot#3x.png
Swift 5, for #Meet Doshi's answer
class CustomTextField: UITextField{
let dashBorder = CAShapeLayer()
override init(frame: CGRect) {
super.init(frame: .zero)
dashBorder.strokeColor = UIColor.lightGray.cgColor
dashBorder.fillColor = nil
dashBorder.lineDashPattern = [4, 2]
layer.addSublayer(dashBorder)
}
override func layoutSubviews() {
super.layoutSubviews()
dashBorder.path = UIBezierPath(roundedRect: bounds, cornerRadius: 5).cgPath
dashBorder.frame = bounds
}
}

Making Glow effect to font

I wonder if anybody have an example for Glow effect for font? I tried to look in NSAttributedString but I don't find in attribute for glowing text.
Try this:
#include <Quartzcore/Quartzcore.h>
.......
yourLabel.layer.shadowColor = [[UIColor glowColor] CGColor]; //Replace glowColor with the desired color (redColor, greenColor, etc.)
yourLabel.layer.shadowOffset = CGSizeMake(0.0, 0.0); //The glow starts from the center
yourLabel.layer.shadowRadius = 20.0; //You can change this to the desired amount
yourLabel.layer.shadowOpacity = 0.5; //You can change this to the desired amount
yourLabel.layer.masksToBounds = NO; //Keep this the same to avoid shadow being clipped by the label bounds
Hope this helps!

Resources