Using CAShapeLayer fill color issue - ios

I want to draw a circle with text inside.Unable to display text.Any help?
Below image is ref. for expected behaviour.
Below is the code for reference:
UILabel *lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(100, 40, 40, 40)];
lblTitle.text = #"Me";
lblTitle.textColor = [UIColor blackColor];
lblTitle.font = [UIFont fontWithName:#"HelveticaNeueLight" size:10.0] ;
lblTitle.textAlignment = NSTextAlignmentCenter;
[view addSubview:lblTitle];
CAShapeLayer *circleLayer = [CAShapeLayer layer];
circleLayer.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 40, 40)].CGPath;
circleLayer.fillColor = [UIColor clearColor].CGColor;
circleLayer.fillColor = [UIColor colorWithRed:56.0/255.0 green:212.0/255.0 blue:203.0/255.0 alpha:1.0f].CGColor;
circleLayer.strokeColor = [UIColor blackColor].CGColor;
circleLayer.lineWidth = 1;
[lblTitle.layer addSublayer:circleLayer];

You can setting cornerRadius of UILabel as #Sujay said:
UILabel *lblTitle = [[UILabel alloc]initWithFrame:CGRectMake(100, 80, 40, 40)];
lblTitle.text = #"Me";
lblTitle.textColor = [UIColor blackColor];
lblTitle.layer.borderWidth = 1;
lblTitle.layer.borderColor = [UIColor blackColor].CGColor;
lblTitle.layer.cornerRadius = lblTitle.bounds.size.height / 2;
lblTitle.layer.masksToBounds = YES;
lblTitle.backgroundColor = [UIColor colorWithRed:56.0/255.0 green:212.0/255.0 blue:203.0/255.0 alpha:1.0f];
lblTitle.font = [UIFont fontWithName:#"HelveticaNeueLight" size:10.0] ;
lblTitle.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:lblTitle];

Related

Tableview not scrolling smooth with static data

I am using custom cell and headerView with static images and footerView but not scrolling as smoothly as it should scroll, it is getting stuck in middle and then scrolls........
Header
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerview = [[UIView alloc]init];
if (section == 2) {
headerview.frame = CGRectMake(0, 30, tableView.frame.size.width, 117);
Button = [[UIButton alloc]initWithFrame:CGRectMake(0, 18, headerview.frame.size.width, 100)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, tableView.frame.size.width, 17)];
label.text = #"Siddharth and hiren Like this";
label.font = [UIFont systemFontOfSize:10];
label.textColor = [UIColor blackColor];
[headerview addSubview:label];
}
else
{
headerview.frame = CGRectMake(0, 0, tableView.frame.size.width, 100);
Button = [[UIButton alloc]initWithFrame:headerview.frame];
}
// sheap top left and top right
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:headerview.bounds
byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)
cornerRadii:CGSizeMake(7.0, 7.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
headerview.frame = headerview.bounds;
maskLayer.path = maskPath.CGPath;
headerview.layer.mask = maskLayer;
// shape for button bottom
headerview.backgroundColor = [UIColor whiteColor];
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, 0, headerview.frame.size.width, 1);
bottomBorder.backgroundColor = [UIColor colorWithRed:217/255.0f green:217/255.0f blue:217/255.0f alpha:1].CGColor;
bottomBorder.shadowColor = [[UIColor blackColor] CGColor];
bottomBorder.shadowOpacity = 0.9f;
bottomBorder.shadowRadius = 0.3f;
[headerview.layer addSublayer:bottomBorder];
headerview.layer.shouldRasterize = YES;
// Button Imageview
UIImageView *buttonimg = [[UIImageView alloc]initWithFrame:Button.bounds];
buttonimg.image = [UIImage imageNamed:[_SecImgArray objectAtIndex:section]];
buttonimg.layer.shouldRasterize = YES;
[Button addSubview:buttonimg];
// imageview overlay
UIView *overlay = [[UIView alloc]initWithFrame:Button.bounds];
overlay.alpha = 0.3;
overlay.backgroundColor = [UIColor blackColor];
overlay.layer.shouldRasterize = YES;
[buttonimg addSubview:overlay];
[Button addTarget:self action:#selector(Expand: and:) forControlEvents:UIControlEventTouchUpInside];
Button.tag = section;
if ([[AllNewsdic objectForKey:#"posts"] count] == 0) {
[Button setTitle:[[[DownloadData objectForKey:#"posts"]objectAtIndex:section]objectForKey:#"title"] forState:UIControlStateNormal];
}
else
{
[Button setTitle:[[[AllNewsdic objectForKey:#"posts"]objectAtIndex:section]objectForKey:#"title"] forState:UIControlStateNormal];
}
[Button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
Button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
Button.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
Button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
Button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
Button.titleLabel.numberOfLines = 0;
Button.titleLabel.font = [UIFont fontWithName:#"Avenir-Black" size:12];
Button.layer.shadowColor = [UIColor greenColor].CGColor;
Button.layer.shadowOpacity = 0.4;
Button.layer.shadowRadius = 1.0f;
Button.layer.cornerRadius = 7;
Button.layer.masksToBounds = YES;
Button.layer.shouldRasterize = YES;
[headerview addSubview:Button];
return headerview;}
Footer
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UIView *fotter = [[UIView alloc]initWithFrame:CGRectMake(0, -10, tableView.frame.size.width, 30)];
fotter.backgroundColor = [UIColor whiteColor];
// corner redius
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:fotter.bounds
byRoundingCorners:(UIRectCornerBottomLeft|UIRectCornerBottomRight)
cornerRadii:CGSizeMake(7.0, 7.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
fotter.frame = fotter.bounds;
maskLayer.path = maskPath.CGPath;
// maskLayer.backgroundColor = [UIColor colorWithRed:217/255.0f green:217/255.0f blue:217/255.0f alpha:1].CGColor;
fotter.layer.mask = maskLayer;
// Bottom layer
CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, 32, fotter.frame.size.width, 2);
bottomBorder.backgroundColor = [UIColor blackColor].CGColor;
[fotter.layer addSublayer:bottomBorder];
// top Layer
CALayer *topLayer = [CALayer layer];
topLayer.frame = CGRectMake(13, 0, fotter.frame.size.width - 26, 0.7);
topLayer.backgroundColor = [UIColor colorWithRed:197/255.0f green:187/255.0f blue:188/255.0f alpha:1].CGColor;
topLayer.shadowOpacity = 0.6f;
topLayer.shadowRadius = 0.5f;
[fotter.layer addSublayer:topLayer];
// Like bUtton
likebutton = [[UIButton alloc]initWithFrame:CGRectMake(0, -3, fotter.frame.size.width/2, fotter.frame.size.height + 3)];
likebutton.titleLabel.font = [UIFont systemFontOfSize:10];
likebutton.titleLabel.tintColor = [UIColor colorWithRed:197/255.0f green:187/255.0f blue:188/255.0f alpha:1];
[likebutton addTarget:self action:#selector(like:) forControlEvents:UIControlEventTouchUpInside];
[likebutton setTitle:#"1 likes" forState:UIControlStateNormal];
likebutton.tag = section;
like = [[UIImageView alloc]initWithFrame:CGRectMake(40 ,8, 15, 15)];
like.image = [UIImage imageNamed:#"SimpleLike.png"];
[likebutton addSubview:like];
[likebutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[fotter addSubview:likebutton];
// Follow Button
NSLog(#"%f",fotter.frame.size.width/2 + 1);
follow = [[UIButton alloc]initWithFrame:CGRectMake(fotter.frame.size.width/2 , -3, fotter.frame.size.width/2, fotter.frame.size.height + 3)];
follow.titleLabel.font = [UIFont systemFontOfSize:10];
follow.titleLabel.tintColor = [UIColor colorWithRed:197/255.0f green:187/255.0f blue:188/255.0f alpha:1];
[follow setTitle:#"Follow" forState:UIControlStateNormal];
followimage = [[UIImageView alloc]initWithFrame:CGRectMake(35 ,8, 17, 17)];
followimage.image = [UIImage imageNamed:#"Follow.png"];
[follow addSubview:followimage];
[follow setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
follow.tag = section;
[follow addTarget:self action:#selector(Follow:) forControlEvents:UIControlEventTouchUpInside];
[fotter addSubview:follow];
return fotter;}
Cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
AllNewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"AllNews"];
cell.one.text = [one objectAtIndex:indexPath.section];
cell.two.text = [two objectAtIndex:indexPath.section];
cell.three.text = [three objectAtIndex:indexPath.section];
[cell.three sizeToFit];
return cell;}
how to set tableview scroll smooth and effective
how to set interface properties
tableview data is static
Use reusable UITableViewCell inside function viewForHeaderInSection and inside viewForFooterInSection
Do not create the new UIView and buttons for each function call.
Your table view scrolling is not smooth because the views inside viewForHeaderInSection and viewForFooterInSection methods are allocated multiple times. Why ? Because whenever you scroll up and down, both the methods are called as and when required.
Check whether header view is already allocated. If it is nil, then and then allocate it and return it. Otherwise, just return it. Same way in case of footer view.
Add comment in case of any doubt..

text transparency|color gradient

I am trying to put transparency (or a color gradient) on the title of an UIButton. I don't know how do that. I have tried to simply put a clearColor on the titleColor but we only see the background color.
So i hope that there is someone here who will be an amazing idea for my problem.
My solution
self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.loginButton.backgroundColor= [UIColor whiteColor];
self.loginButton.frame = CGRectMake(self.signUpButton.left,
0,
self.signUpButton.frame.size.width,
self.signUpButton.frame.size.height);
[self.loginButton.layer setBackgroundColor:[[UIColor blackColor] CGColor]];
self.loginButton.layer.borderColor = [[UIColor whiteColor] CGColor];
self.loginButton.layer.borderWidth = 1.0f;
self.loginButton.layer.cornerRadius = 3.0f;
[self.loginButton addTarget:self action:#selector(loginButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.loginButtonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
self.loginButtonLabel.font = [UIFont fontWithName:#"OpenSans-Light" size:16.0f];
self.loginButtonLabel.textColor = [UIColor whiteColor];
self.loginButtonLabel.text = #"Connexion";
[self.loginButtonLabel sizeToFit];
UIColor *topColor = [UIColor blueColor];
UIColor *bottomColor = [UIColor orangeColor];
self.gradientView = [[UIView alloc] initWithFrame:self.loginButtonLabel.frame];
[self.loginButton addSubview:self.gradientView];
[self.loginButton sendSubviewToBack:self.gradientView];
[self.loginButton addSubview:self.loginButtonLabel];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.gradientView.frame;
gradient.colors = [NSArray arrayWithObjects:(id)topColor.CGColor, (id)bottomColor.CGColor, nil];
gradient.startPoint = CGPointMake(0, 0.5);
gradient.endPoint = CGPointMake(1, 0.5);
[self.gradientView.layer addSublayer:gradient];
self.gradientView.layer.mask = self.loginButtonLabel.layer;
self.gradientView.layer.masksToBounds = YES;
self.gradientView.center = CGPointMake(self.loginButton.width / 2, self.loginButton.height / 2);
[self.view addSubview:self.loginButton];
But Now i need to put the gradient on the border. Any idea ?
So my solution is to create a custom button with an UILabel. I will use an UIView with a gradient for color the text.
The solution :
self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.loginButton.backgroundColor= [UIColor whiteColor];
self.loginButton.frame = CGRectMake(self.signUpButton.left,
0,
self.signUpButton.frame.size.width,
self.signUpButton.frame.size.height);
[self.loginButton.layer setBackgroundColor:[[UIColor blackColor] CGColor]];
self.loginButton.layer.borderColor = [[UIColor whiteColor] CGColor];
self.loginButton.layer.borderWidth = 1.0f;
self.loginButton.layer.cornerRadius = 3.0f;
[self.loginButton addTarget:self action:#selector(loginButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
self.loginButtonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
self.loginButtonLabel.font = [UIFont fontWithName:#"OpenSans-Light" size:16.0f];
self.loginButtonLabel.textColor = [UIColor whiteColor];
self.loginButtonLabel.text = #"Connexion";
[self.loginButtonLabel sizeToFit];
UIColor *topColor = [UIColor blueColor];
UIColor *bottomColor = [UIColor orangeColor];
self.gradientView = [[UIView alloc] initWithFrame:self.loginButtonLabel.frame];
[self.loginButton addSubview:self.gradientView];
[self.loginButton sendSubviewToBack:self.gradientView];
[self.loginButton addSubview:self.loginButtonLabel];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.gradientView.frame;
gradient.colors = [NSArray arrayWithObjects:(id)topColor.CGColor, (id)bottomColor.CGColor, nil];
gradient.startPoint = CGPointMake(0, 0.5);
gradient.endPoint = CGPointMake(1, 0.5);
[self.gradientView.layer addSublayer:gradient];
self.gradientView.layer.mask = self.loginButtonLabel.layer;
self.gradientView.layer.masksToBounds = YES;
self.gradientView.center = CGPointMake(self.loginButton.width / 2, self.loginButton.height / 2);
[self.view addSubview:self.loginButton];

How to draw UILabel with padding text not hidden?

I will drawing UILabel with code
CustomLabel *label = [[CustomLabel alloc] initWithFrame:CGRectMake(0, 0, 260, 100)];
label.layer.borderColor = [[UIColor redColor] CGColor];
label.layer.borderWidth = 3.0;
[label setFont:[UIFont fontWithName:#"Helvetica" size:16]];
CALayer * l1 = [label layer];
[l1 setMasksToBounds:YES];
[l1 setCornerRadius:10.0];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentLeft; // UITextAlignmentCenter, UITextAlignmentLeft
label.textColor=[UIColor whiteColor];
label.backgroundColor = [UIColor redColor];
label.numberOfLines=4;
label.lineBreakMode=NSLineBreakByWordWrapping;
label.text = [NSString stringWithFormat:#"%#", message];
label.tag = index;
[label sizeToFit];
And of course, i will found a solution from stackoverflow for create padding for label
with code
- (id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
self.edgeInsets = UIEdgeInsetsMake(5,5,5,5); //customize padding here
}
return self;
}
- (void)drawTextInRect:(CGRect)rect {
[super drawTextInRect:UIEdgeInsetsInsetRect(rect, self.edgeInsets)];
}
Everything working fine, but text in my label showing not good, here is my label screenshot.
I want like this.
Please help and thanks for your time.
you would want to have a look into this library.. its awesome and totally configurable...
https://github.com/AlexBarinov/UIBubbleTableView

Custom drawing UICollectionViewCell subViews for performance optimization

In my UICollectionViewCell subclass I have this code to layout the subViews I want:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(97, 10, 190, 40)];
_titleLabel.font = [UIFont boldFlatFontOfSize:22];
_titleLabel.backgroundColor = [UIColor clearColor];
_fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(97, 35, 190, 30)];
_fromLabel.font = [UIFont flatFontOfSize:14];
_fromLabel.backgroundColor = [UIColor clearColor];
UIView *containerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 90)];
containerView.backgroundColor = [[UIColor colorWithPatternImage:[UIImage imageNamed:#"cream_pixels"]] colorWithAlphaComponent:0.8];
[self.contentView addSubview:containerView];
_iconView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[_iconView setCenter:containerView.center];
_iconView.backgroundColor = [UIColor whiteColor];
_iconView.contentMode = UIViewContentModeScaleAspectFit;
_iconView.image = [UIImage imageNamed:#"envelopeIcon.png"];
_iconView.backgroundColor = [UIColor clearColor];
[containerView addSubview:_iconView];
_dateLabel = [[UILabel alloc]initWithFrame:CGRectMake(97, self.contentView.frame.size.height - 23, self.contentView.frame.size.width-97, 25)];
[_dateLabel setBackgroundColor:[UIColor clearColor]];
[_dateLabel setFont:[UIFont boldFlatFontOfSize:13]];
[_dateLabel setTextColor:[[UIColor wetAsphaltColor] colorWithAlphaComponent:0.6f]];
[_dateLabel setTextAlignment:NSTextAlignmentLeft];
[_dateLabel setLineBreakMode:NSLineBreakByCharWrapping];
[_dateLabel setNumberOfLines:0];
_typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0.f, 0.f , 4, self.contentView.frame.size.height)];
_typeLabel.layer.cornerRadius = 0.f;
_typeLabel.backgroundColor = [UIColor belizeHoleColor];
[self.contentView addSubview:_titleLabel];
[self.contentView addSubview:_fromLabel];
[self.contentView addSubview:_typeLabel];
[self.contentView addSubview:_dateLabel];
self.backgroundColor = [[UIColor cloudsColor] colorWithAlphaComponent:1.f];
self.layer.cornerRadius = 3.2f;
self.layer.masksToBounds = NO;
self.layer.contentsScale = [UIScreen mainScreen].scale;
self.layer.borderColor = [[UIColor silverColor] colorWithAlphaComponent:0.2].CGColor;
self.layer.borderWidth = 0.5f;
self.layer.shadowOpacity = 0.35f;
self.layer.shadowRadius = 3.f;
self.layer.shadowOffset = CGSizeMake(0, 3.f);
self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
self.layer.shouldRasterize = YES;
[_titleLabel setText:#"Nuevo Mensaje"];
}
return self;
}
I understand that for performance, it's recommended that the subviews are added to the cell using custom drawing, and not by layering the contents of it.
How can I achieve this?

UIView not visible in UITableView Header

Why won't my UIView (created below) show up in the UITableViewHeader that I try to add it to?
UIView *view = [[UIView alloc] initWithFrame: CGRectMake ( 20, 20, 400, 400)];
searchBar = (UISearchBar *)self.tableView.tableHeaderView;
[searchBar.subviews objectAtIndex:0] removeFromSuperview];
searchBar.backgroundColor = [UIColor clearColor];
[view addSubview:searchBar];
self.tableView.tableHeaderView = nil;
view.backgroundColor = [UIColor redColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, self.view.frame.size.width, 60)];
label.shadowColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:.35];
label.shadowOffset = CGSizeMake(0, -1.0);
label.text = #"188 People";
label.textColor = [UIColor grayColor];
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12.0];
label.backgroundColor = [UIColor clearColor];
[view addSubview:label];
self.tableView.tableHeaderView = view;
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame: CGRectMake ( 20, 20, 400, 400)];
searchBar = (UISearchBar *)self.tableView.tableHeaderView;
[searchBar.subviews objectAtIndex:0] removeFromSuperview];
searchBar.backgroundColor = [UIColor clearColor];
[view addSubview:searchBar];
view.backgroundColor = [UIColor redColor];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 60, self.view.frame.size.width, 60)];
label.shadowColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:.35];
label.shadowOffset = CGSizeMake(0, -1.0);
label.text = #"188 People";
label.textColor = [UIColor grayColor];
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:12.0];
label.backgroundColor = [UIColor clearColor];
[view addSubview:label];
return view;//Please note the header will look the same for each section. You can add a switch or if/else to augment this
}
-As a side note, if you're planning on using the label here to set the text for the header, I suggest looking into the following methods:
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

Resources