i want put a shadow in my button, but when i put a outline in the title, the outline have a shadow, and i want put shadow only in the text.
This is my code that i use in my button:
NSShadow *shadow = [NSShadow new];
[shadow setShadowColor:self.shadowColor];
[shadow setShadowOffset:self.offset];
UIColor* strokeColor = self.outlineColor;
UIColor* fontColor = [self titleColorForState:UIControlStateNormal];
NSAttributedString *attributedText =
[[NSAttributedString alloc] initWithString:self.titleLabel.text
attributes:#{
NSShadowAttributeName:shadow,
NSStrokeWidthAttributeName:[NSNumber numberWithFloat:(-1 * self.outlineWidth)],
NSStrokeColorAttributeName: strokeColor,
NSForegroundColorAttributeName: fontColor}];
[self setAttributedTitle:attributedText forState:UIControlStateNormal];
So is i want to show the title of button:
I think the easiest way to do what you want is to stack two instances of your text on top of each other. The bottom one has no outline, but casts the narrow shadow that you want. The one on top has the outline and no shadow.
i find a solution for this question, i used the next code:
-(void)drawRect:(CGRect)rect {
[super drawRect:rect];
CGRect rectt = self.titleLabel.frame;
rect = rectt;
rectt.origin.y += self.offset.height;
rectt.origin.x += self.offset.width;
UIColor *borderColor = [self.outlineColor copy];
UIColor *fillColor = self.titleLabel.textColor;
CGContextRef context = UIGraphicsGetCurrentContext();
if (self.drawShadow) {
CGContextSetLineWidth(context, 0.0f);
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
[self setTitleColor:self.shadow forState:UIControlStateNormal];
[self.titleLabel drawTextInRect:rectt];
}
if (self.drawOutline) {
CGContextSetLineWidth(context, self.outlineWidth);
CGContextSetTextDrawingMode(context, kCGTextStroke);
[self setTitleColor:borderColor forState:UIControlStateNormal];
[self.titleLabel drawTextInRect:self.titleLabel.frame];
}
CGContextSetLineWidth(context, 0.0f);
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
[self setTitleColor:fillColor forState:UIControlStateNormal];
[self.titleLabel drawTextInRect:self.titleLabel.frame];
}
My class is inherited of UIButton
Related
I'm trying to set the foreground/tintColor of a UISegmentedControl to a horizontal gradient. I want the gradient to extend across the control and appropriately adjust when each segment is clicked. See image below:
I've tried setting the tintColor using the stackoverflow solution found here, but using colorWithPattern doesn't seem to work when I change it to be horizontal:
Here's what I'm using now:
UIView* test2 = [[UIView alloc] initWithFrame:CGRectMake(10, 60, 200, 25)];
test2.backgroundColor = [self gradientFromColor:[UIColor redColor] toColor:[UIColor greenColor] withWidth:test2.frame.size.width];
[self.view addSubview:test2];
UISegmentedControl *test = [[UISegmentedControl alloc] initWithItems:#[#"one", #"two"]];
test.frame = CGRectMake(10, 100, 200, 25);
test.tintColor = [self gradientFromColor:[UIColor redColor] toColor:[UIColor greenColor] withWidth:test.frame.size.width];
[self.view addSubview:test];
...
- (UIColor*) gradientFromColor:(UIColor*)c1 toColor:(UIColor*)c2 withWidth:(int)width{
CGSize size = CGSizeMake(width, 1);
UIGraphicsBeginImageContextWithOptions(size, NO, 1);
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
NSArray* colors = [NSArray arrayWithObjects:(id)c1.CGColor, (id)c2.CGColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorspace, (CFArrayRef)colors, NULL);
CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(width, 0), 0);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
CGGradientRelease(gradient);
CGColorSpaceRelease(colorspace);
UIGraphicsEndImageContext();
return [UIColor colorWithPatternImage:image];
}
I'm thinking I need to do some work with masking but I've been having trouble getting it to work.
I am using below code to add text and border to selected image from cameraroll.
-(UIImage*)image:(UIImage*)image withText:(NSString*)text atPoint:(CGPoint)point{
if (text) {
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[image drawInRect:rect];
NSDictionary *attributes = #{NSFontAttributeName : [UIFont boldSystemFontOfSize:80],
//NSStrokeWidthAttributeName : #(4),
NSStrokeColorAttributeName : [UIColor whiteColor]};
[text drawAtPoint:point withAttributes:attributes];
CALayer *l = [self.imageView layer];
[l setBorderWidth:10.0];
[l setBorderColor:[[UIColor blueColor] CGColor]];
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newImage;
}
When i click on save button in my app, i am saving edited image to cameraroll.But edited image inside cameraroll only has text without any added border. Whether i am missing anything in above code to add and save border around image? Is there any other way to add border other than CALayer?
Replace this:
CALayer *l = [self.imageView layer];
[l setBorderWidth:10.0];
[l setBorderColor:[[UIColor blueColor] CGColor]];
With this:
UIBezierPath *rectangleBezier = [UIBezierPath bezierPathWithRect:rect];
rectangleBezier.lineWidth = 20.0f; // 10 * 2 because line drawn from the center of edge
[[UIColor blueColor] setStroke];
[rectangleBezier stroke];
Remarks:
CALayer *l = [self.imageView layer]; has nothing to do with UIImage data. imageView's layer change imageView presentation. Not the content of UIImage.
I will respond to "Adding a border around an UIImage in a UIView"
From far, the easiest method is to insert another view in the view hierarchy.
[self.view insertSubview:({
UIView * border = [[UIView alloc]
initWithFrame:CGRectInset(self.imageView.frame, -1, -1)];
border.backgroundColor = [UIColor blueColor];
border;
})
belowSubview:self.imageView];
I'm running the following code in viewDidLoad, it is applying the glow outside the label not on the text. Please advise.
lbl_score.font = myfont;
lbl_score.alpha = 1.0;
lbl_score.textColor = UIColor.greenColor;
lbl_score.layer.shadowColor = [[UIColor blueColor] CGColor];
lbl_score.layer.shadowOffset = CGSizeMake(0.0, 0.0);
lbl_score.layer.shadowRadius = 30.0f;
lbl_score.layer.shadowOpacity = 0.9;
lbl_score.layer.masksToBounds = NO;
I've imported QuartzCore/QuartzCore.h". I would like to apply the glow only on the label text.
Thanks
To apply a glow to the actual text in a label, you have to override drawTextInRect on the label itself. You're setting a shadow on the label's layer, which is a rectangle.
The override is quite simple. You just adjust the graphics context, call super, then restore the graphics context:
-(void)drawTextInRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, CGSizeZero, 30.0, [UIColor blueColor].CGColor);
[super drawTextInRect:rect];
CGContextRestoreGState(context);
}
You are putting a shadow on the layer, which is rectangular. UILabel has shadowColor and shadowOffset properties which you should use instead. You can’t set a separate opacity for the shadow this way, but you can bake it into the UIColor that you pass for the shadow color.
lbl_score.font = myfont;
lbl_score.alpha = 1.0;
lbl_score.textColor = UIColor.greenColor;
lbl_score.shadowColor = [[UIColor blueColor] colorWithAlphaComponent:0.9];
lbl_score.shadowOffset = CGSizeMake(0.0, 0.0);
Unfortunately, you can’t set a shadow radius in this way. If you need to change the radius, look at doing your own custom drawing in -drawRect: instead.
I found code similar to jrturton ,but with more customisation.
This is working for me.
DTGlowingLabel.h
#interface DTGlowingLabel : UILabel {
}
#end
DTGlowingLabel.m
#import "DTGlowingLabel.h"
#implementation DTGlowingLabel
- (void) drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIColor *insideColor = [UIColor colorWithRed:69.0/255.0 green:254.0/255.0 blue:0 alpha:1];
UIColor *outlineColor = [UIColor colorWithRed:22.0/255.0 green:145.0/255.0 blue:0 alpha:0.8];
UIColor *blurColor = [UIColor colorWithRed:104.0/255.0 green: 248.0/255.0 blue:0 alpha:0.7];
CGContextSetStrokeColorWithColor(ctx, outlineColor.CGColor);
CGContextSetFillColorWithColor(ctx, insideColor.CGColor);
CGContextSetLineWidth(ctx, self.font.pointSize/60.0);
CGContextSetShadowWithColor(ctx, CGSizeMake(0, 0), self.font.pointSize / 10.0, blurColor.CGColor);
CGContextSetTextDrawingMode(ctx, kCGTextFillStroke);
[self.text drawInRect:self.bounds withFont:self.font lineBreakMode:self.lineBreakMode alignment:self.textAlignment];
}
#end
Source : http://www.cocoanetics.com/2010/01/uilabels-with-neon-effect/
I am trying to show text above CALayer but somehow I am not able to achieve it. I have used following code:
- (void)drawRect:(CGRect)rect
{
self.layer.sublayers = nil;
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
// Code to show grid lines clipped
CGRect overallRectForBarItem = CGRectMake(20, 20, 176, 35);
CAShapeLayer *barItemShape = [CAShapeLayer layer];
barItemShape.frame = overallRectForBarItem;
barItemShape.path = [UIBezierPath bezierPathWithRect:overallRectForBarItem].CGPath;
barItemShape.strokeColor = [UIColor lightGrayColor].CGColor;
barItemShape.fillColor = [UIColor lightGrayColor].CGColor;
[self.layer addSublayer:barItemShape];
[self drawTextForBarItem:#"TESTING" inRect:CGRectMake(100, 40, 35, 0)];
// Other code clipped
CGContextRestoreGState(ctx);
}
-(void) drawTextForBarItem:(NSString*)barItemTitle inRect:(CGRect)rect
{
float actualFontSize = 14.0;
UIFont *font = [ISUtility getSystemFont:actualFontSize];
CGSize size = [barItemTitle sizeWithFont:font];
NSMutableParagraphStyle *paragraphStyle = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
paragraphStyle.alignment = NSTextAlignmentRight;
[barItemTitle drawInRect:CGRectMake(rect.origin.x, rect.origin.y - size.height/2, rect.size.width, size.height) withAttributes:#{NSFontAttributeName:font, NSForegroundColorAttributeName:[UIColor blueColor], NSParagraphStyleAttributeName:paragraphStyle}];
}
Following is the screen shot of simulator.
I have also tried zPosition but no use.
barItemShape.zPosition = -1000;
The most possible cause of you issue is that you creating CGContextRef but adding sublayer when you need to draw lightgray rectangle. I think, that it would be more consistant to draw your rectange with CoreGraphics functions and that draw your text.
To be more detailed, it seems that you drawing text in current layer but adding sublayer that will be above your text.
For instance, you can use this code to give it a try:
UIColor * redColor = [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0];
CGContextSetFillColorWithColor(context, redColor.CGColor);
CGContextFillRect(context, self.bounds);
I want to add a border in a CGRect. Although previous solutions suggest either this:
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0);
CGContextFillRect(context, rectangle);
or something like this:
view.layer.borderWidth = 10;
view.layer.borderColor = [UIColor redColor].CGColor;
I cannot apply either of this in my approach.
In my mainView Controller I am having this code:
-(void)actionHint
{
CGRect viewRect = CGRectMake(kScreenWidth/2 -kMenuWidth, kScreenHeight/2-kMenuHeight - 70,kMenuWidth*10, kMenuHeight*4);
HintMenu* hintMenu = [HintMenu viewWithRect:viewRect];
[hintMenu.btnReveal addTarget:self action:#selector(actionReveal) forControlEvents:UIControlEventTouchUpInside];
[hintMenu.btnNewAnag addTarget:self action:#selector(actionNewAnagram) forControlEvents:UIControlEventTouchUpInside];
[self.gameView addSubview:hintMenu];
}
I am creating a game and I want a simple rect with some help actions to come on top of my current view. The code for viewRect is:
+(instancetype)viewWithRect:(CGRect)r
{
HintMenu* hint = [[HintMenu alloc] initWithFrame:r];
hint.userInteractionEnabled = YES;
UIImage* image=[UIImage imageNamed:#"btn"];
... rest of items in the cgrect
return hint;
}
Where should I add the code for adding the borders and what that code should be?
Your HintMenu is a type of view so in its initWithFrame method you can do:
self.layer.borderWidth = 10;
self.layer.borderColor = [UIColor redColor].CGColor;
You can use this code to create a bordered rectangle. All you have to do is import the QuartzCore framework. Feel free to copy+paste this code.
#import <QuartzCore/QuartzCore.h>
-(void)viewDidLoad{
//give it a blue background
self.layer.backgroundColor=[UIColor blueColor].CGColor;
//set the boarder width
self.layer.borderWidth=5;
//set the boarder color
self.layer.borderColor=[UIColor redColor].CGColor;
}