Highlightr icon indicating copy to clipboard operation
Highlightr copied to clipboard

The UITextField can't show the whole code file

Open Desgard opened this issue 7 years ago • 12 comments

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

Desgard avatar Jan 19 '18 12:01 Desgard

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

raspu avatar Jan 19 '18 12:01 raspu

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

Desgard avatar Jan 19 '18 13:01 Desgard

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

raspu avatar Jan 19 '18 13:01 raspu

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.....😓

Desgard avatar Jan 19 '18 16:01 Desgard

I have this issue as well

JosephShenton avatar Jan 23 '18 11:01 JosephShenton

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 avatar Jan 23 '18 11:01 JosephShenton

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

JosephShenton avatar Jan 24 '18 03:01 JosephShenton

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

raspu avatar Jan 24 '18 09:01 raspu

@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.

Desgard avatar Jan 29 '18 01:01 Desgard

Disable text wrapping on the textview maybe?

JosephShenton avatar Jan 30 '18 09:01 JosephShenton

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

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

cntrump avatar Jul 09 '18 02:07 cntrump

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.

steve-taylor avatar Jan 13 '19 07:01 steve-taylor