IOS development USES UIFont to set fonts and bulk create controls
- 2020-05-27 07:16:55
- OfStack
In IOS, the [UIFont familyNames] method is used to get 72 system fonts.
Use [UIFont fontWithName:@"Zapfino" size:18] to set the font and font size for the text in the space.
You can define controls and set properties through for looping batch.
The following program takes 72 fonts from the system and stores them in an array. There are two ways to do this. One is to take each font and add it to a mutable array through an for loop, and the other is to assign 72 fonts directly to an array.
Note: choose to create each control manually when there are fewer controls on the page, and use looping batch to create controls when there are a large number of controls and they are regularly arranged. The size of the control can be automatically adapted to the device by obtaining the resolution of the hardware device. The specific method is as follows:
// The screen size
CGRect rect = [[UIScreen mainScreen] bounds];
CGSize size = rect.size;
CGFloat width = size.width;
CGFloat height = size.height;
NSLog(@"print %f,%f",width,height);
// The resolution of the
CGFloat scale_screen = [UIScreen mainScreen].scale;
width*scale_screen,height*scale_screen
Program content:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// define 1 A mutable array that holds all fonts
NSMutableArray *fontarray = [NSMutableArray arrayWithCapacity:10];
// traverse UI The font
for (id x in [UIFont familyNames]) {
NSLog(@"%@",x);
[fontarray addObject:x];
}
// Store the font directly in an array
NSArray *fontarrauy2 = [UIFont familyNames];
NSLog(@"%@",fontarrauy2);
// create 1 a label , a string used to display a font
UILabel *mylab1 = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 200, 50)];
mylab1.font = [UIFont systemFontOfSize:20];
mylab1.font = [UIFont fontWithName:@"Zapfino" size:18];
mylab1.font = [UIFont fontWithName:[fontarray objectAtIndex:10] size:18];
mylab1.text = @"HelloWorld";
[self.view addSubview:mylab1];
// new 1 A mutable array for storage and use for Circular batch creation label
NSMutableArray *labarr = [NSMutableArray arrayWithCapacity:100];
for (int x=0; x<24; x++) {
for (int y=0; y<3; y++) {
// Loop to create 72 a label , each label Horizontal spacing 135-130=5 , longitudinal spacing 30-28=2 .
UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(y*135+7, x*30+20, 130, 28)];
lab.backgroundColor = [UIColor colorWithRed:0.820 green:0.971 blue:1.000 alpha:1.000];
lab.text = @"HelloWorld";
// Will create the label Add to the mutable array
[labarr addObject:lab];
}
}
// use for Loop to 72 a label Font sets various font formats
for (int i=0; i<72; i++) {
UILabel *lab = [labarr objectAtIndex:i];
NSString *fontstring = [fontarray objectAtIndex:i];
lab.font = [UIFont fontWithName:fontstring size:18];
[self.view addSubview:[labarr objectAtIndex:i]];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
The above is the entire content of this article, I hope to help you with your study.