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

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 :)

Related

UINavigationBar navigationItem title height

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

OSX - how to change NSButton title color

My project have button as well as check box with default title color black i want to change color of title to red
Here's the code to set the button title color in a controller:
NSColor *color = [NSColor greenColor];
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
[style setAlignment:NSCenterTextAlignment];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
color, NSForegroundColorAttributeName, style, NSParagraphStyleAttributeName, nil];
NSAttributedString *attrString = [[NSAttributedString alloc]
initWithString:#"Title" attributes:attrsDictionary];
[self.button setAttributedTitle:attrString];
may be this is all you want.

how to create button from title string

I want create UIButton from NSString.
I have one string like this #"First Button" I want first get size of width pixel and after create one Button According to this string but I don't know about it.
please guide me.
You can get the CGSize of NSString using sizeWithAttributes (before we used sizeWithFont but this is now deprecated method with iOS 7) and then create UIButton according to the size like
NSString *string = #"First Button";
CGSize size = [string sizeWithAttributes:
#{NSFontAttributeName:
[UIFont systemFontOfSize:27.0f]}];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 50, size.width, size.height);
button.titleLabel.textColor = [UIColor blackColor];
[button setTitle:string forState:UIControlStateNormal];
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
Review this working fine for me.
Hope this will help you.
First you have to get the size of the text by using
NSString someText = #"some Text";
CGSize constraintSize = CGSizeMake(youMaxWidthForButton, youMaxHeightForButton);
CGRect labelSize = [someText boundingRectWithSize:constraintSize options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:[UIFont systemFontOfSize:17.0f]} context:nil];
than you create your UIButton
UIButton *button = [[UIButton alloc]initWithFrame:labelSize];
finally you set the button title
button.titleLabel.text = someText;

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!!!

How to Align the UISegment Text at the Centre

Hi have an App where in have to show UISegmentedControls but the text on the UISegment is coming out of the segment towards Left .How to algin the Text at the Centre of UISegmented Control?
optionControl = [[UISegmentedControl alloc]initWithItems:optionNameArr];
[optionControl setFrame:CGRectMake(ReportingPanelScrollView.frame.size.width/2.8+80, y_Val +x_Dis, 100, 30)];
UIFont *font = [UIFont systemFontOfSize:12.0f];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font,UITextAttributeFont,[UIColor darkTextColor],UITextAttributeTextColor,nil];
[optionControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
optionControl.segmentedControlStyle = UISegmentedControlStyleBordered;
optionControl.tag = sectionHeader*2000+itemHeader+200;
NSString *tagStr = [NSString stringWithFormat:#"%d",optionControl.tag];
[optionItemControlDict setObject:optionSelectedTextArr forKey:tagStr];
[optionControl addTarget:self action:#selector(OptionControlSelected:) forControlEvents:UIControlEventValueChanged];
[ReportingPanelScrollView addSubview:optionControl];
[optionControl release];
Try this:
optionControl.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;