Open Desgard opened 6 years ago
Wierd, I'll take a look at it, but must probably is an issue in the demo, not in the library.
@raspu I kill the app, and restart. It go back to normal. 🤣 I will try to find the problem, again.....
Lol, let me know if you can reproduce it again.
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.....😓
I have this issue as well
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)
}
}
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
Thank you @JosephShenton I will try to fix it later today using your code.
@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.
Disable text wrapping on the textview maybe?
tested https://github.com/raspu/Highlightr/pull/52
the key:
let textContainer = NSTextContainer()
also iOS demo
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.
In your demo, the
Maxima
file:But in the demo, your
UITextField
can't show all the letters: