ios7以上自定义导航栏标题的字体大小及颜色的方法
自定义导航栏的字体和颜色,只需要自定义一个lable,然后将lable添加到导航栏的titleview中就可以了
代码如下
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)];//这个frame是初设的,没关系,后面还会重新设置其size。 [label setNumberOfLines:0]; UIFont *font = [UIFont fontWithName:@"Arial" size:17]; label.font = font; label.text = @"二维码结果"; CGSize size = CGSizeMake(320,2000); //设置一个行高上限 NSDictionary *attribute = @{NSFontAttributeName: label.font}; CGSize labelsize = [label.text boundingRectWithSize:size options: NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:attribute context:nil].size; //动态根据文本内容设置lable的大小 [label setFrame:CGRectMake(0,0, labelsize.width, labelsize.height)];
self.navigationItem.titleView = label;
假如想获取ios系统本身所自带的全部字体,并且在调试模式下输出,可使用以下代码
NSArray* fontFamilys = [UIFont familyNames]; for (int i = 0; i<[fontFamilys count]; i++) { NSString* family = [fontFamilys objectAtIndex:i]; NSLog(@"系统自带字体有:%@rn=====",family); NSArray* fonts = [UIFont fontNamesForFamilyName:family]; for (int j = 0; j<[fonts count]; j++) { NSLog(@"%@",[fonts objectAtIndex:j]); } }
假如想换一个字体库使用,如使用微软雅黑字体,则可以使用如下代码
1、网搜索字体文件(缀名.ttf,或.odf)
2.字体库导入工程resouce
3.程序viewdidload加载段代码
NSArray *familyNames = [UIFont familyNames]; for( NSString *familyName in familyNames ){ printf( "Family: %s \n", [familyName UTF8String] ); NSArray *fontNames = [UIFont fontNamesForFamilyName:familyName]; for( NSString *fontName in fontNames ){ printf( "\tFont: %s \n", [fontName UTF8String] ); } }
4.假加入字体微软雅黑NSLog看MicrosoftYaHei
5.工程Info.plist文件新建行(Add Row)添加key:UIAppFonts,类型Array或Dictionary都行;UIAppFonts再建立键值,key:Item 0,添加ValueXXX.ttf(字体名字,string型),添加
6.项目要用字体候 xx.font = [UIFont fontWithName:@"MicrosoftYaHei" size:20.0]