Adding a UILabel to UiTableViewCell - text is being cropped vertically - objective-c

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;
}

Related

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

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];

How to set property of UITextField at UITextField subclass

I have this subclass of UITextField and I would like to set a property as background color, border etc of UITextField. I don't know, if I use right method, because when I use this class for a UItextField, the UITextField doesn't change.. In which method I have to declare this properties?
#import "defaultUITextField.h"
#implementation defaultUITextField
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.delegate = self;
[self setBackgroundColor:([UIColor redColor])];
UIColor *borderColor = [UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:233.0/255.0];
self.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
self.leftViewMode = UITextFieldViewModeAlways;
self.layer.borderColor = [borderColor CGColor];
self.layer.borderWidth = 1.0f;
}
return self;
}
#end
your solution seems to be right.
how did you place your suctom TextField on a View?
If you created your TextField in Interface Builder then you overrided the wrong constructor.
- (id)initWithCoder:(NSCoder *)inCoder {
if (self = [super initWithCoder:inCoder]) {
self.delegate = self;
[self setBackgroundColor:([UIColor redColor])];
UIColor *borderColor = [UIColor colorWithRed:233.0/255.0 green:233.0/255.0 blue:233.0/255.0 alpha:233.0/255.0];
self.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 20)];
self.leftViewMode = UITextFieldViewModeAlways;
self.layer.borderColor = [borderColor CGColor];
self.layer.borderWidth = 1.0f;
}
return self;
}
this constructor is called when you create UITextField in IB and change its class to UItextField.

Cannot change the Y cordinate of the UILabel programmatically

hi Im programmatically creating an UILabel like this
`
- (UILabel *)titleLabel {
if (!_titleLabel) {
_titleLabel = [[[UILabel alloc] init] initWithFrame:CGRectMake(0.0, _photoView.frame.size.height, _photoView.frame.size.width, 100.0)];
// _titleLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
_titleLabel.backgroundColor =[UIColor clearColor];
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14];
_titleLabel.textAlignment = NSTextAlignmentRight;
[self addSubview:_titleLabel];
}
return _titleLabel;
}`
this _photoView is an UIImageView I have created already. I want to change the UILabel View Y value. But the problem is when I change this second parameter label y position is not changing. Any one can tell e the reason for this.
And this is how I created the ImageView
`
- (UIImageView *)photoView {
if (!_photoView) {
_photoView = [[UIImageView alloc] init];
_photoView.contentMode = UIViewContentModeScaleAspectFill;
_photoView.clipsToBounds = YES;
_photoView.layer.cornerRadius = 5;
_photoView.clipsToBounds = YES;
[self addSubview:_photoView];
}
return _photoView;
}`
Thanks
In this line , you have used two types init
[[[UILabel alloc] init] initWithFrame:CGRectMake(0.0, _photoView.frame.size.height, _photoView.frame.size.width, 100.0)]
change this line to
[[UILabel alloc] initWithFrame:CGRectMake(0.0, _photoView.frame.size.height, _photoView.frame.size.width, 100.0)]

text format trouble objective-c

need to make effect, that text is interrupted by another text/uiimageview, but cannot understand how does it works. For example i need to make interface similar to ios7 status bar where operator name such a "Oper ..." + icon + time. So i cannot do this right way
operatorName = [self getOperatorName];
UILabel *operatorLabel = [[UILabel alloc] init];
operatorLabel.backgroundColor = [UIColor clearColor];
operatorLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:kStatusBarFontOperatorSize];
operatorLabel.textColor = kStatusBarTextColor;
operatorLabel.shadowOffset = CGSizeMake(0.f, -0.6);
operatorLabel.shadowColor = kStatusBarTextShadow;
operatorLabel.adjustsFontSizeToFitWidth = YES;
operatorLabel.text = operatorName;
operatorLabel.frame = CGRectMake(0.0, 0.0, operatorStrSize.width, operatorStrSize.height);
[operatorLabel sizeToFit];
/* connection type */
UIImageView *conImgView = [[UIImageView alloc] initWithImage:conImg];
/* time in status bar */
time = [self getStatusBarTime];
UILabel *statusBarLabel = [[UILabel alloc] init];
statusBarLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:kStatusBarFontSize];
statusBarLabel.textColor = kStatusBarTextColor;
statusBarLabel.adjustsFontSizeToFitWidth = YES;
statusBarLabel.text = time;
int maxDistance = imgView.frame.size.width/2 - timeStrSize.width/2;
int connectionPower = 44;
double delimiter = 0;
NSString *cName = [self returnChoosenConnectionName];
if ([cName isEqualToString:#"Wi-Fi"]) {
delimiter = 6.5;
} else {
delimiter = 9.5;
}
int fullLine = connectionPower + operatorLabel.frame.size.width + delimiter + conImgView.frame.size.width;
if (fullLine > maxDistance) {
// need to interrupt text in operator name, but how ?
} else {
// all good placed
x = 44.0;
operatorLabel.frame = CGRectMake(x, 3.0, operatorStrSize.width , operatorStrSize.height);
[operatorLabel sizeToFit];
NSString *cName = [self returnChoosenConnectionName];
if ([cName isEqualToString:#"Wi-Fi"]) {
x += operatorLabel.frame.size.width + 6.5;
} else {
x += operatorLabel.frame.size.width + 9.5;
}
conImgView.frame = CGRectMake(x, 0.0, conImgView.frame.size.width, conImgView.frame.size.height);
[imgView addSubview:operatorLabel];
[imgView addSubview:conImgView];
}
statusBarLabel.frame = CGRectMake(imgView.frame.size.width/2 - timeStrSize.width/2, 2.5, timeStrSize.width , timeStrSize.height);
[imgView addSubview:statusBarLabel];
What i need:
what i have:
I would suggest to use autolayout rather than trying to calculate everything yourself. You may use XIB and set constraints in there or you may do everything programmatically. Here is a sample code that will get you started. It creates controls and sets constraints programatically; it doesn't use XIB.
- (void)viewDidLoad
{
[super viewDidLoad];
/*
Uncomment this if you would like to translate your subviews vertically.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, 50)];
[self.view addSubview:view];
UIView *superview = view;
*/
UIView *superview = self.view;
UILabel *label1 = [[UILabel alloc] init];
label1.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:14.0];
label1.text = #"MTS";
[label1 sizeToFit];
[label1 setTranslatesAutoresizingMaskIntoConstraints:NO];
UILabel *label2 = [[UILabel alloc] init];
label2.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:14.0];
label2.text = #"1:22 PM";
[label2 sizeToFit];
[label2 setTranslatesAutoresizingMaskIntoConstraints:NO];
UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"strength.png"]];
UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"carrier.png"]];
[image1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[image2 setTranslatesAutoresizingMaskIntoConstraints:NO];
[image1 sizeToFit];
[image2 sizeToFit];
[superview addSubview:image1];
[superview addSubview:label1];
[superview addSubview:image2];
[superview addSubview:label2];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(image1, image2, label1, label2);
NSString *format = [NSString stringWithFormat:#"|-[image1(<=%f)]-[label1]-[image2(<=%f)]-[label2(>=20)]-|",
image1.frame.size.width, image2.frame.size.width];
NSArray *constraints;
constraints = [NSLayoutConstraint constraintsWithVisualFormat:format
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraints];
}
When adding views to a layout in code iOS will attempt to convert the autosizing mask for that view to auto layout constraints. Those auto-generated constraints will conflict with any constraints added within the application code. It is essential to turn the translation off:
setTranslatesAutoresizingMaskIntoConstraints:NO
The result of the code is:

objective c - hide a custom annotation view

I want to have a custom Annotation view that behaves exactly as the standard one, but mine needs to have an image inside and several texts, that's why I've implemented the tutorial on http://developer.apple.com/library/ios/#samplecode/WeatherMap/Introduction/Intro.html
But my problem is that I want the annotation view to hide and just show a pin, same thing as the default annotation view behaves but all annotations are showing and I cant figure out a way to hide them.
Any ideas?
Thanks.
[EDIT]
My current implementation of viewForAnnotation is:
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation{
NSLog(#"Item añadido");
static NSString *AnnotationViewID = #"annotationViewID";
CustomMKAnnotationView *annotationView =
(CustomMKAnnotationView *)[mapa dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
if (annotationView == nil)
{
annotationView = [[[CustomMKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID] autorelease];
}
annotationView.annotation = annotation;
return annotationView;
}
Becuase I need the standard bubble but with an image and a couple of UILabels. But I would like to keep the standard behaviour, that is, there's a pin when the bubble is not showing, and when you tap it shows the bubble. The content of my custom bubble is implemented in "CustomMKAnnotationView".
Which is as follows:
- (id)initWithAnnotation:(id <MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self != nil)
{
CGRect frame = self.frame;
frame.size = CGSizeMake(10.0, 10.0);
self.frame = frame;
// self. = [super pincolor];
self.backgroundColor = [UIColor clearColor];
self.centerOffset = CGPointMake(10.0, 10.0);
}
return self;
}
- (void)drawRect:(CGRect)rect{
CustomMKAnnotation *custom = (CustomMKAnnotation *)self.annotation;
if (custom != nil)
{
NSLog(#"El nombre es: %#", [custom nombre]);
UILabel *nombre = [[UILabel alloc]init];
UILabel *media = [[UILabel alloc]init];
PDColoredProgressView *barrita = [[PDColoredProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
[barrita setTintColor:[UIColor colorWithRed:0.6 green:0.83 blue:0.91 alpha:1.0f]];
nombre.textColor = [UIColor whiteColor];
media.textColor = [UIColor whiteColor];
nombre.font = [UIFont fontWithName:#"DIN-Bold" size:14];
media.font = [UIFont fontWithName:#"DIN-Medium" size:12];
CGSize size = [[custom nombre] sizeWithFont:nombre.font constrainedToSize:CGSizeMake(300, 20) lineBreakMode:nombre.lineBreakMode];
NSLog(#"el ancho es: %f y alto %f", size.width, size.height);
nombre.backgroundColor = [UIColor clearColor];
media.backgroundColor = [UIColor clearColor];
nombre.text = [custom nombre];
barrita.progress = [custom gente];
media.text = [NSString stringWithFormat:#"Media %# años", [custom media]];
UIImageView *fondo = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"bubble_map.png"]];
nombre.frame = CGRectMake(10, 10, size.width, size.height);
media.frame = CGRectMake(10, size.height + 10, size.width, size.height);
barrita.frame = CGRectMake(10, media.frame.origin.y + 20, size.width, 10);
fondo.frame = CGRectMake(-((size.width+ 20.0f)/2), -((size.height +10)*2 + 20)-10, size.width+ 20.0f, (size.height +10)*2 + 20);
fondo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
nombre.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[fondo addSubview:nombre];
[fondo addSubview:media];
[fondo addSubview:barrita];
[self addSubview:fondo];
[fondo release];
[nombre release];
[media release];
}
}
If you mean hiding the details of pin, have you tried creating a custom MKPinAnnotationView and set its property of canShowCallout=NO; ?
In your mapview delegate method :
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView*pinView;
if([annotation isKindOfClass:[<yourannotationclass> class]])
{
static NSString*annoIdentifier=#"AnnotationIdentifier";
pinView=(MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:annoIdentifier];
if(pinView==nil)
{
pinView=[[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:annoIdentifier]autorelease ];
}
pinView.animatesDrop=NO;
pinView.canShowCallout=NO;
pinView.pinColor=MKPinAnnotationColorRed;
}
return pinView;
}