IOS字体库查看并获取字体名称

来源:互联网 发布:php 获取字符串后4位 编辑:程序博客网 时间:2024/06/03 11:16

1.编写初衷

     由于IOS中字体都是英文名称,根据设计去去选择字体的时候,经常只能看到字体名字,不能确定字体名字对应的字体是什么样子,所以写了一个demo。用于查看IOS系统中目前所有的可用字体,并展示到界面中,点击想要字体的按钮就弹出字体名字。       

2.获取所有字体名字

   

-(void)initFontArray{  fontNameArray=[[NSMutableArray alloc] init];  NSArray *familyNames = [[NSArray alloc] initWithArray:[UIFont familyNames]];  NSInteger indFamily, indFont;  for(indFamily=0;indFamily<[familyNames count];++indFamily)    {               NSArray* fontNames =[[NSArray alloc]initWithArray:[UIFont fontNamesForFamilyName:[familyNames objectAtIndex:indFamily]]];        for(indFont=0; indFont<[fontNames count]; ++indFont)        {            [fontNameArray addObject:[fontNames objectAtIndex:indFont]];        }            }}


3.展示到界面上

-(void)initScrollView{            UIScrollView *btnScrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];    [btnScrollView setBackgroundColor:[UIColor whiteColor]];    CGFloat jianGe=20;    CGFloat btnToLeft=40;    CGFloat btnHeight=30;    CGFloat btnToTop=40;    chinese=@"测试字体";        for (NSInteger index=0; index<[fontNameArray count]; index++) {                NSString *fontName=[fontNameArray objectAtIndex:index];        UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(btnToLeft, btnToTop, self.view.frame.size.width-2*btnToLeft, btnHeight)];        NSString *title=[[NSString alloc] initWithFormat:@"%@%ld",chinese,index];        [btn setTitle:title forState:UIControlStateNormal];        [btn addTarget:self action:@selector(titleBtnPress:) forControlEvents:UIControlEventTouchUpInside];        btn.titleLabel.textAlignment=NSTextAlignmentCenter;        [btn.layer setCornerRadius:4.0]; //设置矩形四个圆角半径        [btn.layer setBorderWidth:1.0];        btn.tag=index;        [btn.layer setBorderColor:[UIColor blueColor].CGColor];        [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];        [btn.titleLabel setFont:[UIFont fontWithName:fontName size:20]];        [btnScrollView addSubview:btn];        btnToTop=btnToTop+btnHeight+jianGe;    }    [btnScrollView setContentSize:CGSizeMake(self.view.frame.size.width, btnToTop)];    [self.view addSubview:btnScrollView];}

4.例子源码

  点击打开下载源码



0 0
原创粉丝点击