除[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
}
}
[这是您提供的代码的实现,请帮助它不会在我的控制器中工作,它将打印与标签上完全相同的文本,这是您提供的代码的实现,请帮助它无法在我的控制器中正常工作,它将打印与标签上完全相同的文本]
答案 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];