简体   繁体   中英

UIButton Text Size Issues

I have the following code:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"My Title" forState:UIControlStateNormal];
button.frame = myCustomRect;
UIColor *fontColor = [UIColor colorWithRed:0.43 green:0.84 blue:0.86 alpha:0.9];
[button setTitleColor:fontColor forState:UIControlStateNormal];
button.backgroundColor = [UIColor whiteColor];
button.titleLabel.textAlignment = NSTextAlignmentCenter;
button.titleLabel.font = [UIFont fontWithName:@"Cooperplate" size:9];
[button addTarget:self
                action:@selector(moreDetails:)
 forControlEvents:UIControlEventTouchUpInside];    
[self.view addSubview:button];

I am trying to add a button programmatically to the view. MyCustomRect's parameters are (0,0,75,50).

Two things: a) No matter what font I enter, it always shows the System font. b) The size also doesn't change at all. I tried increasing the font size and it doesn't change and I tried reducing the font size and it doesn't change either.

What am I doing wrong?

You can use your own custom fonts in your project as mentioned here

Once you add a custom font to your project, you can use it as,

[button.titleLabel setFont:[UIFont fontWithName:@"Cooperplate" size:24.0]];

First, in XCode locate the application .plist file. Usually called, [YOUR_APP_NAME]-Info.plist. Once you locate the file, add a new row of key: "Fonts provided by application". XCode will create the first key/value pair for you. Here is a screenshot of my .plist file entry.

Image

Add the name of the font file in the "value" column. Be sure to spell the name of the font file EXACTLY as it is in seen in the list of files in your project. This is NOT the name you will be referencing from within your UIFont statement. This is just so your app knows where to find the font you will be referencing.

Next, you need the name of the fontface you are going to use. Now you reference it in your app.

Try this for UIButton

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];
[title release];
[self.view addSubview:btn];

试试这个

[button.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:13.0]];

Use this line of code after : This should resolve the issue with size

button.titleLabel.font = [UIFont fontWithName:@"Cooperplate" size:9];
button.titleLabel.font = [UIFont systemFontOfSize:30];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM