如何加粗数组的第一个单词?

时间:2019-03-04 10:44:41

标签: arrays swift

var userComment = [“这些时间让我。珍妮是“,”我不能做到。”,“嘿!布拉德的出色表现”,“我不能。”,“像make的时间是个坏蛋”,“我做不到”,“她是个卑鄙的选择”,“给我一个坏蛋的时间”,“哇!我就像珍妮。我会屎”,“我做不到。“]

除[TIME,I,HEY,WOW]以外的其他大写字母的第一个单词与写的相同

    var attributeCommentArray:[NSAttributedString] = []
    override func viewDidLoad()  {
        super.viewDidLoad()
        for comment in userComment {
            if comment.contains("") {
                let firstCharacter = comment.components(separatedBy: "").first ?? ""
                let myString:NSMutableAttributedString = NSMutableAttributedString.init(string:comment)
                myString.addAttribute(NSAttributedString.Key.font,
                                      value: UIFont(
                                        name: "HelveticaNeue-Bold",
                                        size: 18.0)!,
                                      range: NSRange(
                                        location:0,
                                        length:firstCharacter.count))
                attributeCommentArray.append(myString)
            } else   {
                attributeCommentArray.append(NSMutableAttributedString.init(string:comment))
            }
            }
     //  self.navTitleWithImageAndText(titleText: "oneTwoThree", imageName: "")
        self.navigationController?.navigationBar.isHidden = false
     //   chatView.makeCornerRadius(self.chatView.layer.bounds.height / 2)
        chatView.layer.borderWidth  =  1
        chatView.setCorner(borderWidth: 1, borderColor: UIColor.darkGray.cgColor, cornerRadius: 25, clip: true)
        self.tblView.rowHeight = UITableView.automaticDimension
        self.tblView.estimatedRowHeight = 60
        tblView.delegate = self
        tblView.dataSource = self
            self.loadXib()
                        }
    private  func loadXib()  {
         tblView.loadXibForCellResuse(LiveCell.identifier)
                      }
                      }
extension LiveChatVC:UITableViewDelegate,UITableViewDataSource          {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      return self.userName.count
           }
  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell  {
  let cell = tblView.dequeueReusableCell(withIdentifier: "LiveCell", for: indexPath) as! LiveCell
  //  cell.userName.text = self.userComment[indexPath.row]
   // cell.userName.attributedText = myMutableString
    cell.userName.attributedText = attributeCommentArray[indexPath.row]
            return cell
          }
          }

[这是您提供的代码的实现,请帮助它不会在我的控制器中工作,它将打印与标签上完全相同的文本,这是您提供的代码的实现,请帮助它无法在我的控制器中正常工作,它将打印与标签上完全相同的文本]

1 个答案:

答案 0 :(得分:2)

尝试以下代码

var userComment = ["Time these make me.jenny is ","I can't she did it.", "Hey! what a great play made by brad", "I can't she .", "Time like make is a badass", "I can't it.", "She is a mean chose to place","Time me a badass", "Wow! I am just like jenny.I would shit", "I can't did it."]


var attributeCommentArray:[NSAttributedString] = []

for comment in userComment
{
    if comment.contains(" ")
    {
        let firstWord = comment.components(separatedBy: " ").first ?? ""
        let myString:NSMutableAttributedString = NSMutableAttributedString.init(string: comment)

        myString.addAttribute(NSAttributedString.Key.font,
                                     value: UIFont(
                                        name: "HelveticaNeue-Bold",
                                        size: 18.0)!,
                                     range: NSRange(
                                        location:0,
                                        length:firstWord.count))


        attributeCommentArray.append(myString)
    }
    else
    {
        attributeCommentArray.append(NSAttributedString.init(string: comment))
    }
}

创建Attrinbuted String数组并在uitableview单元格标签中使用该数组

cellForRowMethod

lable.attributedText = attributeCommentArray[indexPath.row];