iPhone/iPad的UILabel如何设置粗体和斜体?
如何在iPhone/iPad的UILabel
上设置粗体和斜体? 我搜索了论坛,但没有任何帮助。有人可以帮助我吗?
How do I set bold and italic on UILabel
of iPhone/iPad?
I searched the forum but nothing helped me. Could anyone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
我最近写了一篇关于 UIFont API 的限制以及如何解决的博文。
您可以在此处
使用我在那里提供的代码,您可以轻松获得所需的 UIFont,如下所示:
UIFont *myFont = [FontResolver fontWithDescription:@"font-family: Helvetica; font-weight: bold; font-style : italic;"];
然后将其设置为您的 UILabel (或其他):
myLabel.font = myFont;
I recently wrote a blog post about the restrictions of the UIFont API and how to solve it.
You can see it at here
With the code I provide there, you can get your desired UIFont as easy as:
UIFont *myFont = [FontResolver fontWithDescription:@"font-family: Helvetica; font-weight: bold; font-style: italic;"];
And then set it to your UILabel (or whatever) with:
myLabel.font = myFont;
正如这些答案中已经提到的,您只需要正确的字体名称。我发现 iOSFonts.com 是准确了解要使用的名称的最有用的资源。
As has already been mentioned in these answers, you just need the right font name. I find that iOSFonts.com is the most helpful resource for knowing exactly what name to use.
这很简单。这是代码。
这会对你有所帮助。
This is very simple. Here is the code.
This will help you.
很多时候,粗体文本被视为另一级别的信息架构方式,因此一行中没有粗体和常规,因此您可以将其拆分为两个标签/文本视图,一个常规,一个粗体斜体。并使用编辑器选择字体样式。
Many times the bolded text is regarded in an information architecture way on another level and thus not have bolded and regular in one line, so you can split it to two labels/textViews, one regular and on bold italic. And use the editor to choose the font styles.
使用Swift 5
With Swift 5
只是想提一下,
他们有不同的结果......
Just want to mention that,
They have different outcome...
针对 Swift 5 进行了更新,并尊重设备的首选尺寸类别。
这里发布的其他答案都很好,但有硬编码的 pointSizes。
Updated for Swift 5 and respects the device's preferred size category.
The other answers posted here are good, but have hard-coded pointSizes.
不要尝试使用字体名称。使用字体描述符,您不需要名称:
size:0
表示“保持原样大小”使用 Swift 尝试以下扩展:
然后您可以这样使用它:
甚至添加其他特征,例如 Condensed:
Swift 4 更新:
按如下方式使用:
myLabel.font = myLabel.font.bold
或
myLabel.font = myLabel.font.italic
或
myLabel.font = myLabel.font.with(traits: [ .traitBold, .traitCondensed ])
Swift 5 更新
Don't try to play with the font names. Using the font descriptor you need no names:
size:0
means 'keep the size as is'With Swift try the following extension:
Then you may use it this way:
or even add additional traits like Condensed:
Update for Swift 4:
Use it as follows:
myLabel.font = myLabel.font.bold
or
myLabel.font = myLabel.font.italic
or
myLabel.font = myLabel.font.with(traits: [ .traitBold, .traitCondensed ])
Update for Swift 5
您可以设置一个字体名称列表来代替“fontWithName”属性。链接位于此处
There is a list of font names that you can set in place of 'fontWithName' attribute.The link is here
@Edinator 看看这个..
一次使用上面的任何一个你想要的
@Edinator have a look on this..
use any one of the above at a time you want
您可以通过单击字体字段中的字母“T”来设置标签的任何字体样式、系列、大小。
You can set any font style, family, size for the label, by clicking on letter "T" in Font field.
Swift 3
粗体:
let bondFont = UIFont.boldSystemFont(ofSize:UIFont.labelFontSize)
斜体:
让 italicFont = UIFont.italicSystemFont(ofSize:UIFont.labelFontSize)
Swift 3
Bold:
let bondFont = UIFont.boldSystemFont(ofSize:UIFont.labelFontSize)
Italic:
let italicFont = UIFont.italicSystemFont(ofSize:UIFont.labelFontSize)
对于 iOS 7 系统默认字体,如果您希望保留系统默认字体,您将使用 helvetica neue 粗体。
或者你可以简单地调用它:
With iOS 7 system default font, you'll be using helvetica neue bold if you are looking to keep system default font.
Or you can simply call it:
我有同样的问题,需要在标签和按钮上应用粗体和斜体。您可以简单地使用以下代码来实现此效果:
I have the same issue that need to apply both Bold and Italic on a label and button. You can simply use following code to achieve this effect:
我对马克西米利安·沃贾科夫斯基的反应做了一个变体
您可以在其中添加或删除特征,
例如
,当重复使用单元格并且您想要删除在前一个单元格中的标签上放置的斜体时,它很有用
I made a variation of the response of maksymilian wojakowski
where you can add or remove a trait(s)
exemple
it useful when reusing cell and you want to remove the italic you put on a label in a previous cell
您也可以使用它来制作粗体标签/按钮字体
you can do bold label/button font also using this
粗体文本示例:
斜体文本示例:
Example Bold text:
Example Italic text:
尽管@tolbard 提供的答案令人惊叹并且效果很好!
我觉得为只需一行代码即可实现的东西创建一个扩展将是一种过度的杀伤力。
通过使用 UIFontDescriptor 设置字体属性,您可以为
label
中的相同文本获得粗体以及斜体样式> 如下例所示,使用 Swift 4.0:其他选项包括:
有关这些符号特征含义的更多信息? 访问此处
Although the answer provided by @tolbard is amazing and works well!
I feel creating an extension for something that can be achieved in just a line of code, would be an over kill.
You can get bold as well italic styling for the same text in your
label
by setting up the font property usingUIFontDescriptor
as shown below in the example below using Swift 4.0:Other options include:
For more information on what those symbolic traits mean? visit here
更新 Maksymilian Wojakowski 对 swift 3 的精彩答案
Updating Maksymilian Wojakowski's awesome answer for swift 3