个人学习笔记存放地

修改字体的方法

上一篇 / 下一篇  2008-03-04 15:30:29 / 个人分类:MAC

不很完善,没有错误处理

- (NSFont*) changeUsrFont:(NSFont*)usrFont
    sFontName:(NSString*) fontName
     bBold:(BOOL) bold
      bItalic:(BOOL) italic
      size:(double) dFontSize

 NSFontManager* FM = [NSFontManager sharedFontManager];
 
 dFontSize = (dFontSize < 1.0) ? 1.0 : dFontSize;
 
 NSFontTraitMask s = 0;
 
 if (italic)
 {
  s |= NSItalicFontMask;
 }
 else
 {
  s |= NSUnitalicFontMask;
 }
 
 if (bold)
 {
  s |= NSBoldFontMask;
 }
 else
 {
  s |= NSUnboldFontMask;
 }
 
 if (!usrFont)
 {
  usrFont = [FM fontWithFamily:fontName traits:s weight:5 size:dFontSize];
  
  if (!usrFont) // this is bad; the wrong font name ends up in the font popups - FIXME please!
  {
   /* Oops!  We don't have that font here.
   * first try "Times New Roman", which should be sensible, and should
   * be there unless the user fidled with the installation
   */
   NSLog (@"Unable to find font \"%s\".", fontName);
   
   usrFont = [[NSFontManager sharedFontManager] fontWithFamily:@"Times" traits:s weight:5 size:dFontSize];
  }
 }
 else
 {
  usrFont = [FM convertFont:usrFont toFamily:fontName];
  usrFont = [FM convertFont:usrFont toHaveTrait:s];
  usrFont = [FM convertFont:usrFont toSize:dFontSize];
 }
 
 return usrFont; 
}


TAG: 字体 dFontSize BOOL NSFont double

 

评分:0

我来说两句

显示全部

:loveliness: :handshake :victory: :funk: :time: :kiss: :call: :hug: :lol :'( :Q :L ;P :$ :P :o :@ :D :( :)

我的栏目

日历

« 2008-08-11  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 157
  • 日志数: 17
  • 图片数: 1
  • 文件数: 4
  • 建立时间: 2008-01-26
  • 更新时间: 2008-07-09

RSS订阅

Open Toolbar