自定义字体在swift中不能正常工作 [英] custom font not working programetically in swift

查看:306
本文介绍了自定义字体在swift中不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照步骤在xcode中添加自定义字体 swift day-by-day


<自定义字体



,但无法在应用程序标签中设置该字体programetically

  var labeladd = UILabel(frame:CGRectMake(40,50,70,22))
// label.center = CGPointMake(160,284)
/// labeladd.font = UIFont.boldSystemFontOfSize(15)
labeladd.font = UIFont(名称:Source Sans Pro,size:15)
labeladd.textColor = UIColor.blackColor()
labeladd.textAlignment = NSTextAlignment.Center
labeladd。 text =this is custom fonts
myview.addSubview(labeladd)


解决方案

我们说你想添加这个字体: SourceSansPro-Regular.otf



三步:


  1. 将字体文件 SourceSansPro-Regular.otf 添加到您的项目中,确保您在添加到目标。

构建阶段,并确保其位于复制包资源。如果没有,添加它。









2.转到目标的信息。添加一个新的应用程序由应用程序提供的字体,然后添加一个新的项目,该值为 SourceSansPro-Regular.otf








3.打开OS X 字体书应用程序,导航至您的字体,然后按下 Command + i 。请注意 PostScript名称,并在您的Swift代码中使用该名称。在这种情况下,它是 SourceSansPro-Regular



< b

所以在你的代码中:
$ b $ p $ labeladd.font = UIFont(name:SourceSansPro-正常,大小:15)


i have followed the step to to add custom fonts in xcode at swift day-by-day

and custom fonts

but cant able to set that font in app label programetically

var labeladd = UILabel(frame: CGRectMake(40, 50, 70, 22))
    //  label.center = CGPointMake(160, 284)
 ///  labeladd.font=UIFont.boldSystemFontOfSize(15)
   labeladd.font = UIFont(name:"Source Sans Pro",size:15)
    labeladd.textColor=UIColor.blackColor()
    labeladd.textAlignment = NSTextAlignment.Center
    labeladd.text = "this is custom fonts"
    myview.addSubview(labeladd)

解决方案

Let's say you want to add this font: SourceSansPro-Regular.otf

Three steps:

  1. Add the font file SourceSansPro-Regular.otf to your project, make sure you select your target in the "Add to targets".

Go to the target's Build Phases and make sure it is under Copy Bundle Resources. If not, add it.



2. Go to the target's Info. Add a new entry Font provided by application then add a new item with this value SourceSansPro-Regular.otf.



3. Open the OS X Font Book application, navigate to your font then press Command+i. Note the PostScript name and use that name in your Swift code. In this case, it's SourceSansPro-Regular.



So in your code:

labeladd.font = UIFont(name:"SourceSansPro-Regular", size:15)

这篇关于自定义字体在swift中不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆