iOS change font size programmatically not work - ios

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.backgroundColor = [UIColor clearColor];
if ([[UIDevice currentDevice].systemVersion floatValue] < 7.0f) {
self.textLabel.backgroundColor = [UIColor whiteColor];
}
_topUserNewName= [[UILabel alloc] init];
_topUserNewName.textAlignment = NSTextAlignmentLeft;
_topUserNewName.backgroundColor = [UIColor clearColor];
_topUserNewName.font = [UIFont fontWithName:#"HelvaticaNeue-Regular" size:5.0];
_topUserNewName.textColor = [UIColor colorWithRed:(47/255.0) green:(55/255.0) blue:(65/255.0) alpha:1];
_topUserNewName.frame = CGRectMake(66, 8, 215, 21);
_topUserName= [[UILabel alloc] init];
_topUserName.textAlignment = NSTextAlignmentLeft;
_topUserName.backgroundColor = [UIColor clearColor];
_topUserName.font = [UIFont fontWithName:#"HelvaticaNeue-Regular" size:10.0];
_topUserName.textColor = [UIColor colorWithRed:(145/255.0) green:(212/255.0) blue:(210/255.0) alpha:1];
_topUserName.frame = CGRectMake(66, 37, 210, 21);
[self.contentView addSubview:_topUserNewName];
[self.contentView addSubview:_topUserName];
self.selectionStyle = UITableViewCellSelectionStyleNone;
}
return self;
}
This codes in my table cell class. I want to change font size with programmatically right here when i change size nothing change ?
i researched on google this rule is right but not work any idea ?

The name HelveticaNeue-Regulardoes not actually exist. This should be [UIFont fontWithName:#"HelvaticaNeue" size:10.0];.
You can see the list of all font names here : http://iosfonts.com/

Please make sure you are passing the right font family and font face..try this
[UIFont fontWithName:#"HelveticaNeue-Bold" size:10.0];
HelvticaNeue-Regular face is not suported by ios, you can check all supported fonts by enumerating as:
for (NSString *familyName in [UIFont familyNames]){ NSLog(#"Font Family = %#",familyName);
for (NSString *fontName in
[UIFont fontNamesForFamilyName:familyName]){ NSLog(#"\t%#", fontName);
}
}
}

/===FOR IOS8===/
static NSString *_myCustomFontName;
+ (NSString *)myCustomFontName
{
if ( !_myCustomFontName )
{
NSArray *arr = [UIFont fontNamesForFamilyName:#"Custom Font Family"];
// I know I only have one font in this family
if ( [arr count] > 0 )
_myCustomFontName = arr[0];
}
return _myCustomFontName;
}

Related

How to align custom UINavigationBar title (UILabel )in middle (iOS)

I am using following code to align 2 strings in UINavigationBar centre .
for ex:
<Back John Kelly Maria
23/F
I need these kind of middle aligned text in UINavigationbar but what jam now getting is all the text aligned in right side of UINavigationBar .
something like this
<Back John Kelly Maria
23/F
Please help..me .
this is my code
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,50)];
UIFont * customFont = [UIFont fontWithName:#"Helvetica Neue" size:19]; //custom font
NSString * text =title;
UILabel *patientNameLabel;
if([title length]>15){
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}else{
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}
patientNameLabel.text = #"John Kelly Maria";
patientNameLabel.font = customFont;
patientNameLabel.numberOfLines = 0;
patientNameLabel.textAlignment=NSTextAlignmentCenter;
patientNameLabel.minimumScaleFactor = 10.0f/12.0f;
patientNameLabel.clipsToBounds = YES;
patientNameLabel.backgroundColor = [UIColor clearColor];
patientNameLabel.textColor = [UIColor blackColor];
patientNameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
NSString *subTitle =subTitleText;
UIFont * customFont1 = [UIFont fontWithName:#"Helvetica Neue" size:14]; //custom font
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,320,20)];
subTitleLabel.textAlignment = NSTextAlignmentCenter;
subTitleLabel.text = #"23/F";
subTitleLabel.font = customFont1;
subTitleLabel.textAlignment=NSTextAlignmentCenter;
subTitleLabel.numberOfLines = 1;
subTitleLabel.adjustsFontSizeToFitWidth = YES;
subTitleLabel.minimumScaleFactor = 10.0f/12.0f;
subTitleLabel.clipsToBounds = YES;
subTitleLabel.backgroundColor = [UIColor clearColor];
subTitleLabel.textColor = [UIColor blackColor];
[titleView addSubview:patientNameLabel];
[titleView addSubview:subTitleLabel];
Add below two lines before addSubview lines..
patientNameLabel.center = CGPointMake(titleView.frame.size.width/2, 0);
subTitleLabel.center = CGPointMake(titleView.frame.size.width/2, 25);
Add Below Code & see
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,50)];
UIFont * customFont = [UIFont fontWithName:#"Helvetica Neue" size:19]; //custom font
NSString * text =#"Nilesh Patel";
UILabel *patientNameLabel;
if([text length]>15){
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}else{
patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,320,30)];
}
patientNameLabel.text = #"Nilesh Patel";
patientNameLabel.font = customFont;
patientNameLabel.numberOfLines = 0;
patientNameLabel.textAlignment=NSTextAlignmentCenter;
patientNameLabel.minimumScaleFactor = 10.0f/12.0f;
patientNameLabel.clipsToBounds = YES;
patientNameLabel.backgroundColor = [UIColor clearColor];
patientNameLabel.textColor = [UIColor blackColor];
patientNameLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
NSString *subTitle =#"iOS Developer";
UIFont * customFont1 = [UIFont fontWithName:#"Helvetica Neue" size:14]; //custom font
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,320,20)];
subTitleLabel.textAlignment = NSTextAlignmentCenter;
subTitleLabel.text = #"28/M";
subTitleLabel.font = customFont1;
subTitleLabel.textAlignment=NSTextAlignmentCenter;
subTitleLabel.numberOfLines = 1;
subTitleLabel.adjustsFontSizeToFitWidth = YES;
subTitleLabel.minimumScaleFactor = 10.0f/12.0f;
subTitleLabel.clipsToBounds = YES;
subTitleLabel.backgroundColor = [UIColor clearColor];
subTitleLabel.textColor = [UIColor blackColor];
patientNameLabel.center = CGPointMake(titleView.frame.size.width/2, 10);
subTitleLabel.center = CGPointMake(titleView.frame.size.width/2, 30);
[titleView addSubview:patientNameLabel];
[titleView addSubview:subTitleLabel];
[self.navigationController.navigationBar addSubview:titleView];
Note : You have set static frame to titleView so if you see your app on different device you will not be able to see the text in exact centre. To avoid this set the titleView frame dynamically based on device/view width.
Try Frame setting like this
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0,20,screenWidth,50)];
UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,25,screenWidth,20)];
UILabel *patientNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,screenWidth,30)];

Changing navigationController title after viewdidload

When I set self.navigationItem.title in viewdidload the title appears correctly, Later based on user interaction I am updating the title. The length of title is fixated to length of the title that is set in viewdidload.
This is want happening in my code
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationController.navigationBar setTitleTextAttributes:#{
[UIColor brownColor]: NSForegroundColorAttributeName,
[UIFont fontWithName:#"HelveticaNeue-Medium" size:12.0]: NSFontAttributeName}];
self.navigationItem.title = #"My Account";
}
- (void)updateTitleWithUserName {
self.navigationItem.title = #"Test User 167";
}
Result:
Test...
Any suggestion how to fix this?
Try using a label for title so you can adjust its size to fit like this on viewDidLoad, then just change tlabel on update and assign it to the nav title. (Manage color and fonts as you wish)
self.title = #"Your TiTle Text";
UILabel* tlabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];
tlabel.text=self.navigationItem.title;
tlabel.textColor=[UIColor whiteColor];
tlabel.font = [UIFont fontWithName:#"Helvetica-Bold" size: 30.0];
tlabel.backgroundColor =[UIColor clearColor];
tlabel.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView=tlabel;
try this
- (void)viewDidLoad
{
titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0, 200, 40)];// create instance variable UILabel * titleLabel;
titleLabel.text=#"My Account";
titleLabel.textColor=[UIColor blackColor];
titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size: 16.0];
titleLabel.backgroundColor =[UIColor clearColor];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.adjustsFontSizeToFitWidth=YES;
self.navigationItem.titleView= titleLabel;
}
- (void)updateTitleWithUserName
{
titleLabel.text = #"Test User 167";
}

why setText for textView add text bottom of textView?

I'm noob in objective c and I create one page xib file that this page have one textView empty.
I drag UITextView ton this page and connect to self outlet.
Now I want to add any text like this "I AM JANATAN" in it. when I using this code and run my simulator , my simulator show me my textView but I see my sentence is last line of textView !!!
why??? this is my image : pic of simulator
this is my code :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
getTextPlaces = [self CreateTextViews];
textField1 = [getTextPlaces objectAtIndex:0];
[self.view addSubview:textField1];
NSString *str = [_stack pop];
textArray = [self readInformationFromDatabaseWithData:str];
NSString *s1 = [[textArray objectAtIndex:0]objectForKey:#"Names"];
textField1.text = [NSString stringWithFormat:#"%#",s1]; //this s1 value is I am janatan
}
- (NSMutableArray *)CreateTextViews{
UITextView *tf = [[UITextView alloc] initWithFrame:CGRectMake(35, 70, 250, 50)];
tf.backgroundColor = [UIColor purpleColor];
tf.font = [UIFont fontWithName:#"Helvetica-Bold" size:20];
tf.autocorrectionType = UITextAutocorrectionTypeNo;
tf.textAlignment = NSTextAlignmentRight;
//tf.text=#"Hello World";
//second one
UITextView *tf1 = [[UITextView alloc] initWithFrame:CGRectMake(35, tf.frame.origin.y + 70, 250, 50)];
tf1.font = [UIFont fontWithName:#"Helvetica-Bold" size:20];
tf1.backgroundColor=[UIColor whiteColor];
tf1.autocorrectionType = UITextAutocorrectionTypeNo;
tf1.textAlignment = NSTextAlignmentRight;
//and so on adjust your view size according to your needs
NSMutableArray *twoText = [[NSMutableArray alloc]init];
[twoText addObject:tf];
[twoText addObject:tf1];
return twoText;
}
my friend for remove this you need is to set the following:
self.automaticallyAdjustsScrollViewInsets = NO;
in the viewDidLoad method.

Change UIPickerView pickerView:viewForRow: attributes based on a certain condition

I am trying to change the text color in viewForRow based on a certain condition. When I press a button the view changes to a different color but I would also like to change the colour of the text in the picker. I use 'viewForRow' because I have a custom view.
//adds subcategories to the wheel
-(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];
if (!self.isBackgroundWhite)//Boolean that changes when the background changes
{
NSLog(#"Set white text");
label.textColor = [UIColor whiteColor];
}else{
label.textColor = [UIColor blackColor];
}
if (row == 1) {
label.text = [NSString stringWithFormat:#" %#",[self.servicesArray objectAtIndex:row]];
label.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18];
}else{
label.text = [NSString stringWithFormat:#" -%#",[self.servicesArray objectAtIndex:row] ];
label.font = [UIFont fontWithName:kAppFont size:16];
}
return label;
}
EDIT: Thanks to #Rich I was able to spot my problem, I just need to call [self.pickerView reloadAllComponents];
If you only want to change the text color just use the other delegate method pickerView:attributedTitleForRow:forComponent:
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSString *title;
UIFont *font;
if (row == 1) {
title = [NSString stringWithFormat:#" %#",[self.servicesArray objectAtIndex:row]];
font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:18];
} else{
title = [NSString stringWithFormat:#" -%#",[self.servicesArray objectAtIndex:row] ];
font = [UIFont fontWithName:kAppFont size:16];
}
UIColor *textColor;
if (self.isBackgroundWhite) {
textColor = [UIColor blackColor];
} else {
NSLog(#"Set white text");
textColor = [UIColor whiteColor];
}
NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
paragraphStyle.alignment = NSTextAlignmentLeft;
NSDictionary *attributes = #{NSForegroundColorAttributeName : textColor,
NSFontAttributeName : font,
NSParagraphStyleAttributeName : paragraphStyle};
return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}
Also a note that you should call [self.pickerView reloadAllComponents]; when changing isBackgroundWhite. I would do this by overriding the setter for backgroundWhite and when the boolean value changes reload the picker view.
EDIT:
There appears to be a 'bug' (intentional or not) in iOS 7 (works fine on iOS 6) with setting the UIFont on an NSAttributedString returned from the pickerView:attributedTitleForRow:forComponent: method. So while the above works for the text color the font does not change. Luckily you can get around this with the code in the question. See this answer for more info.

Slow loading tableview on iPhone with reusable cells

I load the data from Parse.com backend, they send me a solutions for use the reusable cells but now I still have troubles with the loading speed, this is the coding I have in my tableview and I have a Subclass for making up my cells (ExploreStreamCustomCell.m)
- (ExploreStreamCustomCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
object:(PFObject *)object
{
static NSString *CellIdentifier = #"ExploreStreamCustomCell";
ExploreStreamCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[ExploreStreamCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
// Configure the cell
cell.listItemTitle.text = [object objectForKey:#"text"];
cell.checkinsLabel.text = [NSString stringWithFormat:#"%#", [object objectForKey:#"checkins"]];
cell.descriptionLabel.text = [object objectForKey:#"description"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
PFFile *listThumbnail = [object objectForKey:#"header"];
cell.listViewImage.image = [UIImage imageNamed:#"loading_image_stream.png"]; // placeholder image
cell.listViewImage.file = listThumbnail;
[cell.listViewImage loadInBackground:NULL];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForNextPageAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [super tableView:tableView cellForNextPageAtIndexPath:indexPath];
cell.textLabel.font = [cell.textLabel.font fontWithSize:kPAWWallPostTableViewFontSize];
return cell;
}
If I have all the content of the //configure cell in the cell == nil the it's fast but it show up 3 of the 9 unique datarows and repeat those 3 unique content cell 3 times?
Edit extra code within ExploreStreamCustomCell.m
#import "ExploreStreamCustomCell.h"
#implementation ExploreStreamCustomCell
#synthesize listViewImage,
iconLocation,
iconPeople,
iconCheckins,
listItemTitle,
locationLabel,
peopleLabel,
checkinsLabel,
descriptionLabel,
listItemView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
//Initialization code
listItemView = [[UIView alloc] init];
listViewImage = [[PFImageView alloc] init];
iconLocation = [[UIImageView alloc] init];
iconPeople = [[UIImageView alloc] init];
iconCheckins = [[UIImageView alloc] init];
listItemTitle = [[UILabel alloc] init];
locationLabel = [[UILabel alloc] init];
peopleLabel = [[UILabel alloc] init];
checkinsLabel = [[UILabel alloc] init];
descriptionLabel = [[UILabel alloc] init];
listViewImage.image = [UIImage imageNamed:#"nachtwacht_list_formaat.png"];
iconLocation.image = [UIImage imageNamed:#"icon_magenta_location.png"];
iconPeople.image = [UIImage imageNamed:#"icon_magenta_people.png"];
iconCheckins.image = [UIImage imageNamed:#"icon_magenta_checkins.png"];
listItemTitle.text = #"text";
locationLabel.text = #"0,7 km";
peopleLabel.text = #"34";
checkinsLabel.text = #"61";
descriptionLabel.text = #"Description text.";
[self.contentView addSubview:listItemView];
[self.contentView addSubview:listViewImage];
[self.contentView addSubview:iconLocation];
[self.contentView addSubview:iconPeople];
[self.contentView addSubview:iconCheckins];
[self.contentView addSubview:listItemTitle];
[self.contentView addSubview:locationLabel];
[self.contentView addSubview:peopleLabel];
[self.contentView addSubview:checkinsLabel];
[self.contentView addSubview:descriptionLabel];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGFloat boundsX = contentRect.origin.x;
CGRect frame;
frame= CGRectMake(boundsX+0 , 33, 280, 124);
listViewImage.frame = frame;
listViewImage.contentMode = UIViewContentModeScaleAspectFill;
listViewImage.layer.masksToBounds = YES;
//listViewImage.backgroundColor = [UIColor lightGrayColor];
frame= CGRectMake(boundsX+20 , 164, 12, 18);
iconLocation.frame = frame;
//iconLocation.backgroundColor = [UIColor lightGrayColor];
frame= CGRectMake(boundsX+102 , 164, 24, 18);
iconPeople.frame = frame;
//iconPeople.backgroundColor = [UIColor lightGrayColor];
frame= CGRectMake(boundsX+193 , 164, 20, 16);
iconCheckins.frame = frame;
//iconLocation.backgroundColor = [UIColor lightGrayColor];
frame= CGRectMake(boundsX+0 , 0, 280, 33);
listItemView.frame = frame;
listItemView.backgroundColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000];
frame= CGRectMake(boundsX+20 , 3, 240, 29);
listItemTitle.frame = frame;
//listItemTitle.textColor = [UIColor colorWithRed:250.0f/255.0f green:194.0f/255.0f blue:9.0f/255.0f alpha:0.8f];
listItemTitle.textAlignment = UITextAlignmentLeft;
listItemTitle.font = [UIFont boldSystemFontOfSize:15];
listItemTitle.textColor = [UIColor whiteColor];
listItemTitle.backgroundColor = [UIColor clearColor];
listItemTitle.lineBreakMode = UILineBreakModeTailTruncation;
//listItemTitle.backgroundColor = [UIColor orangeColor];
frame= CGRectMake(boundsX+40 , 164, 57, 21);
locationLabel.frame = frame;
locationLabel.textAlignment = UITextAlignmentLeft;
locationLabel.font = [UIFont boldSystemFontOfSize:12];
locationLabel.textColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000];
locationLabel.backgroundColor = [UIColor clearColor];
locationLabel.lineBreakMode = UILineBreakModeTailTruncation;
locationLabel.numberOfLines = 1;
//locationLabel.backgroundColor = [UIColor redColor];
frame= CGRectMake(boundsX+134 , 164, 57, 21);
peopleLabel.frame = frame;
peopleLabel.textAlignment = UITextAlignmentLeft;
peopleLabel.font = [UIFont boldSystemFontOfSize:12];
peopleLabel.textColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000];
peopleLabel.backgroundColor = [UIColor clearColor];
peopleLabel.lineBreakMode = UILineBreakModeTailTruncation;
peopleLabel.numberOfLines = 1;
frame= CGRectMake(boundsX+221 , 164, 51, 21);
checkinsLabel.frame = frame;
checkinsLabel.textAlignment = UITextAlignmentLeft;
checkinsLabel.font = [UIFont boldSystemFontOfSize:12];
checkinsLabel.textColor = [UIColor colorWithRed:0.749 green:0.000 blue:0.243 alpha:1.000];
checkinsLabel.backgroundColor = [UIColor clearColor];
checkinsLabel.lineBreakMode = UILineBreakModeTailTruncation;
checkinsLabel.numberOfLines = 1;
frame= CGRectMake(boundsX+0 , 189, 280, 55);
descriptionLabel.frame = frame;
descriptionLabel.textAlignment = UITextAlignmentLeft;
descriptionLabel.font = [UIFont systemFontOfSize:13];
descriptionLabel.backgroundColor = [UIColor clearColor];
descriptionLabel.lineBreakMode = UILineBreakModeTailTruncation;
descriptionLabel.numberOfLines = 3;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
/*
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
A great way to explore the inefficiencies of your code is to use Instruments' Time Profiler tool. The Time Profiler will let you see how much time is being spent on each task, line-by-line in your code.
I would recommend the following settings for profiling:
From Apple's Face Detection sample app:
You can then double click any line (higher percentages mean more time is being devoted to that method call) to see in the code how much time is spent in each place.
From here you can begin to figure out where you are being inefficient and see exactly what is taking up so much time. Good luck!

Resources