Problems with UIButton Text Size

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 a view. MyCustomRect Parameters: (0,0,75,50).

Two things: a) No matter what font I entered, it always displays the system font. b) Size also does not change at all. I tried to increase the font size, and it did not change, and I tried to reduce the font size, and it will not change either.

What am I doing wrong?

+4
source share
4 answers

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

After adding a special font to the project, you can use it as

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

First, in Xcode, find the .plist application file. Commonly called, [YOUR_APP_NAME] -Info.plist. After you find the file, add a new line key: "Fonts provided by the application." Xcode will create the first key / value for you. Here is a screenshot of my .plist file entry.

Picture

Add the font file name to the value column. Be sure to spell the font file name EXACTLY, as shown in the list of files in your project. This is NOT the name you will refer to from your UIFont application. It’s just that your application knows where to find the font to which you will refer.

Then you need the name of the font that you are going to use. You are now linking to it in your application.

+3
source

Try it 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]; 
+2
source

Try using

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

Use this line of code after: This should fix the size problem.

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

Source: https://habr.com/ru/post/1442238/


All Articles