I have a storyboard with tableview. I have 4 cells in table. They have their identifiers. Problem is that I can't change font of cells programmatically. I've already add font to the project and added a new key in the array named "Fonts Provided by application" in the plist file. Maybe I need to reloadData but where I can do it? Under viewWillAppear it's useless
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [self.menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell.textLabel setFont:[UIFont fontWithName:#"Xiomara" size:30]];
return cell;
}
Make sure that you're using right font name.
You can use this method.
- (void)logFontNamesOfAllFonts
{
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily=0; indFamily<[familyNames count]; ++indFamily) {
NSLog(#"Family name: %#", [familyNames objectAtIndex:indFamily]);
fontNames = [[NSArray alloc] initWithArray:
[UIFont fontNamesForFamilyName:
[familyNames objectAtIndex:indFamily]]];
for (indFont=0; indFont<[fontNames count]; ++indFont) {
NSLog(#" Font name: %#", [fontNames objectAtIndex:indFont]);
}
}
}
Just call in your AppDelegate.m's applicationDidFinishLaunching: method, and see in concole the right name of your font.
If there isn't font with name that you installed, then your font not installed correctly.
Related
I have two array. One for Tableview Header and another one for Tableview row. I want to display header wise values for different Rows. Number of sections are depend on that array which is used in Header view.
Number of rows is calculated tableview row array divide by header array.
The UI working perfectly and also values of header and sections are fine but row values appear same for each and every section.
yo have to use this Approach to show different Cell Result
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _mutableArray.count;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(5, 0, tableView.frame.size.width, heightOfSection)];
NSDictionary *dict = [_mutableArray objectAtIndex:section];
NSString *useIt= [dict objectForKey:#"CategoryName"]
return view;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSDictionary *dict = [_mutableArray objectAtIndex:section];
NSArray *array= [dict objectForKey:#"FontNames"];
return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableViewLocal cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// static NSString *cellIdentifier = #"Cell";
UITableViewCell *Cell = [tableViewLocal dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
for (__strong UIView *view in Cell.contentView.subviews) {
[view removeFromSuperview];
view = nil;
}
// [tableViewLocal setAllowsSelection:NO];
/* UILabel *labelDetailName =[[UILabel alloc]init];
labelDetailName.frame =CGRectMake(5, 15, 300, 40);
NSDictionary *dict = [_mutableArray objectAtIndex:(tableViewLocal.tag-100)];
NSArray *arrayLocal = [dict objectForKey:#"FontNames"];
NSString *titleName =[ [arrayLocal objectAtIndex:indexPath.item]objectForKey:#"title"];
labelDetailName.text = titleName;
NSString *fontName = [[arrayLocal objectAtIndex:indexPath.item] objectForKey:#"nameForSystem"];
labelDetailName.font = [UIFont fontWithName:fontName size: kSystemVersion < 7.0 ? 20 : 16];
[Cell.contentView addSubview:labelDetailName]; */
NSDictionary *dict = [_mutableArray objectAtIndex:(tableViewLocal.tag-100)];
NSArray *Array = [dict objectForKey:#"FontNames"];
NSString *localString = [Array objectAtIndex:indexPath.row]; // use it
}
I try to display a checkmark symbol like this:
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentCenter;
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
[#"✓" drawInRect: CGRectInset(ovalRect, 0/116.0*width, 11/116.0*width) withAttributes:[NSDictionary dictionaryWithObjects:#[[UIFont fontWithName: #"HelveticaNeue" size: 80.0/116.0*width], paragraphStyle, [UIColor orangeColor]] forKeys:#[NSFontAttributeName, NSParagraphStyleAttributeName, NSForegroundColorAttributeName]]];
On http://iosfonts.com the symbol looks just like I want it for Helvetica Neue. However, on the device/ simulator it looks completely different. I found about 3 fonts where it looked different but for the other 20 fonts I tested it always looked the same, always different from http://iosfonts.com. How can I find out how the checkmark symbol would look in different iOS fonts? I don't want to try them all out by hand.
If someone else has font problems, here is the code to display text in all available fonts in an UITableView.
- (void)viewDidLoad
{
[super viewDidLoad];
//Fill NSMutableArray fonts with all available fonts
fonts = [NSMutableArray array];
NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];
NSArray *fontNames;
NSInteger indFamily, indFont;
for (indFamily = 0; indFamily < [familyNames count]; ++indFamily)
{
fontNames = [[NSArray alloc] initWithArray:[UIFont fontNamesForFamilyName:[familyNames objectAtIndex:indFamily]]];
for (indFont = 0; indFont < [fontNames count]; ++indFont)
{
[fonts addObject:[fontNames objectAtIndex:indFont]];
}
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *font = [fonts objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:#"✓ %#", font]];
[cell.textLabel setFont:[UIFont fontWithName:font size:16]];
return cell;
}
I load data from plist to uitableview like this:
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [[documentPaths lastObject] stringByAppendingPathComponent:#"data.plist"];
NSMutableDictionary *resultDic = [[NSMutableDictionary alloc] init];
NSMutableArray *resultArray = [[NSMutableArray alloc] init];
NSDictionary *myDict = [NSDictionary dictionaryWithContentsOfFile:path];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"purpleKey"])
{
NSArray *purple = [myDict objectForKey:#"Purple"];
[resultArray addObject:#"Purple"];
[resultDic setValue:purple forKey:#"Purple"];
}
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"orangeKey"])
{
NSArray *orange = [myDict objectForKey:#"Orange"];
[resultArray addObject:#"Orange"];
[resultDic setValue:orange forKey:#"Orange"];
}
self.tableData = resultDic;
self.sectionsTitle = resultArray;
}
titleForHeaderInSection
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sectionsTitle objectAtIndex:section];
}
my plist structure
My question is:
How can I manually set title for Purple header and Orange header without to changing names in plist file?
like this: Purple = Category 1, Orange = Category 2
EDIT
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return sectionsTitle.count;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [sectionsTitle objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
int num = [[tableData objectForKey:[sectionsTitle objectAtIndex:section]] count];
if (num > 3) {
num = 3;
}
return num;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *dict = [[tableData objectForKey:[sectionsTitle objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.numberOfLines = 1;
cell.textLabel.font = [UIFont systemFontOfSize:11];
cell.textLabel.text = [NSString stringWithFormat:#"%# - %#", [dict objectForKey:#"Name"], [dict objectForKey:#"Address"]];
if ([dict objectForKey:#"Address"] == (NULL)) {
//do nothing
}
return cell;
}
From the look of your code, the section headers are coming from resultsArray, which you are populating with the constant strings Orange and Purple.
You can just put different constant strings into that array, unless I'm missing something.
So, instead of
[resultArray addObject:#"Orange"];
Use
[resultArray addObject:#"Category 1"];
I am trying to learn how to code a UITableView and is having some problems with the programming of the section.
I have declared 3 arrays with strings and 1 array with the 3 arrays.
firstSection = [NSArray arrayWithObjects:#"Red", #"Blue", nil];
secondSection = [NSArray arrayWithObjects:#"Orange", #"Green", #"Purple", nil];
thirdSection = [NSArray arrayWithObject:#"Yellow"];
array = [[NSMutableArray alloc] initWithObjects:firstSection, secondSection, thirdSection, nil];
To shows the headers
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
NSString * title;
title = [NSString stringWithFormat:#"%#" , [array objectAtIndex:section]];
return title;
}
which shows the array itself as the headers
therefore is it possible to actually show the name of the sections using the names of the arrays such as firstSection and secondSection?
In your case, it's better to store your arrays in an NSDictionary. For example, if you declare and synthesize an NSDictionary variable called tableContents and an NSArray called titleOfSections, you can do something like this:
- (void)viewDidLoad {
[super viewDidLoad];
//These will automatically be released. You won't be needing them anymore (You'll be accessing your data through the NSDictionary variable)
NSArray *firstSection = [NSArray arrayWithObjects:#"Red", #"Blue", nil];
NSArray *secondSection = [NSArray arrayWithObjects:#"Orange", #"Green", #"Purple", nil];
NSArray *thirdSection = [NSArray arrayWithObject:#"Yellow"];
//These are the names that will appear in the section header
self.titleOfSections = [NSArray arrayWithObjects:#"Name of your first section",#"Name of your second section",#"Name of your third section", nil];
NSDictionary *temporaryDictionary = [[NSDictionary alloc]initWithObjectsAndKeys:firstSection,#"0",secondSection,#"1",thirdSection,#"2",nil];
self.tableContents = temporaryDictionary;
[temporaryDictionary release];
}
Then in the table view controller methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.titleOfSections count];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
return [[self.tableContents objectForKey:[NSString stringWithFormat:#"%d",section]] count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
//Setting the name of your section
return [self.titleOfSections objectAtIndex:section];
}
Then to access the contents of each array in your cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *arrayForCurrentSection = [self.tableContents objectForKey:[NSString stringWithFormat:#"%d",indexPath.section]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
cell.textLabel.text = [arrayForCurrentSection objectAtIndex:indexPath.row];
return cell;
}
can u do like this
NSArray *SectionArray = [[NSArray alloc]initwithArray:[array objectAtIndex:section]];
NSString * title;
title = [NSString stringWithFormat:#"%#" , [SectionArray objectAtIndex:section]];
return title;
I just started learning Xcode, read the book and now trying to implement what I read. The app needs to store certain data in arrays and dictionary, then table view extracts the data by automatically counting number of sections, number of rows and populating table cells with content, assigned to a key word.
Please, see my code, because I've been trying to run the app, but it gives me SIGABRT error. I don't know if new Xcode doesn't declare or call things this way(my book is for iOS3). I tried to find solutions from blogs, youtube, but the app is still not running at this point.
I know that I declared everything correctly in AppDelegate files, etc, because I used someone's sample code of one 1 array. And the table view got populated.
Help me, because something is wrong with my code with NSMutableDictionary and indexPath. Thank you!
#import "TableViewController.h"
#implementation TableViewController
#synthesize tableData;
#synthesize tableSections;
#pragma mark - View lifecycle
- (void)viewDidLoad
{ [self createData];
[super viewDidLoad];
}
-(void)createData{
NSMutableArray *category1;
NSMutableArray *category2;
category1=[[NSMutableArray alloc] init];
category2=[[NSMutableArray alloc] init];
tableSections=[[NSMutableArray alloc] initWithObjects:#"Category 1", #"Category 2", nil];
tableData=[[NSMutableArray alloc] initWithObjects:category1, category2, nil];
[category1 addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"TitleA", #"name",#"A",#"property", nil]];
[category1 addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"TitleB", #"name",#"B",#"property", nil]];
[category2 addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"TitleC", #"name",#"C",#"property", nil]];
[category2 addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"TitleD", #"name",#"D",#"property", nil]];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [tableSections count];
//return [self.tableSections count]; Also tried to use with self. Don’t know which one is correct
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[self.tableSections objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[[tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] objectForKey:#"name"];
/* Also tried this:
tableData *cellObj = [[self.tableData objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
cell.textLabel.text = cellObj.name;
/* Also tried this:
tableData *cellObj= [[[self.tableData objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]objectForKey:#"name"];
cell.textLabel.text=smsObj.name; */
/* Also tried this:
NSDictionary *item = (NSDictionary *)[self.tableData objectAtIndex:indexPath.row];
cell.textLabel.text = [item objectForKey:#"name"];*/
/* Also tried this way:
NSDictionary *dictionary = [tableData objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"name"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.text = cellValue;
And the same way, but switched order of NSArray and NSDictionary*/
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// This I will add later, after I make my Table View Controller work
}
#end
I know I'm doing something very silly with the codes, but I'm trying to understand the right way to write the codes...
[category1 addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"TitleA", #"name",#"A",#"property", nil]];
[category1 addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"TitleB", #"name",#"B",#"property", nil]];
[category2 addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"TitleC", #"name",#"C",#"property", nil]];
[category2 addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"TitleD", #"name",#"D",#"property", nil]];
}
add this code after initilization of those arrays and try