Deadpikle / iOS-Rich-Text-Editor

A Rich Text Editor library for iOS
Other
14 stars 10 forks source link

Editing text increases font size #13

Open TheGeekPharaoh opened 5 years ago

TheGeekPharaoh commented 5 years ago

I've run into an issue where if i create HTML text, save it (somewhere), then use RichTextEditor to edit the same text, the font size is increased. Example...

  1. The original text I enter is "This is my basic entry", with the font set to Arial 12.0. The result:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <meta http-equiv="Content-Style-Type" content="text/css">
      <title></title>
      <meta name="Generator" content="Cocoa HTML Writer">
      <style type="text/css">np.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Arial}nspan.s1 {font-family: 'Arial'; font-weight: normal; font-style: normal; font-size: 12.00pt}</style>
   </head>
   <body>
      <p class="p1"><span class="s1">This is my basic entry<span class="Apple-converted-space"> </span></span></p>
   </body>
</html>
  1. I save this text off, then go to edit it by calling richTextEditor?.setHtmlString()

  2. Upon saving, my font size is automatically increased (with no interaction from me) to Arial 16.0. Result:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <meta http-equiv="Content-Style-Type" content="text/css">
      <title></title>
      <meta name="Generator" content="Cocoa HTML Writer">
      <style type="text/css">np.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 20.0px; font: 16.0px Arial; color: #000000; -webkit-text-stroke: #000000}nspan.s1 {font-family: 'Arial'; font-weight: normal; font-style: normal; font-size: 16.00pt; font-kerning: none}</style>
   </head>
   <body>
      <p class="p1"><span class="s1">This is my basic entry<span class="Apple-converted-space"> </span></span></p>
   </body>
</html>
  1. If I repeat this edit on the text from (3), the font size is again increased to Arial 21.3. Result:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <meta http-equiv="Content-Style-Type" content="text/css">
      <title></title>
      <meta name="Generator" content="Cocoa HTML Writer">
      <style type="text/css">np.p1 {margin: 0.0px 0.0px 0.0px 0.0px; line-height: 26.0px; font: 21.3px Arial; color: #000000; -webkit-text-stroke: #000000}nspan.s1 {font-family: 'Arial'; font-weight: normal; font-style: normal; font-size: 21.33pt; font-kerning: none}</style>
   </head>
   <body>
      <p class="p1"><span class="s1">This is my basic entry<span class="Apple-converted-space"> </span></span></p>
   </body>
</html>

Please advise. Thanks!

Deadpikle commented 5 years ago

Greetings!

How are you saving the text? What function(s) are you calling to convert the NSAttributedString to HTML? Any chance you could plop your functions here for load and save?

TheGeekPharaoh commented 5 years ago

The load and save is just a simple CoreData operation.  I obtain the HTML string by calling richTextEditor?.htmlString() and save that returned value.  I do not manipulate it at all.  When I want to later edit the text, I call richTextEditor?.setHtmlString() and pass it the same value.  Again, I do not manipulate it at all.  If I call richTextEditor?.htmlString() again, I see that the font size has increased, even if I do not manipulate the text. On Wednesday, April 24, 2019, 8:51:35 PM EDT, Deadpikle notifications@github.com wrote:

Greetings!

How are you saving the text? What function(s) are you calling to convert the NSAttributedString to HTML? Any chance you could plop your functions here for load and save?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

Deadpikle commented 5 years ago

Hi again,

Sorry for your wait.

I am able to replicate the issue you're facing; however, I don't think it's unique to this RTE. All the code for loading and saving is calling native iOS stuff...hhmmmm....

The best I can figure out right now is that the HTML for px is being converted to pt (times 96/72) -- 12x96/72 = 16, which is what you're seeing. So, both the saving and loading appear to be messing up somehow.

It looks as though someone has had this issue before. Please see: https://stackoverflow.com/q/28441486/3938401 -- thus, it looks like this is an iOS/Apple issue. 😢

The reason I never ran into this myself is that I use DTCoreText to do all my HTML loading and saving (on both iOS and Mac). I recall usually using DTUseiOS6Attributes when loading/saving HTML, but I don't know if that's needed on newer versions of iOS.

TheGeekPharaoh commented 5 years ago

Thanks for your quick response! I'll be sure to check out DTCoreText. Any examples or advice for making use of it?

TheGeekPharaoh commented 5 years ago

For what it's worth, I took a stab at incorporating the answer from the Stackoverflow link you provided and it worked like a charm! I simply overrode the [RichTextEditor attributedStringFromHTMLString] method with the answer. I can try and clean it up a bit and submit a pull request for you.

Deadpikle commented 5 years ago

Hi, I'm sorry for my lack of response. I'm not dead, just very busy. 😅

I'm glad the SO link answer worked!! Very good to hear. Will keep this issue open for visibility for future users.

I'm open to PR's, sure. If you do one, please make the behavior opt-out so that folks can turn it off as they have need. Thanks!