首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有不同字号和颜色的UILabel文本

问具有不同字号和颜色的UILabel文本
EN

Stack Overflow用户
提问于 2016-01-15 22:46:52
回答 1查看 12.1K关注 0票数 16

如何使用Swift在UILabel中设置不同的字体大小和颜色?

我需要用不同于字符串其余部分的颜色和大小给字符串的第一个字符上色。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-15 22:46:52

假设您想要一个较小的灰色货币符号,如下所示:

​

​

只需使用NSMutableAttributedString对象:

代码语言:javascript
复制
let amountText = NSMutableAttributedString.init(string: "€ 60,00")

// set the custom font and color for the 0,1 range in string
amountText.setAttributes([NSFontAttributeName: UIFont.systemFontOfSize(12), 
                              NSForegroundColorAttributeName: UIColor.grayColor()],
                         range: NSMakeRange(0, 1))
// if you want, you can add more attributes for different ranges calling .setAttributes many times

// set the attributed string to the UILabel object
myUILabel.attributedText = amountText

Swift 5.3:

代码语言:javascript
复制
let amountText = NSMutableAttributedString.init(string: "€ 60,00")

// set the custom font and color for the 0,1 range in string
amountText.setAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
                              NSAttributedString.Key.foregroundColor: UIColor.gray],
                             range: NSMakeRange(0, 1))
// if you want, you can add more attributes for different ranges calling .setAttributes many times
// set the attributed string to the UILabel object

// set the attributed string to the UILabel object
myUILabel.attributedText = amountText
票数 46
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34813634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档