Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

针对单个 <p> 标签处理的问题 #16

Closed
caocanx opened this issue Mar 14, 2023 · 3 comments
Closed

针对单个 <p> 标签处理的问题 #16

caocanx opened this issue Mar 14, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@caocanx
Copy link

caocanx commented Mar 14, 2023

最近在学习 SwiftUI,尝试开发一个 Hacker News Reader,通过官方 API 返回的 HTML String 里面的 <p> 标签都是独立的,举例:

{
  "by": "joak",
  "id": 34934428,
  "kids": [
    34934473
  ],
  "parent": 34934270,
  "text": "Henry Kissinger will be 100 years old in few weeks. And he is still around giving advice. Amazing.<p>But I don&#x27;t see why his opinion could be relevant on the topic...",
  "time": 1677306778,
  "type": "comment"
}

调用 parser.render(text) 后,会产生空行,如图所示:

CleanShot 2023-03-14 at 15 07 47@2x

我的预期是通过控制 paragraphSpacing 来调整段间距,从而让段落之间有一定的空间。上图中 paragraphSpacing 的值 是 8,但由于中间有空行,让段落间的空隙显得很大。

@zhgchgli0718
Copy link
Member

Hi,

  • HTML 使用中出現 <p> 的情況比較少見,通成會成對出現
  • <p> 在 HTML 代表段落 會在前後加上換行
  • 在 ZMarkupParser 中如果標籤沒有正確關閉 <p></p> or <p/> 會自動解讀成 <p/>
  • 換行在 NSAttributedString 要 insert/append \n 到字串,而非 NSParagrahstyle 可控制

回到問題,原文在 ZMarkupParser Render 後會解讀成:

Henry Kissinger will be 100 years old in few weeks. And he is still around giving advice. Amazing.<p/>But I don&#x27;t see why his opinion could be relevant on the topic...

Render 過後應該要是:

Henry Kissinger will be 100 years old in few weeks. And he is still around giving advice. Amazing.\nBut I don&#x27;t see why his opinion could be relevant on the topic...

圖上結果多了一個 \n,變成 \n\n,因為當初沒考慮到 <p> 可能單獨出現,因此在 mapping p 時在前後都加上了 \n
在成對的狀況下是正確的:

test<p>1231</p>test

->

test
1231
test

但在單獨的情況會多一個 \n 變成:

test
1231

test

會儘速修正,感謝回報。


另外,行距空間可透過設置 root style 控制:

let parser = ZHTMLParserBuilder.initWithDefault().set(rootStyle: MarkupStyle(paragraphStyle: .init(lineSpacing: 8))).build()

要注意的是,預設是 0,設置 8 是加大的意思。

by the way, 目前 ZMarkupParser 並不能很好是配 SwiftUI 的 AttributedString,實際上 ZMarkupParser 應該多實現直接 Convert HTML To AttributedString,而不是轉換成 NSAttributedString 放到 AttributedString 中再給 SwiftUI 使用。

ref: #12

@zhgchgli0718 zhgchgli0718 added bug Something isn't working ASAP and removed ASAP labels Mar 14, 2023
@caocanx
Copy link
Author

caocanx commented Mar 14, 2023

感谢回复,关于你回复中提到的这一段:

by the way, 目前 ZMarkupParser 並不能很好是配 SwiftUI 的 AttributedString,實際上 ZMarkupParser 應該多實現直接 Convert HTML To AttributedString,而不是轉換成 NSAttributedString 放到 AttributedString 中再給 SwiftUI 使用。

我其实是在用 UILabelView,因为 SwiftUI 的 Text 对于各种文本样式的支持还比较欠缺,所以 ZMarkupParser 真的很有帮助!

@zhgchgli0718
Copy link
Member

感谢回复,关于你回复中提到的这一段:

by the way, 目前 ZMarkupParser 並不能很好是配 SwiftUI 的 AttributedString,實際上 ZMarkupParser 應該多實現直接 Convert HTML To AttributedString,而不是轉換成 NSAttributedString 放到 AttributedString 中再給 SwiftUI 使用。

我其实是在用 UILabelView,因为 SwiftUI 的 Text 对于各种文本样式的支持还比较欠缺,所以 ZMarkupParser 真的很有帮助!

了解,感謝補充;我對 SwiftUI 不熟😅

@zhgchgli0718 zhgchgli0718 modified the milestone: 2023Q2 Mar 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants