-12

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."]

first word of array in capital ex [TIME,I,HEY,WOW] other is same as written

    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
          }
          }

[This is the implementation of code which is given by you please help it will not working in my controller it will print exactly the same text which is on the label,This is the implementation of code which is given by you please help it will not working in my controller it will print exactly the same text which is on the label]

  • So, have you tried something so far? Please provide it. – Ahmad F Mar 04 '19 at 10:48
  • feeling help less – Rahul Mishra Mar 04 '19 at 10:49
  • i don't understand why user give me minus reputation if they have gut then give the answer not reputation i don't want any reputation – Rahul Mishra Mar 04 '19 at 10:50
  • Refer this : https://makeapppie.com/2014/10/20/swift-swift-using-attributed-strings-in-swift/ – Nikunj Rajyaguru Mar 04 '19 at 10:51
  • So you could start looking at: https://stackoverflow.com/questions/24666515/how-do-i-make-an-attributed-string-using-swift https://stackoverflow.com/questions/36486761/make-part-of-a-uilabel-bold-in-swift/36486949 https://stackoverflow.com/questions/35331812/make-first-letter-bold-ios-swift – Ahmad F Mar 04 '19 at 10:52
  • i already see this but i didn't find the right answer it is only for one string but i have an array and different words in the array i have to show different data on label – Rahul Mishra Mar 04 '19 at 10:55
  • I think you have 2 question here. 1. How to get first word from each string in array. (If that's the case update your question) 2. How to make them bold. For 2nd refer link shared by @AhmadF – Rizwan Mar 04 '19 at 10:56
  • i want to show whole data in label which is in the tableview and first word is bold and other is same as shown in array – Rahul Mishra Mar 04 '19 at 10:57
  • if you know the solution for one string, you can iterate your string array with the same code, then append it to new array. – Saurabh Mar 04 '19 at 11:00
  • one string means "this is the string" THIS is get bold easily if there is only one string but stuck in array – Rahul Mishra Mar 04 '19 at 11:06
  • https://stackoverflow.com/questions/36486761/make-part-of-a-uilabel-bold-in-swift?noredirect=1&lq=1 – AfnanAhmad Mar 04 '19 at 11:15

1 Answers1

2

Try the following code

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))
    }
}

Create Attrinbuted String array and use that array in uitableview cell label

cellForRowMethod

lable.attributedText = attributeCommentArray[indexPath.row];
Rizwan
  • 3,324
  • 3
  • 17
  • 38
Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44