gpt4 book ai didi

ios - 我想在与 api 数据对应的标签中将可用时间显示为粗体和不可用时间(罢工)

转载 作者:行者123 更新时间:2023-11-28 11:35:50 25 4
gpt4 key购买 nike

enter image description here

对于一天的 api 数据(所有日期和星期日关闭的不同时间)和日期,获取进入时间和离开时间,从中将标签更改为粗体可用时间并在不可用时间上敲击。

进入时间前所有时间不可用,离开后所有时间不可用,午餐时间也不可用

如何将 api 数据的条件写入标签更改(粗体/删除)

API 响应:

 Optional(<__NSSingleObjectArrayI 0x600003873050>(
{
day = Saturday;
dArray = "<null>";
enteringTime = "09.00 am";
entryTime = "<null>";
exitTime = "<null>";
id = 13;
leavingTime = "06.00 pm";
lunchtimeFrom = "13:00";
lunchtimeTo = "14:00";
}

代码片段:

let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: "08:05 AM") 

attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))

time805Lbl.attributedText = attributeString

最佳答案

在您的 viewController 中定义日期格式 -

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm a"
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)

现在,添加一个检查可用时间的方法

func checkAvailableTime(currentTime: String) -> NSMutableAttributedString {

let attributeString: NSMutableAttributedString = NSMutableAttributedString(string: currentTime)

var time = "\(times["lunchtimeFrom"]!)"
let lunchStartTime = dateFormatter.date(from: time)

time = "\(times["lunchtimeTo"]!)"
let lunchEndTime = dateFormatter.date(from: time)


let personCheckTime = dateFormatter.date(from: currentTime)


if (lunchStartTime!.compare(personCheckTime!) == .orderedAscending) && (lunchEndTime!.compare(personCheckTime!) == .orderedDescending){

attributeString.addAttribute(NSAttributedString.Key.strikethroughStyle, value: 2, range: NSMakeRange(0, attributeString.length))
}

return attributeString
}

是时候调用该方法了 -

time805Lbl.attributedText = checkAvailableTime(currentTime: time805Lbl.text!)

times 是您的响应时间。我希望你得到如下回应 -

let times = [
"day":"Saturday",
"dArray":"<null>",
"enteringTime":"09:00 AM",
"entryTime":"<null>",
"exitTime":"<null>",
"id": 13,
"leavingTime":"06:00 PM",
"lunchtimeFrom":"1:00 PM",
"lunchtimeTo":"2:00 PM"
] as [String : Any]

如果您还有任何问题,请告诉我。

关于ios - 我想在与 api 数据对应的标签中将可用时间显示为粗体和不可用时间(罢工),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55448877/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com