IOS swift 有没有办法使文本的特定部分加粗 [英] IOS swift is there a way to make a specific portion of text bold

查看:38
本文介绍了IOS swift 有没有办法使文本的特定部分加粗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UITableview 可以从数据库中获取数据并将其显示在 UILabel 中.我想让文本的一部分显示... Read Less"为粗体,同时保留文本的其余部分.下面的代码只是检查帖子是否超过 120 个字符,如果是,那么我附加... Read Less",附加的语句我想加粗.现在我的整个帖子都加粗了,而不仅仅是附加的字符串,任何建议都会很棒

I have a UITableview that gets data from the database and I display it in a UILabel. I want to make a portion of the text that says " ... Read Less" to be bold while leaving the rest of the text alone. The code below simply checks if the post has more than 120 characters if so then I append " ... Read Less " that statement appended I would like to make bold. Right now I have my entire post bold instead of just the appended string any suggestions would be great

func HomeProfilePlaceTVC(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "HomeTVC", for: indexPath) as! HomeTVC

    cell.post.tag = indexPath.row 

    if streamsModel.Posts[indexPath.row].count > 120 {
            cell.post.text = String(streamsModel.Posts[indexPath.row]).appending(" ... Read Less")
            cell.post.font =  UIFont(name:"HelveticaNeue-Bold", size: 15.0)
        }

        else {
             cell.post.text = streamsModel.Posts[indexPath.row]
        }
    return cell

}

推荐答案

您可以像这样创建扩展.

You can create an extension like this.

extension String {
    func attributedString(with style: [NSAttributedString.Key: Any]? = nil,
                          and highlightedText: String,
                          with highlightedTextStyle: [NSAttributedString.Key: Any]? = nil) -> NSAttributedString {

        let formattedString = NSMutableAttributedString(string: self, attributes: style)
        let highlightedTextRange: NSRange = (self as NSString).range(of: highlightedText as String)
        formattedString.setAttributes(highlightedTextStyle, range: highlightedTextRange)
        return formattedString
    }
}

并像这样调用此方法将中间的字符串设为粗体

And call this method like this for making a string in the middle as Bold

let descriptionText = "This is a bold string"
let descriptionText = descriptionText.attributedString(with: [.font: UIFont.systemFont(ofSize: 12.0, weight: .regular),
                                                              .foregroundColor: .black],
                                                    and: "bold",
                                                    with: [.font: UIFont.systemFont(ofSize: 12.0, weight: .bold),
                                                        .foregroundColor: .black])

输出:这是一个粗体字符串".您可以将其设置为 UILabel,如下所示.

Output: "This is a bold string". You can set this to UILabel as follows.

 textLabel.attributedString = descriptionText

此方法可用于突出显示整个字符串中具有不同样式(例如粗体、斜体等)的任何文本范围.如果这有帮助,请告诉我.

This method can be used to highlight any range of text in the entire string with different styles, say bold, italics etc. Please let me know if this helps.

这篇关于IOS swift 有没有办法使文本的特定部分加粗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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