How to set a tableview cell's border and color when selected - objective-c

I have the following code that sets sets a red border around a custom table view cell with white in the middle when it is selected.
- (void)awakeFromNib
{
self.nameLabel.highlightedTextColor = [UIColor whiteColor];
// Set selected background view
UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
backgroundView.layer.borderColor = [[UIColor redColor] CGColor];
backgroundView.layer.borderWidth = 10.0f;
self.selectedBackgroundView = backgroundView;
// Set the content view
CGRect frame = CGRectMake(self.bounds.origin.x+5, self.bounds.origin.y+5, self.bounds.size.width-10, self.bounds.size.height-10);
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:frame];
[self addSubview:myImageView];
self.imageView.contentMode = UIViewContentModeScaleAspectFill ;
self.imageView.clipsToBounds = YES;
[self.contentView addSubview:self.imageView];
}
I want the opposite: how would you create a cell that has red with a white border / padding?

You can use
[cell.contentView.layer setBorderColor:[UIColor redColor].CGColor];
[cell.contentView.layer setBorderWidth:2.0f];
Hope this will help you

try this in custom cell there is a method - (void)setSelected:(BOOL)selected animated:(BOOL)animated use this to change the state of selected and deselect state for example
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
if(selected)
{
self.contentView.layer.cornerRadius = 10.0f;
self.contentView.layer.borderWidth = 5.0f;
self.contentView.layer.masksToBounds = YES;
self.contentView.backgroundColor = [UIColor redColor];
self.contentView.layer.borderColor = [UIColor whiteColor].CGColor;
}
else
{
self.contentView.layer.cornerRadius = 10.0f;
self.contentView.layer.borderWidth = 5.0f;
self.contentView.backgroundColor = [UIColor whiteColor];
self.contentView.layer.borderColor = [UIColor redColor].CGColor;
}
// Configure the view for the selected state
}
and also set customCell.selectionStyle = UITableViewCellSelectionStyleNone; for cell while creating the cell

why don't you try doing just the opposite such as this
UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
backgroundView.layer.borderColor = [[UIColor whiteColor] CGColor];
backgroundView.layer.borderWidth = 10.0f;
self.selectedBackgroundView = backgroundView;
// Set the content view
CGRect frame = CGRectMake(self.bounds.origin.x+5, self.bounds.origin.y+5, self.bounds.size.width-10, self.bounds.size.height-10);
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:frame];
myImageView.backgroundcolor=[UIColor redColor];
[self addSubview:myImageView];
self.imageView.contentMode = UIViewContentModeScaleAspectFill ;
self.imageView.clipsToBounds = YES;
[self.contentView addSubview:self.imageView];

Related

Search bar rounded corner

How to change the border of search bar to rounded corner. Below is my code and I need the orange coloured border to be rounded not as sharp rectangle. Please help
// Search bar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 60, 270, 41)];
self.searchBar.delegate = self;
self.searchBar.placeholder = MEGLocalizedString(#"search_hint_home_screen", nil);
UIColor *searchBarBackgroundColor = [UIColor clearColor];
self.searchBar.backgroundImage = [UIImage imageFromColor:searchBarBackgroundColor];
[self.searchBar setImage:[[UIImage imageNamed:#"search_icon_general.png"] imageTintedWithColor:primaryEventColor] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
UIView *textField = [self.searchBar subviewWithKindOfClass:[UITextField class]];
textField.backgroundColor = [UIColor orangeColor];
// textField.layer.borderWidth = 1;
textField.layer.cornerRadius = 14;
[(UITextField *)textField setBorderStyle:UITextBorderStyleNone];
// Change the search bar placeholder text color
[textField setValue:[UIColor whiteColor] forKeyPath:#"_placeholderLabel.textColor"];
}
self.searchBar.layer.cornerRadius = 14;
self.searchBar.layer.borderColor = [UIColor orangeColor].CGColor;
This solve your problem
textField.layer.masksToBounds = true

Make area around masked region transparent with CALayers?

I'm trying to crop an image to an irregular shape, but I need to make the area that was removed transparent.
Inside subclass of UIView
CALayer *myLayer = [CALayer layer];
CAShapeLayer *mask = [CAShapeLayer layer];
myLayer.frame = self.bounds;
myLayer.contents = (id)[self.picture CGImage];
mask.path = path;
myLayer.mask = mask;
[self.layer addSublayer:myLayer];
This will crop the image appropriately, but the background color of the view is white and therefore still visible. I've tried making the other layers transparent, but it has not worked.
(self and subview both refer to same view)
[self layer].backgroundColor = [UIColor clearColor].CGColor //no change
[self layer].opacity = 0; //makes entire view transparent
subView.backgroundColor = [UIColor clearColor]; // entire view becomes transparent
Is it possible to create the effect I'm trying to achieve?
I tried the same thing, here is the exact code:
// In KMViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor lightGrayColor];
CGRect frame = CGRectMake(0.0f, 0.0f, 100.0f, 100.0f);
KMView *view = [[KMView alloc] initWithFrame:frame];
view.center = self.view.center;
[self.view addSubview:view];
}
// In KMView.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// self.backgroundColor = [UIColor clearColor];
CALayer *layer = [CALayer layer];
CAShapeLayer *mask = [CAShapeLayer layer];
layer.frame = self.bounds;
UIImage *image = [UIImage imageNamed:#"orange"];
layer.contents = (id)image.CGImage;
CGPathRef path = CGPathCreateWithEllipseInRect(frame, NULL);
mask.path = path;
CGPathRelease(path);
layer.mask = mask;
[self.layer addSublayer:layer];
}
return self;
}
It worked for me as expected. The background was transparent. Then I tried adding
self.backgroundColor = [UIColor clearColor];
to the beginning of the code and nothing changed, the image was showing on a transparent background clipped to the path.
I think the problem might be somewhere else.

UIScrollView and UIPageControl, what am I doing wrong?

I have a class which is predefining some labels and binding their values in a UIScrollView.
I've managed to show those labels, but now I'm stuck at putting a label at the 2nd part of the ScrollView.
I've pushed my project to gitHub.
I can change the label's place on the already visible part, but I must be overlooking something.
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = _detail.name;
UIColor *bgColor = [UIColor blackColor];
UIColor *txtColor = [UIColor grayColor];
CGRect frame;
frame.origin.x = 0;
frame.origin.y = 0;
frame.size.width = _scrollView.frame.size.width *2;
NSString *phoneNr = (_detail.phoneNr == nil) ? #"Not specified" : _detail.phoneNr;
_telLabel = [self prepareLabel:phoneNr textColor:txtColor bgColor:bgColor page:0 y:telNrYAxis];
_webLabel = [self prepareLabel:#"Visit website" textColor:txtColor bgColor:bgColor page:0 y:websiteYAxis];
_detail.address = [_detail.address stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"\n\t "]];
NSArray *addressArrComponents = [_detail.address componentsSeparatedByString:#","] ;
_addressLabel = [self prepareLabel:[addressArrComponents componentsJoinedByString:#"\n"] textColor:txtColor bgColor:bgColor page:0 y:addressYAxis];
UILabel *lbl = [self prepareLabel:#"Derp" textColor:txtColor bgColor:bgColor page:1 y:0];
_detailView = [[UIView alloc] initWithFrame:frame];
_detailView.backgroundColor = [UIColor blackColor];
[_detailView addSubview:_webLabel];
[_detailView addSubview:_addressLabel];
[_detailView addSubview:_telLabel];
[_detailView addSubview:lbl];
[_scrollView addSubview:_detailView];
NSLog(#"%f",self.view.frame.size.height - (_scrollView.frame.origin.y + _scrollView.frame.size.height) );
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height - 250 , self.view.frame.size.width/4, 120)];
_pageControl.numberOfPages=2;
_pageControl.currentPage=0;
[_pageControl addTarget:self action:#selector(pageChange:) forControlEvents:UIControlEventTouchDown];
_scrollView.contentSize = CGSizeMake(800,800);
_scrollView.delegate=self;
_scrollView.backgroundColor = [UIColor blackColor];
_scrollView.pagingEnabled=YES;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.scrollsToTop = NO;
[self pageChange:0];
[self.view addSubview:_pageControl];
// Do any additional setup after loading the view.
}
-(UILabel*)prepareLabel:(NSString*) text textColor:(UIColor*)textColor bgColor:(UIColor*)backgroundColor page:(int)page y:(int) yPos{
int lines = [[text componentsSeparatedByString:#"\n"] count];
CGRect labelFrame = CGRectMake(_detailView.frame.size.width * page +20,yPos,self.view.frame.size.width, [UIFont systemFontSize]*lines);
UILabel *returnLabel = [[UILabel alloc] initWithFrame:labelFrame];
returnLabel.text = text;
returnLabel.backgroundColor = backgroundColor;
returnLabel.textColor = textColor;
[returnLabel setNumberOfLines:lines];
[returnLabel sizeToFit];
return returnLabel;
}
- (void)loadScrollViewWithPage:(int)page {
NSLog(#"Derped");
}
-(IBAction)pageChange:(id)sender{
int page=_pageControl.currentPage;
CGRect frame = _scrollView.frame;
frame.origin.x = _scrollView.frame.size.width * page;
frame.origin.y = 0;
//CGRect frame= (page == 0) ? _detailFrame : _reviewFrame;
NSLog(#"%f",frame.origin.x);
[_scrollView scrollRectToVisible:frame animated:YES];
}
The delegate -(IBAction)pageChange:(id)sender gets fired, but I must be doing something wrong with the frames somewhere :s
Please take a look!
Try to implement this method may help you :
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = self.scrollView.frame.size.width;
float fractionalPage = self.scrollView.contentOffset.x / pageWidth;
NSInteger page = lround(fractionalPage);
self.pageControl.currentPage = page;
}

custom tableview and highlightedTextColor

i have a table view, with custom cell, i have set the cell for have a highlighted color on text when you tap on it.
//cell specific
NSString *ligneTableau = [NSString stringWithFormat:#"%#", [[table objectAtIndex:indexPath.row] nome]];
cell.label.text = ligneTableau;
cell.label.font = [UIFont fontWithName:#"populaire" size:35];
cell.label.textColor = [UIColor colorWithRed:124.0f/255.0f green:153.0f/255.0f blue:106.0f/255.0f alpha:1.0f];
cell.fondo.image = [UIImage imageNamed: #"cell_ant.png"];
//highlighted Text
cell.label.highlightedTextColor = [UIColor colorWithRed:55.0f/255.0f green:70.0f/255.0f blue:48.0f/255.0f alpha:1.0f];
every things work fine, but when come back to the table the text stay highlighted.
I forget somethings?
Probably the cell remains selected when you go back to table. So as soon as come back to table you should set deselected.
[cell setSelected:NO];
In your UpdatesTableViewCell class, you can implement the following methods:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
[super setHighlighted:highlighted animated:animated];
if (highlighted)
self.label.textColor = [UIColor colorWithRed:55.0f/255.0f green:70.0f/255.0f blue:48.0f/255.0f alpha:1.0f];
else
self.label.textColor = [UIColor colorWithRed:124.0f/255.0f green:153.0f/255.0f blue:106.0f/255.0f alpha:1.0f];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
if (selected)
self.label.textColor = [UIColor colorWithRed:55.0f/255.0f green:70.0f/255.0f blue:48.0f/255.0f alpha:1.0f];
else
self.label.textColor = [UIColor colorWithRed:124.0f/255.0f green:153.0f/255.0f blue:106.0f/255.0f alpha:1.0f];
}

Adding a UILabel to UiTableViewCell - text is being cropped vertically

Running into a problem when I try and add a UILabel to a custom UITableViewCell. For some reason the frame's are not being respected at all - when I try and set the label height (through CGRectMake) the text is being cropped vertically. In other words, only half of the text is being shown.
When I play around with the CGRectMake's frame height it shows some strange behavior - sometimes a lower value will actually make things align properly.
Am I setting the wrong values? Is the frame the wrong property to be modifying?
Edit for code:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
// Initialization code
eventName = [[UILabel alloc] initWithFrame:CGRectZero];
//eventName.backgroundColor = [UIColor blueColor];
eventName.numberOfLines = 2;
eventName.minimumFontSize = 8.;
eventName.adjustsFontSizeToFitWidth = YES;
eventName.text = #"Event Name";
[self.contentView addSubview:eventName];
eventStartEndDate = [[UILabel alloc] initWithFrame:CGRectZero];
eventStartEndDate.backgroundColor = [UIColor blueColor];
eventStartEndDate.text = #"Event Start and End Dates";
[self.contentView addSubview:eventStartEndDate];
description = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 30.0, 300.0, 20.0)];
[self.contentView addSubview:description];
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
if (!self.editing) {
eventName.frame = CGRectMake(10.0, 10.0, 300.0, 20.0);
eventStartEndDate.frame = CGRectMake(10.0, 35.0, 300.0, 20.0);;
}
}
First of all, you should pay more attention to memory management. You have to call release on your UILabels.
The reason, why your UILabels sometimes seem clipped is, because your other labels do have a white, opaque background. Setting the backgroundColor property to [UIColor clearColor] does the trick.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
eventName = [[UILabel alloc] initWithFrame:CGRectZero];
//eventName.backgroundColor = [UIColor blueColor];
eventName.numberOfLines = 2;
eventName.minimumFontSize = 8.;
eventName.adjustsFontSizeToFitWidth = YES;
eventName.text = #"Event Name";
eventName.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:eventName];
[eventName release];
eventStartEndDate = [[UILabel alloc] initWithFrame:CGRectZero];
eventStartEndDate.backgroundColor = [UIColor blueColor];
eventStartEndDate.text = #"Event Start and End Dates";
[self.contentView addSubview:eventStartEndDate];
[eventStartEndDate release];
description = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 30.0, 300.0, 20.0)];
description.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:description];
[description release];
}
return self;
}