2015-04-03 19 views
0

我有一個使用HTML製作的NSAttributedString。NSAttributedString在文本視圖中不顯示NSLink

NSAttributedString *decodedString = [[NSAttributedString alloc] initWithData:[encodedString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil]; 
self.story.attributedText = decodedString; 

當我運行代碼和查看的TextView(self.story)所有從HTML文本和圖像顯示除了超鏈接(HTTP)。

它們佔據的空間是存在的,但聯繫不

例如:超鏈接是________________應該在那裏。 只是沒有____它的空白。

控制檯顯示的NSAttributedString顯示

{ 
    NSColor = "UIDeviceRGBColorSpace 0 0 0.933333 1"; 
    NSFont = "<UICTFont: 0x15654c230> font-family: \"Times New Roman\"; font-weight: normal; font-style: normal; font-size: 12.00pt"; 
    NSKern = 0; 
    NSLink = "https://www.kickstarter.com/projects/1425492550/sesame-your-key-reinvented"; 
    NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 12, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (null), Lists (null), BaseWritingDirection 0, HyphenationFactor 0, TighteningFactor 0, HeaderLevel 0"; 
    NSStrokeColor = "UIDeviceRGBColorSpace 0 0 0.933333 1"; 
    NSStrokeWidth = 0; 
    NSUnderline = 1; 
} 

但 「https://www.kickstarter.com/projects/1425492550/sesame-your-key-reinvented」 是無處可尋。

任何有關爲什麼會發生這種情況的想法以及我如何解決這個問題?

回答

0

我在回答我自己的問題。

鏈接顯示爲白色(在白色背景上),並且它們不可點擊,因此看起來好像它們不在那裏。

我用這段代碼修復了它。

twocell.story.linkTextAttributes = @{NSForegroundColorAttributeName:[UIColor darkGrayColor]}; 
     NSMutableAttributedString *res = [twocell.story.attributedText mutableCopy]; 
     [res beginEditing]; 
     [res enumerateAttribute:NSUnderlineStyleAttributeName inRange:NSMakeRange(0,res.length) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) { 

      if (value){ 
       [res addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:range]; 
       [res endEditing]; 
       [twocell.story setAttributedText:res]; 
      } 
     }]; 

將顏色更改爲深灰色並添加下劃線。

相關問題