如何防止iOS在Swift中自动在数字下面划线?
我正在构建一个附带项目,以配合iOS开发,并决定将其作为一个消息传递应用程序。我将时间戳添加到消息体下方的消息中,但仍在气泡内,因为我喜欢它看起来比气泡外更好。iOS会自动设置这些数字的格式,以便包含在日历中。有没有办法避免这些数字的格式?我想在用户输入时间和日期时保存它,因为这非常有用 下面是添加消息正文的块,以及我所指内容的屏幕截图如何防止iOS在Swift中自动在数字下面划线?,ios,swift,Ios,Swift,我正在构建一个附带项目,以配合iOS开发,并决定将其作为一个消息传递应用程序。我将时间戳添加到消息体下方的消息中,但仍在气泡内,因为我喜欢它看起来比气泡外更好。iOS会自动设置这些数字的格式,以便包含在日历中。有没有办法避免这些数字的格式?我想在用户输入时间和日期时保存它,因为这非常有用 下面是添加消息正文的块,以及我所指内容的屏幕截图 override func didPressSendButton(button: UIButton!, withMessageText text: String
override func didPressSendButton(button: UIButton!, withMessageText text: String!, senderId: String!, senderDisplayName: String!, date: NSDate!) {
JSQSystemSoundPlayer.jsq_playMessageSentSound()
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(.CalendarUnitHour | .CalendarUnitMinute, fromDate: date)
let hour = components.hour
var minutes = components.minute
if minutes < 10 {
var minutes = String(minutes)
minutes = String(format: "%02d", minutes)
}
var newText = text + "\n\n \(hour):\(minutes)"
var newMessage = JSQMessage(senderId: senderId, displayName: senderDisplayName, text: newText);
messages += [newMessage]
self.finishSendingMessage()
}
override func didPressSendButton(按钮:UIButton!,withMessageText:String!,senderId:String!,senderDisplayName:String!,日期:NSDate!){
JSQSystemSoundPlayer.jsq_playmesssentsound()
let date=NSDate()
让calendar=NSCalendar.currentCalendar()
让组件=calendar.components(.CalendarUnitHour |.CalendarUnitMinute,fromDate:date)
设hour=components.hour
var minutes=components.minute
如果分钟<10{
var minutes=字符串(分钟)
分钟=字符串(格式:“%02d”,分钟)
}
var newText=text+“\n\n\(小时):\(分钟)”
var newMessage=JSQMessage(senderId:senderId,displayName:senderDisplayName,text:newText);
消息+=[新消息]
self.finishSendingMessage()
}
您启用了dataDetector,因此它可以检测URL、电话号码、日期等,并将它们转换为链接 根据需要禁用所有或特定数据检测 在您的特定情况下,听起来您希望禁用
UIDataDetectorTypeCalendarEvent
数据检测
更新:
这里有一个可能的答案。设置文本格式,使其看起来不是时间。例如,可能会对newText
冒号使用unicode字符,数据检测器不会将其作为时间捕获。另一种选择是使用零宽度空格以“不可见”字符分隔小时
var newText = text + "\n\n \(hour)\u{200b}:\(minutes)" // zero-width space
更新2:
我下载了JSQMessage,并在示例代码中尝试了这一点。冒号前的零宽度空格似乎可以避免检测到时间
[[JSQMessage alloc] initWithSenderId:kJSQDemoAvatarIdSquires
senderDisplayName:kJSQDemoAvatarDisplayNameSquires
date:[NSDate distantPast]
text:@"It even has data detectors. You can call me tonight. My cell number is 123-456-7890. My website is www.hexedbits.com.\n\n9\u200b:41"],
在创建单元格的JSQMessagesViewController中,数据检测器类型设置为all
您只需在Swift 3中将其设置为。:即可禁用所有检测
cell.textView?.dataDetectorTypes = []
这将禁用对整个文本的数据检测,而这并不是他们想要的:“有没有一种方法可以避免这些数字的格式设置?我想保留它,以备用户输入时间和日期时使用,因为这非常有用。”问题是数据检测不基于范围,因此,苹果的功能在整个(属性化的)字符串中不是开启就是关闭。OP可以尝试打开一个JSQMessage问题,看看维护人员是否知道任何方法。我没有查看源代码,但我检查了JSQMessage文档,它似乎也不支持基于范围的数据检测。@Noah,但你给了我一个解决方法的想法。。。我已经更新了答案。不幸的是,更新后的答案不起作用。iOS仍然检测时间并格式化它。好主意@traviswingo我下载了JSQMessage项目,并在演示中更改了一行。它似乎在iOS 8.4上工作。这个演示是用Objective C编写的,但是这个概念在Swift中仍然有效。您可能需要检查unicode语法,因为我没有掌握Swift的最新速度;您能否做到这一点可能取决于该库的可定制性。你能提供一个链接吗?这是iMessage应用程序的屏幕截图还是你自己的UI?你函数的第2-11行应该用
NSDateFormatter
替换。你解决了吗。这在短期内有效,但从长期来看,它还消除了用户输入时间和日期并让iOS自动检测的能力。我只想对这个文本字符串禁用它。可能吗?
cell.textView?.dataDetectorTypes = []