raspu / Highlightr

iOS & OSX Syntax Highlighter.
MIT License
1.67k stars 262 forks source link

The UITextField can't show the whole code file #32

Open Desgard opened 6 years ago

Desgard commented 6 years ago

In your demo, the Maxima file:

/* Maxima computer algebra system */

/* symbolic constants */

[true, false, unknown, inf, minf, ind,
 und, %e, %i, %pi, %phi, %gamma];

/* programming keywords */

if a then b elseif c then d else f;
for x:1 thru 10 step 2 do print(x);
for z:-2 while z < 0 do print(z);
for m:0 unless m > 10 do print(m);
for x in [1, 2, 3] do print(x);
foo and bar or not baz;

/* built-in variables */

[_, __, %, %%, linel, simp, dispflag,
 stringdisp, lispdisp, %edispflag];

/* built-in functions */

[sin, cosh, exp, atan2, sqrt, log, struve_h,
 sublist_indices, read_array];

/* user-defined symbols */

[foo, ?bar, baz%, quux_mumble_blurf];

/* symbols using Unicode characters */

[Љ, Щ, щ, Ӄ, ЩЩЩ, ӃӃЉЉщ];

/* numbers */

ibase : 18 $
[0, 1234, 1234., 0abcdefgh];
reset (ibase) $
[.54321, 3.21e+0, 12.34B56];

/* strings */

s1 : "\"now\" is";
s2 : "the 'time' for all good men";
print (s1, s2, "to come to the aid",
  "of their country");

/* expressions */

foo (x, y, z) :=
  if x > 1 + y
    then z - x*y
  elseif y <= 100!
    then x/(y + z)^2
  else z - y . x . y;

But in the demo, your UITextField can't show all the letters:

img_57f9ac6633c7-1

raspu commented 6 years ago

Wierd, I'll take a look at it, but must probably is an issue in the demo, not in the library.

Desgard commented 6 years ago

@raspu I kill the app, and restart. It go back to normal. 🤣 I will try to find the problem, again.....

raspu commented 6 years ago

Lol, let me know if you can reproduce it again.

Desgard commented 6 years ago

It's a very very strange question.

In the demo, if you choose the device to iPhone 8 and iPhone X in .xib file, the code will be less than the origin code.

WTF!!! The bad apple.....😓

JosephShenton commented 6 years ago

I have this issue as well

JosephShenton commented 6 years ago

I fixed it with the following code

let layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)

var file = UserDefaults.standard.string(forKey: "currentFile")

let token = file?.components(separatedBy: "Documents/")

file = token?[1]

if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {

   let fileURL = dir.appendingPathComponent(file!)

   //reading
   do {
       code = try String(contentsOf: fileURL, encoding: .utf8)
   }
   catch { NSLog("some error")}
}

let textContainer = NSTextContainer(size: code.height(withConstrainedWidth: viewPlaceholder.frame.size.width, font: UIFont.systemFont(ofSize: 35.0)))
layoutManager.addTextContainer(textContainer)

extension String {
    func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGSize {
        let constraintRect = CGSize(width: width, height: .greatestFiniteMagnitude)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [.font : font], context: nil)

        return boundingBox.size
    }

    func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat {
        let constraintRect = CGSize(width: .greatestFiniteMagnitude, height: height)
        let boundingBox = self.boundingRect(with: constraintRect, options: .usesLineFragmentOrigin, attributes: [.font : font], context: nil)

        return ceil(boundingBox.width)
    }
}
JosephShenton commented 6 years ago

You can see it used and working in this app

Phantom HTTP by JJS Digital Pty Ltd https://itunes.apple.com/au/app/phantom-http/id1335468125?mt=8

raspu commented 6 years ago

Thank you @JosephShenton I will try to fix it later today using your code.

Desgard commented 6 years ago

@JosephShenton I use your code to solve this problem. And I found your way is to change the textContainer's height dynamically.
And now, I wanted to achieve an effect, which needs to keep one line code to show in one line (not wrap automatically). And by your way, I calculate the Text Height - (Font Size + Line Interval) * Lines, but it still wrapped automatically..😭 So, is there any ideas? Thx.

JosephShenton commented 6 years ago

Disable text wrapping on the textview maybe?

cntrump commented 6 years ago

tested https://github.com/raspu/Highlightr/pull/52

the key: let textContainer = NSTextContainer() also iOS demo

steve-taylor commented 5 years ago

Disable text wrapping on the textview maybe?

@JosephShenton after a lot of searching, it look like disabling text wrapping in UITextView is impossible. But if you know otherwise, please let us know.