Commit 8e864f81 authored by Steven杜宇's avatar Steven杜宇

// 搜索

parent 29e12ba0
......@@ -233,6 +233,7 @@ extension YHSearchInfomationVC: UITableViewDelegate, UITableViewDataSource {
if 0 <= indexPath.section && indexPath.section < items.count {
let item = items[indexPath.section]
cell.updateModel(item)
cell.renderHighLightText(self.searchBar.textField.text ?? "")
}
return cell
}
......
......@@ -12,7 +12,7 @@ import Kingfisher
class YHSearchInfoCell: UITableViewCell {
static let cellReuseIdentifier = "YHSearchInfoCell"
var model: YHHomeListModel = YHHomeListModel()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupUI()
......@@ -48,6 +48,7 @@ class YHSearchInfoCell: UITableViewCell {
}()
func updateModel(_ model: YHHomeListModel) {
self.model = model
titleLabel.text = model.title
videoPlayImgView.isHidden = (model.type != 1)
if let url = URL(string: model.img_url) {
......@@ -80,24 +81,38 @@ class YHSearchInfoCell: UITableViewCell {
}
}
func highlightText(in text: String, highlightString: String, highlightColor: UIColor) -> NSAttributedString {
let attributedString = NSMutableAttributedString(string: text)
func renderHighLightText(_ text: String) {
var attributedString = NSMutableAttributedString(string:self.model.title, attributes: [
.foregroundColor: UIColor(hex: 0x121A26)
])
if isEmptyString(text) {
self.titleLabel.attributedText = attributedString
return
}
// 创建一个属性字典,用于设置高亮样式
// 设置高亮样式
let highlightAttributes: [NSAttributedString.Key: Any] = [
.backgroundColor: highlightColor
.foregroundColor: UIColor(hex: 0x3570DC)
]
// 查找并应用高亮样式到指定文字
let range = (text as NSString).range(of: highlightString)
attributedString.addAttributes(highlightAttributes, range: range)
return attributedString
let ranges = findOccurrencesOfString(text, inString:self.model.title)
for range in ranges {
let changeRange = NSRange(range, in: self.model.title)
attributedString.addAttributes(highlightAttributes, range: changeRange)
}
self.titleLabel.attributedText = attributedString
}
func hopeless() {
func findOccurrencesOfString(_ searchString: String, inString string: String) -> [Range<String.Index>] {
var occurrences: [Range<String.Index>] = []
var searchStartIndex = string.startIndex
while let range = string.range(of: searchString, options: [], range: searchStartIndex..<string.endIndex) {
occurrences.append(range)
searchStartIndex = range.upperBound
}
return occurrences
}
}
......@@ -207,7 +207,7 @@ extension YHHomePageViewModel {
"page_size": 10,
"title": title]
let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Common.article
let _ = YHNetRequest.getRequest(url: strUrl) { [weak self] json, code in
let _ = YHNetRequest.getRequest(url: strUrl, params: params) { [weak self] json, code in
guard let self = self else { return }
//1. json字符串 转 对象
if json.code == 200 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment