UINavigationBar navigationItem title height - objective-c

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

Related

UIPickerView not change font iOS 9.1

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

UIButton with multi-line titleLabel with NSAttributedString doesn't work in iOS 7 but works in iOS8

In my project i have a requirement to show a UIButton that has two lines of text in its titleLabel With NSAttributedString. Its working fine in iOS8 but Where as in iOS7 its not working,i can see two lined text after selected button.
This is the code I'm using:
calendarBtn= [UIButton buttonWithType:UIButtonTypeCustom];
calendarBtn.frame=CGRectMake(50 ,10, 45, 45);
[calendarBtn addTarget:self action:#selector(calendarBtnclicked:)forControlEvents:UIControlEventTouchUpInside];
calendarBtn.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
calendarBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
calendarBtn.titleLabel.numberOfLines=2;
[calendarBtn setTitleColor:BLACK_COLOR forState:UIControlStateNormal];
NSString *dateString=#"27\nApr";
dateString=[dateString uppercaseString];
[calendarBtn setTitle:dateString forState:UIControlStateNormal];
// Font For 27
UIFont *ddFont = [UIFont fontWithName:ArkitechLight size:17.0f];
NSMutableDictionary *ddDict = [NSMutableDictionary dictionaryWithObject:ddFont forKey:NSFontAttributeName];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setLineSpacing:3];
style.alignment=NSTextAlignmentCenter;
[style setLineBreakMode:NSLineBreakByWordWrapping];
[ddDict addEntriesFromDictionary:#{NSParagraphStyleAttributeName : style,}];
NSMutableAttributedString *ddStr = [[NSMutableAttributedString alloc] initWithString:[dateString substringToIndex:2] attributes:ddDict];
// Font For Apr
UIFont *mmmFont = [UIFont fontWithName:ArkitechLight size:11.5f];
NSDictionary *mmmDict = [NSDictionary dictionaryWithObject:mmmFont forKey:NSFontAttributeName];
NSMutableAttributedString *mmmString = [[NSMutableAttributedString alloc]initWithString:[dateString substringFromIndex:2] attributes:mmmDict];
[ddStr appendAttributedString:mmmString];
calendarBtn.titleLabel.attributedText=ddStr;
You need to use this line to set attributed title
[calendarBtn setAttributedTitle: ddStr forState:UIControlStateNormal];
instead of
calendarBtn.titleLabel.attributedText=ddStr;
Hope it helps :)

UIlabel height issue in Custom UItableViewCell in iOS 6 and iOS 7

i am displaying different items in custom table cell. when i try to display multiline uilabel text. it si only showing single line. even though i calculate the label height
also i try to set new frame with new height after calculation.
my code is as follows:
+(CGRect )getlabelHeight:(CGRect)frame withFontName:(NSString *)font andFontSize:(float)fontSize andText:(NSString *)text
{
CGSize constrainedSize = CGSizeMake(frame.size.width, 9999);
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:font size:fontSize], NSFontAttributeName,
nil];
NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:text attributes:attributesDictionary];
CGRect requiredHeight = [string boundingRectWithSize:constrainedSize options:NSStringDrawingUsesLineFragmentOrigin context:nil];
if (requiredHeight.size.width > constrainedSize.width) {
requiredHeight = CGRectMake(0,0, constrainedSize.width, requiredHeight.size.height);
}
CGRect newFrame = frame;
newFrame.size.height = requiredHeight.size.height;
frame = newFrame;
return frame;
}
my tabelview method is as follows:
CGRect descFrame=[Utility getlabelHeight:CGRectMake(65,35+titleFrame.size.height, 250, 20) withFontName:appFont andFontSize:15 andText:[[allComments objectAtIndex:indexPath.row]valueForKey:#"comment"]];
NSLog(#"%# \n %f",[[allComments objectAtIndex:indexPath.row]valueForKey:#"comment"],descFrame.size.height);
[cell.title setFont:[UIFont fontWithName:appFont size:15]];
[cell.title setText:[[allComments objectAtIndex:indexPath.row]valueForKey:#"comment"]];
[cell.title setLineBreakMode:NSLineBreakByWordWrapping];
[cell.title setNumberOfLines:0];
[cell.title setFrame:descFrame];
[cell.title setTextColor:[UIColor blackColor]];
Please help me!!1 i am screwed...
thanks you!!!

String with emoji is displaced when using NSForegroundColorAttributeName with iOS 7

when I'm using NSForegroundColorAttributeName with a string that includes an emoji the string is displaced vertically.
Here's an example:
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 21)];
label1.text = #"#Hashtag";
//label1.backgroundColor = [UIColor redColor];
label1.numberOfLines = 0;
label1.shadowColor = [UIColor colorWithWhite:1 alpha:0.5];
label1.shadowOffset = CGSizeMake(1,1);
[label1 sizeToFit];
[self.view addSubview:label1];
NSString *comment = #"#Hashtag 👍";
NSMutableAttributedString *commentAttributedString = [[NSMutableAttributedString alloc] initWithString:comment];
[commentAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:NSMakeRange(0, comment.length)];
[commentAttributedString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 8)];
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(10, 100, 300, 21)];
label2.attributedText = commentAttributedString;
label2.numberOfLines = 0;
label2.shadowColor = [UIColor colorWithWhite:1 alpha:0.5];
label2.shadowOffset = CGSizeMake(1,1);
[label2 sizeToFit];
[self.view addSubview:label2];
I get the following result:
The green label is displaced. This happens only with iOS 7, with iOS 6 everything works fine.
Does anybody know why this happens?
Kind regards
Arno

UILabels crashing my app

So I don't know any way to draw text or strings but a UILabel. So I initialized the label, but it keeps crashing my app.
Here are the methods that initialize the labels and are causing the crash:
-(void) setupScore{
scoreLabel = [NSString stringWithFormat:#"%d", score];
scoreLabel.frame = CGRectMake(262, 250, 100, 40);
[scoreLabel setText: scoreString];
//normally you'll want a transparent background for your label
scoreLabel.backgroundColor = [UIColor clearColor];
//you can use non-standard fonts
[scoreLabel setFont:[UIFont fontWithName:#"TimesNewRoman" size: 1.0f]];
//change the label's text color
scoreLabel.textColor = [UIColor whiteColor];
//add it to your view
scoreLabel.transform = CGAffineTransformMakeRotation(89.53);
[self addSubview:scoreLabel]; }
-(void) setupPausedLabel{
pausedLabel = [NSString stringWithFormat:#"Tap To Resume"];
pausedLabel.frame = CGRectMake(262, 250, 100, 40);
[pausedLabel setText: #"Tap To Resume"];
//normally you'll want a transparent background for your label
pausedLabel.backgroundColor = [UIColor clearColor];
//you can use non-standard fonts
[pausedLabel setFont:[UIFont fontWithName:#"TimesNewRoman" size: 1.0f]];
//change the label's text color
pausedLabel.textColor = [UIColor whiteColor];
//add it to your view
pausedLabel.transform = CGAffineTransformMakeRotation(89.53);
[pausedLabel setHidden: YES];
[self addSubview:pausedLabel]; }
Thanks for your help!
You are doing :
scoreLabel = [NSString stringWithFormat:#"%d", score];
You should do :
scoreLabel = [[UILabel alloc] init]; // And release it somewhere
scoreString = [NSString stringWithFormat:#"%d", score];
and same for the pauseLabel.
You are wrong in your first line.
scoreLabel = [NSString stringWithFormat:#"%d", score];
scoreLabel=[[UILable alloc]init];
scoreLabel.text=[NSString stringWithFormat:#"%i",score];
And Same Problem in NextMethod
pausedLabel = [NSString stringWithFormat:#"Tap To Resume"];
It's not Right
pausedLabel=[[UILable alloc]init];
pausedLable.text=[NSString stringWithFormat:#"Tap To Resume"];
UILable text Property you are messing so it's give you crashing problem
You are initializing your label with a NSString class it should be a UILabel. Make sure you generate your string with the good formater: %d for integer and %# for an NSString object. Check this doc to find the appropriate specifier:
NSString* scoreString = [NSString stringWithFormat:#"%d", score];
NSString* anotherString = #"Tap To Resume";
NSString* pausedString = [NSString stringWithFormat:#"%#", anotherString];
-(void) setupScore{
scoreLabel.text = [NSString stringWithFormat:#"%d", score];
scoreLabel.frame = CGRectMake(262, 250, 100, 40);
[scoreLabel setText: scoreString];
//normally you'll want a transparent background for your label
scoreLabel.backgroundColor = [UIColor clearColor];
//you can use non-standard fonts
[scoreLabel setFont:[UIFont fontWithName:#"TimesNewRoman" size: 1.0f]];
//change the label's text color
scoreLabel.textColor = [UIColor whiteColor];
//add it to your view
scoreLabel.transform = CGAffineTransformMakeRotation(89.53);
[self.view addSubview:scoreLabel];
}
It's working fine I'm calling it in viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
scoreLabel=[[UILabel alloc]init];
scoreString=[NSString stringWithString:#"1221"];
score=10;
[self setupScore];
scoreString=[NSString stringWithString:#"11"];
[self setupScore];
scoreString=[NSString stringWithString:#"333"];
[self setupScore];
[self setupScore];
scoreLabel.text=#"okey...";
}