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

// 搜索

parent 29e12ba0
...@@ -233,6 +233,7 @@ extension YHSearchInfomationVC: UITableViewDelegate, UITableViewDataSource { ...@@ -233,6 +233,7 @@ extension YHSearchInfomationVC: UITableViewDelegate, UITableViewDataSource {
if 0 <= indexPath.section && indexPath.section < items.count { if 0 <= indexPath.section && indexPath.section < items.count {
let item = items[indexPath.section] let item = items[indexPath.section]
cell.updateModel(item) cell.updateModel(item)
cell.renderHighLightText(self.searchBar.textField.text ?? "")
} }
return cell return cell
} }
......
...@@ -12,7 +12,7 @@ import Kingfisher ...@@ -12,7 +12,7 @@ import Kingfisher
class YHSearchInfoCell: UITableViewCell { class YHSearchInfoCell: UITableViewCell {
static let cellReuseIdentifier = "YHSearchInfoCell" static let cellReuseIdentifier = "YHSearchInfoCell"
var model: YHHomeListModel = YHHomeListModel()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier) super.init(style: style, reuseIdentifier: reuseIdentifier)
setupUI() setupUI()
...@@ -48,6 +48,7 @@ class YHSearchInfoCell: UITableViewCell { ...@@ -48,6 +48,7 @@ class YHSearchInfoCell: UITableViewCell {
}() }()
func updateModel(_ model: YHHomeListModel) { func updateModel(_ model: YHHomeListModel) {
self.model = model
titleLabel.text = model.title titleLabel.text = model.title
videoPlayImgView.isHidden = (model.type != 1) videoPlayImgView.isHidden = (model.type != 1)
if let url = URL(string: model.img_url) { if let url = URL(string: model.img_url) {
...@@ -80,24 +81,38 @@ class YHSearchInfoCell: UITableViewCell { ...@@ -80,24 +81,38 @@ class YHSearchInfoCell: UITableViewCell {
} }
} }
func highlightText(in text: String, highlightString: String, highlightColor: UIColor) -> NSAttributedString { func renderHighLightText(_ text: String) {
let attributedString = NSMutableAttributedString(string: text)
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] = [ let highlightAttributes: [NSAttributedString.Key: Any] = [
.backgroundColor: highlightColor .foregroundColor: UIColor(hex: 0x3570DC)
] ]
// 查找并应用高亮样式到指定文字 let ranges = findOccurrencesOfString(text, inString:self.model.title)
let range = (text as NSString).range(of: highlightString) for range in ranges {
attributedString.addAttributes(highlightAttributes, range: range) let changeRange = NSRange(range, in: self.model.title)
attributedString.addAttributes(highlightAttributes, range: changeRange)
return attributedString }
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 { ...@@ -207,7 +207,7 @@ extension YHHomePageViewModel {
"page_size": 10, "page_size": 10,
"title": title] "title": title]
let strUrl = YHBaseUrlManager.shared.curURL() + YHAllApiName.Common.article 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 } guard let self = self else { return }
//1. json字符串 转 对象 //1. json字符串 转 对象
if json.code == 200 { 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