欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

IOS開(kāi)發(fā)中使用UIFont設(shè)置字體及批量創(chuàng)建控件

 更新時(shí)間:2016年03月31日 16:32:55   作者:jiwangbujiu  
這篇文章主要介紹了IOS開(kāi)發(fā)中使用UIFont設(shè)置字體及批量創(chuàng)建控件的方法,內(nèi)容很實(shí)用,感興趣的小伙伴們可以參考一下

在IOS 中,使用[UIFont familyNames]這個(gè)方法獲取72種系統(tǒng)字體。

使用[UIFont fontWithName:@"Zapfino" size:18]這個(gè)方法為空間中的文字設(shè)置字體和字號(hào)。

可以通過(guò)for循環(huán)批量定義控件并設(shè)置屬性。

以下程序獲取系統(tǒng)72種字體并存儲(chǔ)在一個(gè)數(shù)組中,有兩種方法,一種是通過(guò)for循環(huán)拿到每一種字體并添加到可變數(shù)組中,另一種是直接把72種字體賦值給一個(gè)數(shù)組。

注:在頁(yè)面控件較少的情況下選擇手動(dòng)創(chuàng)建每個(gè)控件,在控件數(shù)量較大且有規(guī)律排布的時(shí)候使用循環(huán)批量創(chuàng)建控件??梢酝ㄟ^(guò)獲取硬件設(shè)備的分辨率進(jìn)而讓控件的尺寸自動(dòng)適配設(shè)備。具體方式為:

//屏幕尺寸
CGRect rect = [[UIScreen mainScreen] bounds];
  CGSize size = rect.size;
  CGFloat width = size.width;
  CGFloat height = size.height;
  NSLog(@"print %f,%f",width,height);

//分辨率
CGFloat scale_screen = [UIScreen mainScreen].scale;
width*scale_screen,height*scale_screen

程序內(nèi)容:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  
  
//  定義一個(gè)可變數(shù)組,用來(lái)存放所有字體
  NSMutableArray *fontarray = [NSMutableArray arrayWithCapacity:10];
//  遍歷UI字體
  for (id x in [UIFont familyNames]) {
    NSLog(@"%@",x);
    [fontarray addObject:x];
  }
  
  
//  直接把字體存儲(chǔ)到數(shù)組中
  NSArray *fontarrauy2 = [UIFont familyNames];
  NSLog(@"%@",fontarrauy2);
  
  
//  創(chuàng)建一個(gè)label,用來(lái)顯示設(shè)定某種字體的字符串
  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];
  
//  新建一個(gè)可變數(shù)組,用來(lái)存放使用for循環(huán)批量創(chuàng)建的label
  NSMutableArray *labarr = [NSMutableArray arrayWithCapacity:100];
  
  for (int x=0; x<24; x++) {
    for (int y=0; y<3; y++) {
//      循環(huán)創(chuàng)建72個(gè)label,每個(gè)label橫向間距135-130=5,縱向間距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";
//      將創(chuàng)建好的label加入到可變數(shù)組
      [labarr addObject:lab];
    }
  }
  
//  使用for循環(huán)給72個(gè)label的字體設(shè)置各種字體格式
  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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論