Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Option to display formatted C program in iOS

What are the option that I can use to display formatted C Program in iOS. What I can think of right now is
1. Make a HTML file with code in it, and find some CSS to display in web view. Not a good option as I have to manually handle formatting.
2. Use some kind of parser to convert it in NSAttributedString and then display it in UITextView.

If correct option is one , pointer to any such CSS or java script would be great help, or if option is two is there any such parser available.

Or is there any other option to achieve same.

like image 570
Amit Singh Avatar asked Jan 21 '26 22:01

Amit Singh


2 Answers

Lookd in pretty print and looks like it can format cpp see the following example: You could try to paste your code in here and have it formatted (if you don't need it done dynamically). Then use Firefox with firebug plugin to right click the element, go to the root and copy the thml. Copy the css files as well (under the CSS tab you can find them)

<!DOCTYPE html>
<html>
 <head>
<script src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Example</title>
</head>
 <body>
   <pre class="prettyprint lang-cpp">
int
foo(int k)
{
        if (k < 1 || k > 2) {
                printf("out of range\n");
                printf("this function requires a value of 1 or 2\n");
        } else {
                printf("Switching\n");
                switch (k) {
                case 1:
                        printf("1\n");
                        break;
                case 2:
                        printf("2\n");
                        break;
                }
        }
}
   </pre>


 </body>
</html>
like image 103
HMR Avatar answered Jan 24 '26 12:01

HMR


Currently there is no API available for do this.

My suggestion:

Convert your code to html and load it in UIWebView. I know there is a lot of difficulties behind this.

You can use codeformatter for converting your code to html. Hence there is no api's available this is one solution.

I had wrote an open-source library for doing this. Please check MMP_CodeViewer

like image 43
Midhun MP Avatar answered Jan 24 '26 13:01

Midhun MP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!