I have a UITextView where I add some drop shadow to the frame, but when I write, the text got also the same shadow. How to avoid this problem?
My code :
commentary = [[UITextView alloc]initWithFrame:CGRectMake(10, 435, 230, 120)];
commentary.font = STANDARDFONT;
commentary.backgroundColor = BACKGROUND;
commentary.layer.shadowColor = [UIColor blackColor].CGColor;
commentary.layer.shadowOffset = CGSizeMake(2, 2);
commentary.layer.shadowOpacity = 0.8;
commentary.layer.shadowRadius = 2.0;
commentary.layer.borderColor = [UIColor grayColor].CGColor;
commentary.layer.borderWidth = 1.5;
commentary.layer.cornerRadius = 5;
commentary.layer.masksToBounds = NO;
commentary.clipsToBounds = NO;
[self addSubview:commentary];
BACKGROUND and STANDARDFONT is [UICOLOR clearColor].CGColorand [UIFont fontWithName:#"TimesNewRomanPSMT" size:16];
Try setting the layer background color also: commentary.layer.backgroundColor = BACKGROUND.CGColor
Related
Here is my code to add a shadow on my uibutton which is created using a .XIB file
UIBezierPath *shadowPathEndbtn = [UIBezierPath bezierPathWithRect:CGRectMake(0, self->_navigationView.frame.size.height - 70,[UIScreen mainScreen].bounds.size.width , 2)];
self->_EndButton.layer.masksToBounds = NO;
self->_EndButton.layer.shadowColor = [UIColor blackColor].CGColor;
self->_EndButton.layer.shadowOffset = CGSizeMake(0.0f, 5.0f);
self->_EndButton.layer.shadowOpacity = 0.7f;
self->_EndButton.layer.shouldRasterize = YES;
self->_EndButton.layer.shadowPath = shadowPathForEndbtn.CGPath;
I am able to set the shadow using this code but this code makes my button text look blury. I am unable to figure out the exact reason behind this. Any help is Appreciated!
self->_EndButton.layer.masksToBounds = false
self->_EndButton.layer.shadowColor = [UIColor blackColor].CGColor
self->_EndButton.layer.contentsScale = [[UIScreen mainScreen]scale];
self->_EndButton.layer.shadowOpacity = 0.7f;
self->_EndButton.layer.shadowRadius = 10.0;
self->_EndButton.layer.shadowOffset = CGSizeMake(0,0)
This will give shadow and also the test is not blurry
Set button background color
self.EndButton.BackgroundColor=[UIColor WhiteColor];
I have a UILabel that I want to add two shadows.
A black colored shadow, and a white colored one.
One has -1 y-offset, and the other has 1 y-offset.
- (void)layoutSubviews{
[super layoutSubviews];
self.sectionTitleLabel.layer.shadowColor = [[UIColor whiteColor] CGColor];
self.sectionTitleLabel.layer.shadowRadius = 0.0f;
self.sectionTitleLabel.layer.shadowOpacity = .2;
self.sectionTitleLabel.layer.shadowOffset = CGSizeMake(0.f, -1.f);
CALayer *blackShadow = [[CALayer alloc] initWithLayer:self.sectionTitleLabel.layer];
blackShadow.shadowColor = [[UIColor blackColor] CGColor];
blackShadow.shadowRadius = 0.0f;
blackShadow.shadowOpacity = .4;
blackShadow.shadowOffset = CGSizeMake(0.f, 1.f);
[self.sectionTitleLabel.layer addSublayer:blackShadow];
self.sectionTitleLabel.layer.masksToBounds = NO;
}
With this the white shadow appears, but the black one does not.
I don't understand what do you mean by "two shadows to a UILabel" but I hope I can help. If on this picture you can see what you want, I will be happy :)
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableAttributedString* attString = [[NSMutableAttributedString alloc] initWithString:self.custLabel.text];
NSRange range = NSMakeRange(0, [attString length]);
[attString addAttribute:NSFontAttributeName value:self.custLabel.font range:range];
[attString addAttribute:NSForegroundColorAttributeName value:self.custLabel.textColor range:range];
NSShadow* shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor whiteColor];
shadow.shadowOffset = CGSizeMake(0.0f, 1.0f);
[attString addAttribute:NSShadowAttributeName value:shadow range:range];
self.custLabel.attributedText = attString;
[self nextShadow];
}
-(void)nextShadow
{
self.custLabel.layer.masksToBounds = NO;
self.custLabel.layer.cornerRadius = 5;
self.custLabel.layer.shadowOffset = CGSizeMake(3, 0);
self.custLabel.layer.shadowRadius = 5;
self.custLabel.layer.shadowOpacity = 1.5;
}
I'd try to use
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
But you can't use 2 shadow Attributes, only one. And you can customize
-(void)nextShadow
method to create good solution, for example
-(void)nextShadow
{
self.custLabel.layer.masksToBounds = NO;
self.custLabel.layer.cornerRadius = 1;
self.custLabel.layer.shadowOffset = CGSizeMake(1, 0);
self.custLabel.layer.shadowRadius = 1;
self.custLabel.layer.shadowOpacity = 1.5;
}
If you adjust values in -(void)nextShadow you can get what you want.
I have tried this code to draw a circle of rectangle
incNumberDefault=0;
for (int i = 0; i < maxNumber; i++) {
incNumberDefault = incNumberDefault +5;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(incNumberDefault, 100, 3, 15)];
myView.center = CGPointMake(130,130);
myView.transform = CGAffineTransformMakeRotation(DegreesToRadians(incNumberDefault));
myView.backgroundColor = [UIColor lightGrayColor];
[self.speedDisplay addSubview:myView];
}
the outputs seems close circle where as i need transparent or space in middle of circle.
You can set UIView cornerRadius to half of the UIView width or height like,
myView.layer.cornerRadius=myview.frame.size.width / 2; or
myView.layer.cornerRadius=myview.frame.size.height / 2
For that you have to give same width and height to your UIView.
Try this :
// initialize the view with same height and width for perfect circle.
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 15, 15)];
// round the corner of view with required radius.
myView.layer.cornerRadius = myView.frame.size.width/2; // height can also be used
// circle with space in middle.
myView.backgroundColor = [UIColor clearColor];
myView.layer.borderColor = [UIColor lightGrayColor].CGColor;
myView.layer.borderWidth = 2.0f;
[self.speedDisplay addSubview:myView];
Hope it will solve your problem.
Thank you for your answer but i want to develop a dashed circle in certain distance and i have tired this code :
incNumberDefault=0;
for (int i = 0; i < maxNumber; i++) {
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(100+60 * sin(incNumberDefault), 100+60*cos(incNumberDefault), 3, 15)];
//myView.transform = CGAffineTransformMakeRotation(DegreesToRadians(incNumberDefault));
myView.backgroundColor = [UIColor lightGrayColor];
[self.speedDisplay addSubview:myView];
incNumberDefault = incNumberDefault +9;
}
but it generate random rectangle in a circle.
I want it in sequence.
Thank you.
I have a UILabel that I add a shadow to. The UILabel shows up and so does the shadow, but the leftmost shadow is cut off so that it is in line with the edge of the text. I moved the position of the label to see if it was being covered by a view, but everything stayed the same. I also took out the sizeToFit, and it stayed the same. Here is the initialization of the label:
UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
scoreLabel.text = text;
[scoreLabel setFont:[UIFont fontWithName:fontName size:fontSize]];
scoreLabel.textColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];
scoreLabel.shadowColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f];
scoreLabel.shadowOffset = CGSizeMake(-10.0, 2.0);
scoreLabel.clipsToBounds = NO;
[scoreLabel sizeToFit];
scoreLabel.center = CGPointMake(x, y);
I had this problem with a custom font and solved it by subclassing UILabel and adding the shadowOffset to intrinsicContentSize with this override:
override var intrinsicContentSize: CGSize {
get {
let s = super.intrinsicContentSize
return CGSize(width: s.width + abs(shadowOffset.width), height: s.height + abs(shadowOffset.height))
}
}
I think you need to add
scoreLabel.clipsToBounds = NO;
This is the solution you want:
let style = NSMutableParagraphStyle()
style.firstLineHeadIndent = someInset // can be shadowBlur and/or shadowRadius
style.headIndent = someInset
style.tailIndent = -someInset // must be negative since its at the end (tail)
let attrString = NSMutableAttributedString(string: "Some Text")
attrString.addAttribute(.paragraphStyle, value: style, range: .init(location: 0, attrString.length)
let label = UILabel()
label.numberOfLines = 0 // not necessary
label.attributedText = attrString
I added this and the cut off went away:
scoreLabel.textAlignment = NSTextAlignmentCenter;
I've looked on the web but my results mainly consist of "how to do a shadowOffset." I need to apply "Shadow,Border, and CornerRadius" to multiple objects in my views. I just wanted to see if theres a more effienct way? Or if theres a way to keep it more organized. heres some of my code. Keep in mind I have multiple views like this, so the amount of space this code takes gets pretty annoying.
topView.layer.cornerRadius = 3;
topView.layer.masksToBounds = YES;
topView.layer.borderColor= [UIColor lightGrayColor].CGColor;
topView.layer.borderWidth = 0.5f;
bottomView.layer.cornerRadius = 3;
bottomView.layer.masksToBounds = YES;
bottomView.layer.borderColor= [UIColor lightGrayColor].CGColor;
bottomView.layer.borderWidth = 0.5f;
eventName.layer.masksToBounds = NO;
eventName.layer.shadowColor = [UIColor blackColor].CGColor;
eventName.layer.shadowOpacity = 0.5;
eventName.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
eventName.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
addressLabel.layer.masksToBounds = NO;
addressLabel.layer.shadowColor = [UIColor blackColor].CGColor;
addressLabel.layer.shadowOpacity = 0.5;
addressLabel.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
addressLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
dateLabel.layer.masksToBounds = NO;
dateLabel.layer.shadowColor = [UIColor blackColor].CGColor;
dateLabel.layer.shadowOpacity = 0.5;
dateLabel.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
dateLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
typeLabel.layer.masksToBounds = NO;
typeLabel.layer.shadowColor = [UIColor blackColor].CGColor;
typeLabel.layer.shadowOpacity = 0.5;
typeLabel.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
typeLabel.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
eventCaption.layer.masksToBounds = NO;
eventCaption.layer.shadowColor = [UIColor blackColor].CGColor;
eventCaption.layer.shadowOpacity = 0.5;
eventCaption.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
eventCaption.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
If most of your shadows are identical, you could choose to loop over an array (or a set, since you don't really care about the order of the items it is applied to) of your views to apply the same shadow ?
That is, I see 2 kind of shadows in the code you pasted here, the lightGray and the black.
You could do something like :
NSArray * blackShadowItems = #[eventName, addressLabel, dateLabel];
for (UIView * view in blackShadowItems) {
view.layer.masksToBounds = NO;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOpacity = 0.5;
view.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
view.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
}
or declare another function :
- (void)setBlackShadow:(UIView *)view {
view.layer.masksToBounds = NO;
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOpacity = 0.5;
view.layer.shadowRadius = 2;
//(right,down) also (-right,-down)
view.layer.shadowOffset = CGSizeMake(0.0f, 0.8f);
}
You can combine both of these solutions.
Finally, note that if these views are declared in a .xib file, you could declare an IBOutletCollection to regroup the views according to the type of shadow you want to set on it.
This is some quite similar to declaring your NSArray or NSSet yourself.