我想在对应于 api 数据的标签中将可用时间显示为粗体和不可用时间(罢工)
i want to show the avalible times as bold and unavaliable times(strike) in label with corresponding to api data
对于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]
如果您还有任何问题,请告诉我。
对于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]
如果您还有任何问题,请告诉我。