I try to change the font in the title with this code lines.
how can I make this work ?
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title;
UIFont *font;
title = [[countriesToCitiesDic objectForKey:selectedCountryName]objectAtIndex:row];
font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18];
UIColor *textColor;
textColor = [UIColor whiteColor];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = #{NSForegroundColorAttributeName : textColor,
NSFontAttributeName : font,
NSParagraphStyleAttributeName : paragraphStyle};
return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}
You can try this:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, pickerView.frame.size.width, 44)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18];
label.text = [[countriesToCitiesDic objectForKey:selectedCountryName]objectAtIndex:row];
return label;
}
I tried this and it works:
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *title = [[countriesToCitiesDic objectForKey:selectedCountryName]objectAtIndex:row];
UIFont *font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18];
UIColor *textColor = [UIColor whiteColor];
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentCenter;
NSDictionary *attributes = #{NSForegroundColorAttributeName : textColor,
NSFontAttributeName : font,
NSParagraphStyleAttributeName : paragraphStyle};
return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}
Related
I am using custom font and uppercased text for navigation item title text which contain diacritics. Problem is, the diacritics is cut from top, probably because the title label has wrong height.
So question is, can I affect the title label height? Or access directly the title label and do something like sizeToFit, or change frame or something.
// custom font settings
NSDictionary *attr = #{
NSFontAttributeName: [UIFont fontWithName:#"FishmongerK-XCondLight" size:23.0],
NSForegroundColorAttributeName: [UIColor whiteColor]
};
[[UINavigationBar appearance] setTitleTextAttributes: attr];
// then setting of title
self.navigationItem.title = [NSLocalizedString(#"Oblíbené", nil) uppercaseString];
If i try smaller font size, diacritics is still cut from top.
-(void)setCustomNavTitle:(NSString*)title andDescription:(NSString*)desc {
NSString * locname = [NSString stringWithFormat:#"%#\n", title];
NSString * cityname = [NSString stringWithFormat:#"%#", desc];
NSString * text = [NSString stringWithFormat:#"%#%#", [locname capitalizedString], cityname];
NSDictionary *attribs = #{
NSForegroundColorAttributeName:[UIColor colorWithHexString:#"222222"],
NSFontAttributeName: [UIFont fontWithName:[[Variables getInstance] HelveticaNeue] size:14]
};
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text attributes:attribs];
NSRange range = [text rangeOfString:cityname];
[attributedText setAttributes:#{NSForegroundColorAttributeName:[UIColor colorWithHexString:#"222222"],
NSFontAttributeName:[UIFont fontWithName:[[Variables getInstance] HelveticaNeueLight] size:14]} range:range];
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120, 44)];
titleLabel.font = [UIFont fontWithName:[[Variables getInstance] HelveticaNeueMedium] size:15];
titleLabel.attributedText = attributedText;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.numberOfLines=0;
titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
titleLabel.textColor = [UIColor blackColor];
self.navigationItem.titleView = titleLabel;
}
I have a UIImageView installed on my cell, it is transparent black, but when I scroll the CollectionView and I raise my UIImageView but there is always more to transprence.
See picture :
1 - I is not even scroll
2 - After a scroll
My code :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"pictureCell";
MSContestListCollectionViewCell *cell = (MSContestListCollectionViewCell *)[self.collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
cell.titleContest.adjustsFontSizeToFitWidth = YES;
cell.titleContest.minimumScaleFactor = 0.5;
cell.pictureImageView.layer.cornerRadius = 5;
cell.pictureImageView.clipsToBounds = YES;
cell.titleView.layer.cornerRadius = 5;
cell.titleView.clipsToBounds = YES;
switch (_segmentedControl.selectedSegmentIndex) {
case 0: {
NSDictionary *searchResult = [self.readArray objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.statusContest.text = [searchResult objectForKey:#"status"];
if ([[searchResult objectForKey:#"status"] isEqualToString:#"PAUSE"]) {
cell.titleContest.text = [NSString stringWithFormat:#"Concours en pause"];
/*
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];
view.layer.cornerRadius = 5;
view.clipsToBounds = YES;
[cell.contentView addSubview:view];
*/
UIImageView *imagecellPause = [[UIImageView alloc] initWithFrame:CGRectMake(cell.contentView.frame.origin.x, cell.contentView.frame.origin.y, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
UIImage *cellImage = [UIImage imageNamed:#"cell-pause.png"];
imagecellPause.image = cellImage;
[cell.contentView addSubview:imagecellPause];
UIImageView *imagePause = [[UIImageView alloc] initWithFrame:CGRectMake(69.5, 69.5, 25, 25)];
UIImage *image = [UIImage imageNamed:#"Pause Filled-50 (1).png"];
imagePause.image = image;
[cell.contentView addSubview:imagePause];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, 150, 50)];
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"Avenir-Book" size:12];
label.text = [searchResult objectForKey:#"description"];
label.adjustsFontSizeToFitWidth = YES;
label.minimumScaleFactor = 0.5;
[cell.contentView addSubview:label];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 105, 150, 50)];
label2.textColor = [UIColor whiteColor];
label2.font = [UIFont fontWithName:#"Avenir-Black" size:15];
label2.text = #"Concours en pause";
[cell.contentView addSubview:label2];
cell.titleView.hidden = YES;
} else {
cell.titleContest.text = [searchResult objectForKey:#"description"];
}
break;
}
case 1: {
NSDictionary *searchResult2 = [self.readArrayWinner objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult2 objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.titleContest.text = [searchResult2 objectForKey:#"description"];
cell.statusContest.text = [searchResult2 objectForKey:#"status"];
NSLog(#"le gagnant : %#", [searchResult2 valueForKeyPath:#"winners.name"]);
break;
}
case 2: {
NSDictionary *searchResult3 = [self.readArrayPhotos objectAtIndex:indexPath.item];
NSString *stringImage = [searchResult3 objectForKey:#"featuredImage"];
NSString *image = [NSString stringWithFormat:#"https://srv.mediaswapp.com/%#", stringImage];
[cell.pictureImageView sd_setImageWithURL:[NSURL URLWithString:image]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.titleContest.text = [searchResult3 objectForKey:#"description"];
cell.statusContest.text = [searchResult3 objectForKey:#"status"];
break;
}
default:
break;
}
[_activity stopAnimating];
_label1.hidden = YES;
return cell;
}
Your CellForRowAtIndexPath should never contain addSubView, since everytime the cell is reloaded it will add the view. Thats why it keeps getting darker, it just adds view onto view.
Instead you should subclass your UICollectionViewCell and add the view in the subclass.
I have a UITextField in a UITableViewCell.
Even though I set -
textField.userInteractionEnabled = NO;
textField.enabled = NO
But when I click on the table cell which contains the textField, the keyboard comes up for the textfield.
Why is this happening and how can I prevent it?
EDIT: Strangely this is happening when I first set some text in the textfield. When the textfield is empty, it is not editable.
EDIT: Code for cellForRowAtIndexPath -
cell = [tableView dequeueReusableCellWithIdentifier:#"CellIdentifier"];
cell.accessoryType = UITableViewCellAccessoryNone;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, cell.bounds.size.width - 20, cell.bounds.size.height - 20)];
textField.font = [UIFont systemFontOfSize:15];
textField.textColor = [UIColor blackColor];
UIColor *placeholderColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self getPlaceHolderTextForIndexPath:indexPath] attributes:#{NSForegroundColorAttributeName : placeholderColor}];
textField.keyboardType = [self getKeyboardTyeForIndexPath:indexPath];
textField.returnKeyType = UIReturnKeyDone;
textField.backgroundColor = [UIColor clearColor];
textField.textAlignment = NSTextAlignmentLeft;
textField.autocapitalizationType = [self getAutocapitaliztionTypeForIndexPath:indexPath];
textField.tag = 1;
if (_editingNotAllowed) {
[textField setText:[self getTextForTextFieldWithIndexPath:indexPath]];
[textField setUserInteractionEnabled:NO];
textField.enabled = NO;
} else {
[textField setUserInteractionEnabled:YES];
}
[cell.contentView addSubview:textField];
You should create an UITableViewCell as shown in this repo :)
https://github.com/breeno/EditingUITableView
And use your custom UITableViewCell like this:
if(condition){ // Check the row must be TextField
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(!cell){
cell = [[CustomCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: nil];
}
cell.label.text = #"Title Row"; //UITextLabel Title
UIColor *placeholderColor = [UIColor colorWithRed:146/255.0 green:146/255.0 blue:146/255.0 alpha:1];
cell.textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:[self getPlaceHolderTextForIndexPath:indexPath] attributes:#{NSForegroundColorAttributeName : placeholderColor}];
cell.textField.returnKeyType = UIReturnKeyDone;
cell.textField.backgroundColor = [UIColor clearColor];
cell.textField.textAlignment = NSTextAlignmentLeft;
cell.textField.tag = 1;
} else { // Normal UITableViewCell
}
I have gone through this answer and others. I can't seem to center these columns.
This is my code:
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
NSLog(#"%s", __FUNCTION__);
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 120.0, 44.01)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.font = [UIFont fontWithName:#"HelveticaNeue-Thin" size:18];
label.text = [NSString stringWithFormat:#" %d", row+1];
return label;
}
Any ideas what I'm doing wrong?
Update with IB attributes inspector:
I am trying to implement an image in a UIImagePicker that has only one component. I add an uimageview and a uilabel. The catch is that the first row should NOT have an image as well as the 3rd row which I believe I have done right. My problem is that the images for each row are different so to solve this I have put them into an array of images and then based on what row is being passed in, I display the image. However; it is not working for me and seems to display the same image over and over. Can anyone tell me why? Thanks in advance!
imageNames= [[NSArray alloc] initWithObjects:#"9_stars.png", #"8_stars.png",#"7_stars.png",#"6_stars.png",#"5_stars.png",#"4_stars.png", nil];
/
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
NSString *imageName= [imageNames objectAtIndex:theRow];
NSLog (#"the image name is %#", imageName);
NSLog (#"the current count is %d", theRow);
if(component == 0)
{
if (row == 0 || row == 17)
{
UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(-80, 0, 320, 60)];
channelLabel.text = [currentLottoNames objectAtIndex:row];
channelLabel.textAlignment = UITextAlignmentLeft;
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.font = [UIFont boldSystemFontOfSize:20];
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.shadowColor = [UIColor whiteColor];
channelLabel.shadowOffset = CGSizeMake (0,1);
UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
[tmpView insertSubview:channelLabel atIndex:0];
return tmpView;
}
else {
UIImage *img = [UIImage imageNamed:imageName];
UIImageView *temp = [[UIImageView alloc] initWithImage:img];
temp.frame = CGRectMake(120, 15,70, 27);
UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(-80, 0, 320, 60)];
channelLabel.text = [currentLottoNames objectAtIndex:row];
channelLabel.textAlignment = UITextAlignmentLeft;
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.font = [UIFont boldSystemFontOfSize:20];
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.shadowColor = [UIColor whiteColor];
channelLabel.shadowOffset = CGSizeMake (0,1);
UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
[tmpView insertSubview:channelLabel atIndex:0];
[tmpView insertSubview:temp atIndex: 1];
return tmpView;
}
}
}
EDITED CODE
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
NSString *imageName= [imageNames objectAtIndex:row]; // This is where the issue is I believe. What do I use to go through the whole image array?
NSLog (#"the image name is %#", imageName);
NSLog (#"the current count is %d", row);
if(component == 0)
{
UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(-80, 0, 320, 60)];
channelLabel.text = [currentLottoNames objectAtIndex:row];
channelLabel.textAlignment = UITextAlignmentLeft;
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.font = [UIFont boldSystemFontOfSize:20];
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.shadowColor = [UIColor whiteColor];
channelLabel.shadowOffset = CGSizeMake (0,1);
UIImage *img = [UIImage imageNamed:imageName];
UIImageView *temp = [[UIImageView alloc] initWithImage:img];
temp.frame = CGRectMake(120, 15,70, 27);
UILabel *channelLabel = [[UILabel alloc] initWithFrame:CGRectMake(-80, 0, 320, 60)];
channelLabel.text = [currentLottoNames objectAtIndex:row];
channelLabel.textAlignment = UITextAlignmentLeft;
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.font = [UIFont boldSystemFontOfSize:20];
channelLabel.backgroundColor = [UIColor clearColor];
channelLabel.shadowColor = [UIColor whiteColor];
channelLabel.shadowOffset = CGSizeMake (0,1);
UIView *tmpView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 110, 60)];
[tmpView insertSubview:channelLabel atIndex:0];
[tmpView insertSubview:temp atIndex: 1];
return tmpView;
}
}
Your problem is "theRow" - its not even defined in this method. You should just put a placeholder string in the array - that is at position 0 and 17 just put #"". Now you can use "row" to index into your array and all will be well.
EDIT: So your code implies you have 18 rows, want to show an image in most rows, but do something different for rows 0 and 17. Thus, you need an array of size 18.
imageNames= [[NSArray alloc] initWithObjects:
#"", // row 0 unused
// 1 - 6
#"9_stars.png", #"8_stars.png",#"7_stars.png",#"6_stars.png",#"5_stars.png",#"4_stars.png", nil];
// 7 - 12
#"9_stars.png", #"8_stars.png",#"7_stars.png",#"6_stars.png",#"5_stars.png",#"4_stars.png", nil];
// 13 - 16
#"9_stars.png", #"8_stars.png",#"7_stars.png",#"6_stars.png",
// 17
#"" // not used
// keep adding
nil];
The concept is to insure that you always have the same number of objects in the array as you have rows.
You can add an assert at the start of this method:
assert(row < [imageNames count]);
which will cause an exception if what you believe to be true turns out not to be.