我是iOS开发的新手,现在正在开发一个接收某种JSON数据的应用程序.但是一些后端专家认为,如果用户只是直接从Word中复制信息并将其粘贴到信息系统中,对用户来说会更好.所以我坐在这里,尝试在UITableView中创建一个可点击的链接.
我从Web解析数据并获得具有以下格式的String:
Für mehr Informationen klicken sie <a href="http://www.samplelink.com/subpage.php?id=8">here</a>.
Run Code Online (Sandbox Code Playgroud)
我已经尝试了UILabel,但经过一些研究后我现在使用经常建议的UITextView.在Attributed Inspector中,我将其设置为属性文本并启用链接检测.现在文本显示为红色且可单击.
现在的问题是,HTML标签和正确的(德国)字符集仍然缺失,我不知道如何以正确的方式显示它.
显示的字符串以这种方式解析:
func showHTMLString(unformattedString: String) -> NSAttributedString{
var attrStr = NSAttributedString(
data: tmpString.dataUsingEncoding(NSUnicodeStringEncoding, allowLossyConversion: true)!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil,
error: nil)
return attrStr!
}
Run Code Online (Sandbox Code Playgroud)
如果我填写Textview,attrStr?.string格式以正确的方式显示,但链接也消失了.
有什么建议如何以正确的方式格式化显示的字符串?
在此先感谢AR4G4
我已经为这两天苦苦挣扎了,所以我和互联网的聪明人一起参与其中.
我正在展示一篇文章作为我的一部分UITableView.为了正确显示,我需要给代表一个单元格的高度,我希望它与UIWebView高度相同,所以我可以在WebView上禁用滚动并将整个Web内容显示为静态细胞.
我的第一种方法是在heightForRowAtIndexpath方法中渲染它,但这显然不起作用,因为我需要等待UIWebViewDelegate告诉我何时Web视图完全加载并具有高度.过了一会儿,我发现了一个工作解决方案,它使用Web视图委托在加载Web视图时刷新单元格高度.
工作正常,直到屏幕大小改变.无论是旋转还是全屏我的UISplitView.我强制对它进行更新didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation),但是这会使它闪烁大约10次然后才能进入正确的高度.我记录了这个更改,似乎WebView多次调用自身,导致循环.
曾经出现在这个日志,从我旋转屏幕开始.
每次重新加载时它会闪烁一次,正如您所看到的,它会自动重新加载一次.
所以.我需要一种在uitableview中显示整个Web视图内容的方法,并在屏幕大小更改时可靠地获取高度.如果有人以前以任何方式管理过这个,请告诉我.我会给任何可以解决这个问题的人一个赏金和我的长子,因为它让我疯了.
这是我的相关代码.
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
switch indexPath.row {
case 4:
//Content
print("Height for row called")
return CGFloat(webViewHeight)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
switch (indexPath.row){
//HTML Content View
case 4:
let cell = tableView.dequeueReusableCellWithIdentifier("ContentCell", forIndexPath: indexPath)
var contentCell = cell as? ContentCell
if contentCell == nil {
contentCell = …Run Code Online (Sandbox Code Playgroud) 我UITextView通过以下方式显示html :
[self.textView setValue:@"<b>Content</b>" forKey:@"contentToHTMLString"];,
编辑内容后UITextView,我想获取内容包括html所以我使用:
[self.textView valueForKey:@"contentToHTMLString"]; 但应用程序崩溃,如何解决?
在iOS 8(或甚至可能在iOS 7/7.1中)NSAttributedStringiOS上添加了渲染文本表的功能.此API 在OS X上公开,但在iOS 上不公开.但是,可以通过将包含表的HTML传递给iOS,在iOS上创建包含文本表的属性字符串-[NSAttributedString initWithData:options:documentAttributes:error:].
正确创建了属性字符串,但是当我尝试使用a渲染字符串时UITextView,会抛出此异常:
<NSATSTypesetter: 0x7f8900faec90>: Exception *** -[NSConcreteTextStorage attribute:atIndex:effectiveRange:]: Range or index out of bounds raised during typesetting layout manager <NSLayoutManager: 0x7f8902e13440>
1 containers, text backing has 1884 characters
Currently holding 1884 glyphs.
Glyph tree contents: 1884 characters, 1884 glyphs, 1 nodes, 64 node bytes, 7680 storage bytes, 7744 total bytes, 4.11 bytes per character, 4.11 bytes per glyph
Layout tree contents: 1884 …Run Code Online (Sandbox Code Playgroud) 我想使用 swift 在 UITextView 控件中显示字符串 HTML。我找到了一些代码并尝试了一下:
do {
let str = try NSAttributedString(data: htmlString.data(using: String.Encoding.unicode, allowLossyConversion: true)!, options: [NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType], documentAttributes: nil)
} catch {
print(error)
}
Run Code Online (Sandbox Code Playgroud)
但我给出了一些我无法理解或找到任何解决方案的错误,其中指出,
无法将“NSAttributedString.DocumentAttributeKey”类型的值转换为预期的字典键类型“NSAttributedString.DocumentReadingOptionKey”
我需要弄清楚为什么我会遇到这个错误。任何解决方案或替代代码?其他解决方案在objective-c中,如果他们有答案,他们会给出同样的错误,我试过很多只是因为旧版本。
使用UISearchBar在UITableView中搜索文本.我只是想知道如何突出搜索文本.突出显示可以使文本变粗或更改文本颜色.
例如,如果我在UITableView中使用UISearchBar搜索文本"Stack",其中包含一个字符串为"Stackoverflow"的行,那么现在我的搜索结果应该以" Stack overflow"的方式出现.
在此先感谢..快乐编码..
ios ×5
swift ×3
uitextview ×3
objective-c ×2
autolayout ×1
format ×1
hyperlink ×1
ipad ×1
iphone ×1
uitableview ×1
uiwebview ×1