arrays - 如何加粗数组的第一个单词?
问题描述
var userComment = [“时间这些让我.jenny 是”,“我不能她做到了。”,“嘿!布拉德的戏剧真棒”,“我不能她。”,“时间就像 make是个坏蛋”,“我做不到。”,“她是个卑鄙的人选择的地方”,“时间我是个坏蛋”,“哇!我就像珍妮一样。我会拉屎”,“我不能做到了。”]
大写数组的第一个单词 ex [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
}
}
[这是你给出的代码的实现,请帮助它在我的控制器中不起作用它将打印与标签上完全相同的文本,这是你给出的代码的实现,请帮助它不会在我的控制器中工作,它将打印与标签上完全相同的文本]
解决方案
试试下面的代码
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 单元格标签中使用该数组
cellForRow 方法
lable.attributedText = attributeCommentArray[indexPath.row];
推荐阅读
- flutter - 如何修复“已关闭的 Dismissible 小部件仍然是树的一部分。” 颤振错误
- python - 解析 netParameter 文件失败
- c# - how to pass mxgraph generated xml to Action of controller?
- php - 如何知道PHP中异常来自的行号
- mysql - 什么是正确的查询语句?
- php - 如何在laravel中搜索2个日期的特定一条记录?
- animation - 使用 libwebp c api 创建动画,但只编码第一帧
- python-3.x - 如何在 python 中修复“IndentationError:预期有缩进块”
- java - 如何修复 MockitoExtension.class 未解决的错误
- drools - Optaplanner Drools 性能相当慢