【问题标题】:Partially Bold Text in UILabel [duplicate]UILabel中的部分粗体文本[重复]
【发布时间】:2017-03-21 11:15:43
【问题描述】:

我有一个 tableView,其中的单元格由两个数组填充:

var names = ["Marie : ", "Nicolas : " , "Sarah : "]

var colors = ["White" , "Blue" , "Red"]

我的单元格由我的数组填充在一个名为“info”的 UILabel 中:

cell.info.text = names[indexPath.row] + colors[indexPath.row]

一切正常,我只是没有设法让我的“名称”数组出现在 bold 中而不触及“颜色”数组。

最好的方法是什么?

提前感谢您的帮助!

【问题讨论】:

    标签: ios swift uilabel


    【解决方案1】:

    如果您只想让名称以粗体显示,请使用UILabel attributedText 属性来设置属性字符串而不是纯文本字符串。

    类似:

    let name = names[indexPath.row]
    let color = colors[indexPath.row]
    let attributedText = NSMutableAttributedString(string: name, attributes:[NSFontAttributeName : UIFont.boldSystemFont(ofSize:15)])
    attributedText.append(NSAttributedString(string:color))
    cell.info.attributedText = attributedText
    

    当然,您需要根据单元格样式设置上面的字体为粗体部分。

    【讨论】:

    • 感谢您的快速回答!似乎这种方法只能用于简单的字符串。如果我错了,请告诉我。我没有找到与字符串数组一起使用此方法的任何示例。
    • 一个字符串数组在这里仍然可以与字符串一样工作,因为您只是从数组中提取一个值 - 不显示整个数组:) 所以它不应该有任何区别。我将修改我上面的代码以使用您的数据来展示它是如何完成的。
    • 您正在将属性文本分配给 text 属性,而应该分配给属性文本属性
    • @Fahim 你不能像这样附加它,你需要把它分成两行。 let attributedText = NSMutableAttributedString(string: name, attributes:[NSFontAttributeName : UIFont.boldSystemFont(ofSize: 15)])
    • @LeoDabus 你是对的,只是测试了代码,链接不起作用。会改变我的答案。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 2014-09-21
    • 2016-05-30
    • 2012-12-10
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 2016-07-28
    相关资源
    最近更新 更多