粗体&单个 UILabel 中的非粗体文本?
如何在 uiLabel 中同时包含粗体和非粗体文本?
我不想使用 UIWebView ..我也读过使用 NSAttributedString 可能可以实现这一点,但我不知道如何使用它。有什么想法吗?
苹果在他们的几个应用程序中实现了这一点; 示例屏幕截图:
谢谢! - 多姆
How would it be possible to include both bold and non-bold text in a uiLabel?
I'd rather not use a UIWebView.. I've also read this may be possible using NSAttributedString but I have no idea how to use that. Any ideas?
Apple achieves this in several of their apps;
Examples Screenshot:
Thanks!
- Dom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
更新
在 Swift 中,我们不必处理 iOS5 的旧东西,除了语法更短之外,一切都变得非常简单:
Swift 5
Swift 3
用法:
奖励:国际化
有些人对国际化发表了评论。我个人认为这超出了这个问题的范围,但出于教学目的,这就是我要做的
结果(假设英语和日语 Localized.strings 可用)
iOS6 及更高版本的先前答案(Objective-C 仍然有效):
在 iOS6
UILabel
、UIButton
、UITextView
、UITextField
中>,支持属性字符串,这意味着我们不需要创建 CATextLayer 作为属性字符串的接收者。此外,为了制作属性字符串,我们不再需要使用 CoreText :) 我们在 obj-c Foundation.framework 中提供了新类,例如NSParagraphStyle
和其他常量,这将使我们的生活更轻松。耶!因此,如果我们有这个字符串:
我们只需要创建属性字符串:
这里有一些很好的介绍性博客文章这里< /a> 来自 pregnantcode 的人员,他们用更多示例解释了
NSAttributedString
的使用,请查找“Introduction to NSAttributedString for iOS 6” 和 >“使用 Interface Builder 的 iOS 属性字符串” :)PS:上面的代码应该可以工作,但它是大脑编译的。我希望这已经足够了:)
iOS5 及以下版本的旧答案
使用 CATextLayer 带有 NSAttributedString !比 2 个 UILabels 更轻、更简单。 (iOS 3.2 及更高版本)
示例。
不要忘记添加 QuartzCore 框架(CALayers 所需)和 CoreText(属性字符串所需)。
下面的示例将向导航控制器的工具栏添加一个子层。 iPhone 中的 Mail.app。 :)
在这个例子中,我只有两种不同类型的字体(粗体和普通),但你也可以有不同的字体大小、不同的颜色、斜体、下划线等。
看看 NSAttributedString / NSMutableAttributedString 和 CoreText 属性字符串键。
Update
In Swift we don't have to deal with iOS5 old stuff besides syntax is shorter so everything becomes really simple:
Swift 5
Swift 3
Usage:
Bonus: Internationalisation
Some people commented about internationalisation. I personally think this is out of scope of this question but for instructional purposes this is how I would do it
Result (Assuming English and Japanese Localizable.strings are available)
Previous answer for iOS6 and later (Objective-C still works):
In iOS6
UILabel
,UIButton
,UITextView
,UITextField
, support attributed strings which means we don't need to createCATextLayer
s as our recipient for attributed strings. Furthermore to make the attributed string we don't need to play with CoreText anymore :) We have new classes in obj-c Foundation.framework likeNSParagraphStyle
and other constants that will make our life easier. Yay!So, if we have this string:
We only need to create the attributed string:
There is a couple of good introductory blog posts here from guys at invasivecode that explain with more examples uses of
NSAttributedString
, look for "Introduction to NSAttributedString for iOS 6" and "Attributed strings for iOS using Interface Builder" :)PS: Above code it should work but it was brain-compiled. I hope it is enough :)
Old Answer for iOS5 and below
Use a CATextLayer with an NSAttributedString ! much lighter and simpler than 2 UILabels. (iOS 3.2 and above)
Example.
Don't forget to add QuartzCore framework (needed for CALayers), and CoreText (needed for the attributed string.)
Below example will add a sublayer to the toolbar of the navigation controller. à la Mail.app in the iPhone. :)
In this example I only have two different types of font (bold and normal) but you could also have different font size, different color, italics, underlined, etc.
Take a look at NSAttributedString / NSMutableAttributedString and CoreText attributes string keys.
在 UILabel 上尝试一个类别:
这里是它的使用方式:
这是类别
UILabel+Boldify.h
UILabel+Boldify.m
请注意,这将仅适用于 iOS 6 及更高版本。在 iOS 5 及更早版本中它将被忽略。
Try a category on UILabel:
Here's how it's used:
And here's the category
UILabel+Boldify.h
UILabel+Boldify.m
Note that this will only work in iOS 6 and later. It will simply be ignored in iOS 5 and earlier.
在Interface Builder中这很容易做到:
1)在属性检查器中创建UILabel 属性
2) 选择要加粗的部分短语
3) 在字体选择器中更改其字体(或相同字体的粗体字体)
就这样!
That's easy to do in Interface Builder:
1) make UILabel Attributed in Attributes Inspector
2) select part of phrase you want to make bold
3) change its font (or bold typeface of the same font) in font selector
That's all!
有一个基于 bbrame 类别的类别。它的工作原理类似,但允许您使用累积结果多次加粗相同的
UILabel
。UILabel+Boldify.h
UILabel+Boldify.m
通过此更正,您可以多次使用它,例如:
将导致:“更新: 2012/10/14 21:59 PM”。
There's category based on bbrame's category. It works similar, but allows you boldify same
UILabel
multiple times with cumulative results.UILabel+Boldify.h
UILabel+Boldify.m
With this corrections you may use it multiple times, eg:
will result with: "Updated: 2012/10/14 21:59 PM".
它对我有用:
对于 Swift 版本:请参阅这里< /a>
It worked for me:
For Swift version: See Here
我采用了 Crazy Yoghurt 对 swift 扩展的回答。
可能 Range 和 NSRange 之间没有很好的转换,但我没有找到更好的东西。
I've adopted Crazy Yoghurt's answer to swift's extensions.
May be there is not good conversion between Range and NSRange, but I didn't found something better.
查看 TTTAttributedLabel。它是 UILabel 的直接替代品,允许您通过将 NSAttributedString 设置为该标签的文本来在单个标签中混合字体和颜色。
Check out TTTAttributedLabel. It's a drop-in replacement for UILabel that allows you to have mixed font and colors in a single label by setting an NSAttributedString as the text for that label.
在这种情况下你可以尝试,
In this case you could try,
使 UILabel 中的文本加粗并加下划线。只需在您的代码中添加以下几行即可。
To make text bold as well as underline in a UILabel. Just add the following lines in your code.
斯威夫特4:
Swift 4:
提供要处理的字符串作为输入,并提供应为粗体/彩色的单词作为输入。
Supply the string to process as input and supply the words which should be bold/colored as input.
不需要 NSRange ,我刚刚在我的项目中实现了以下代码(在 Swift 中):
No need for NSRange with the following code I just implemented in my project (in Swift):
如果您想更轻松地使用属性字符串,请尝试使用属性字符串创建器,它将为您生成代码。 https://itunes.apple.com/us/app/attributed-string -creator/id730928349
If you want to make using attributed strings easier, try using Attributed String Creator, which will generate the code for you. https://itunes.apple.com/us/app/attributed-string-creator/id730928349
AttributeString 具有接受标记字符串的构造函数,这样做可能意味着您的属性字符串没有其他属性,因此如果您使用 xibs,您可以将文本不加粗体,设置为您想要的所有其他属性,然后在代码枚举标记字符串的属性范围,并将它们应用到从 xib 文件中获取的属性字符串,然后将其重新应用到特定字段的属性 attributeString。
AttributeString has constructors that take mark down strings, doing it this way can mean your attribute string has no other attributes, so if you are using xibs, you can have the text without the bold, set to all the other attributes you want, then in code enumerate through the attribute ranges of your mark down string and apply them to the attributed string you get from you xib file, and then reapply it to your attributed attributeString of the particular field.