Bold & Non-Bold Text In A Single UILabel?

后端 未结 14 2203
别那么骄傲
别那么骄傲 2020-11-22 08:51

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 NSAttribut

14条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 09:39

    Hope this one will meets your need. Supply the string to process as input and supply the words which should be bold/colored as input.

    func attributedString(parentString:String, arrayOfStringToProcess:[String], color:UIColor) -> NSAttributedString
    {
        let parentAttributedString = NSMutableAttributedString(string:parentString, attributes:nil)
        let parentStringWords = parentAttributedString.string.components(separatedBy: " ")
        if parentStringWords.count != 0
        {
            let wordSearchArray = arrayOfStringToProcess.filter { inputArrayIndex in
                parentStringWords.contains(where: { $0 == inputArrayIndex }
                )}
            for eachWord in wordSearchArray
            {
                parentString.enumerateSubstrings(in: parentString.startIndex..

    Thank you. Happy Coding.

提交回复
热议问题