Reputation: 821
I'd need a UIButton
to display two lines of text, where the first one acts as a title and the second as some kind of subtitle, so it should be of a smaller font size.
By now my button has two lines, custom font, and the content for the title/subtitle come from a UILabel
each.
UIFont *displayFont = [UIFont fontWithName:@"Chalkboard" size:15];
NSString *buttonLabel;
UILabel *headline = [[UILabel alloc] init];
headline.text = @"This is the title";
UILabel *subtitle = [[UILabel alloc] init];
subtitle.text = @"this is the sub";
buttonLabel = [NSString stringWithFormat:@"%@\n%@", headline.text, subtitle.text];
[openDetail.titleLabel setLineBreakMode:UILineBreakModeWordWrap];
[openDetail setTitle:buttonLabel forState:UIControlStateNormal];
[openDetail.titleLabel setTextAlignment:UITextAlignmentCenter];
openDetail.titleLabel.font = displayFont;
I tried to set the Labels themselves to displayFont
, so I could make a second UIFont
, making the subtitle of it. Unfortunately the button label won't be of this font, if I keep the rest as shown above, but will switch back to Arial, I think it is, and didn't change the font size at all.
Any idea how I could achieve a smaller font size in the second line?
Side question for the font: I added Chalkboard.ttc to my Resources and added it as an entry in my info.plist as "Fonts provided by application". Still, if I try to set the font in IB, I'll get another font shown (very similar to Helvetica). Do I really have to set every buttons font programmatically now?
Upvotes: 2
Views: 10453
Reputation: 1286
Use UIButton.Configuration: (iOS 15)
if (@available(iOS 15.0, *)) {
UIButtonConfiguration * configuration = _button.configuration;
[configuration setSubtitle:@"Subtitle text"];
[_button setConfiguration:configuration];
}
Upvotes: 0
Reputation: 5517
Your problem is that there is only a single label in the system button you are using. And that can only have one font.
If you want more complex buttons you have to build them yourself.
One option for you is to just add your subtitle to the existing button. Here is a simple example. I assume you have an outlet for your IB button called myButton.
UILabel *subTitle = [[[UILabel alloc] initWithFrame:CGRectMake(0, 20, 100, 30)] autorelease];
[subTitle setText:@"Subtitle"];
[subTitle setTextColor:[UIColor blackColor]];
[subTitle setBackgroundColor:[UIColor clearColor]];
[subTitle setFont:[UIFont fontWithName:@"American Typewriter" size:12]];
[myButton setTitle:@"Main title" forState:UIControlStateNormal];
[myButton addSubview:subTitle];
If this is something you need in more than one place you should turn the button into it's own class with nice attributes for the title and subtitle. Actually you should do it anyway. :-)
Upvotes: 0
Reputation: 547
Try this
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(20 , 13, 200, 85)];
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(8, 8, 185, 30)];
[title setBackgroundColor:[UIColor clearColor]];
[title setFont:[UIFont fontWithName:@"Chalkboard" size:14]];
[title setFont:[UIFont boldSystemFontOfSize:18]];
title.text=@"This is the title";
[title setTextColor:[UIColor blackColor]];
[btn addSubview:title];
UILabel *subtitle = [[UILabel alloc]initWithFrame:CGRectMake(8, 20, 185, 30)];
[subtitle setBackgroundColor:[UIColor clearColor]];
[subtitle setFont:[UIFont fontWithName:@"Helvetica" size:14]];
subtitle.text=@"This is the sub";
[subtitle setTextColor:[UIColor blackColor]];
[btn addSubview:subtitle];
[title release];
[subtitle release];
[self.view addSubview:btn];
Upvotes: 5