I recently read cocoaui source code and cocoaui source code.

Source: Internet
Author: User

I recently read cocoaui source code and cocoaui source code.

In the first half of this year, a cocoaui library was introduced to build an ios application. It was mainly used to layout the ios interface, and found that a lot of code and workload were simplified. Because when writing the first ios app, the Code layout was used. When it adapted to models 4s and 6, it was almost killed. A large number of constraints and definitions were filled with the code.

Cocoaui is the author of ssdb, ideawu. It is very active in Weibo. If there is a problem, he will usually respond soon. Ssdb is a nosql database similar to redis; there are few people who have made achievements in the client and server fields. I am a common programmer. I am still a little away from this kind of great god, but I am not discouraged. Only by understanding them can I approach them. In addition to envy their ability to solve problems, they still need to learn their ideas for solving problems and the spirit of solving these problems.

I remember that every time I learned a new language in college, I always used a third-party library to create a dictionary, a weather forecast, a calculator, or a little tool like a browser. For example, the dictionary uses the Baidu dictionary api and webview used by the browser. However, at that time, I really wanted to implement a browser and parse html + css by myself. In college, xml parsing is still much worse, and a line of code is not written. In fact, there are many xml parsing libraries that can be used for reference.

Let's talk about cocoaui's idea. When deploying the ios application interface, xib is not used, nor storyboard is used. Instead, html and css of the web are used for ios interface layout. The overall idea is to use the libxml2 library to parse the html tags and convert them into the corresponding uibutton, uilabel, uiimage, and uiview, and place them in the interface. As for their location in the interface, it is through parsing css, and then set border, margin, padding, width, height, corlor, text; on the parsing of css, the author wrote two articles, http://www.ideawu.net/blog/archives/912.html Coding.

Everyone is saying that the constraint layout of ios is not suitable for people to understand, but most people are still adapting to it. However, cocoaui's authors bypassed the ios constraint layout and used the web div + css to implement stream layout. This really gave us a good choice, at the same time, it also provides an idea to solve the problem. Instead of falling into it, it jumps out and selects another method. Of course, cocoaui writing also requires deep technical skills, such as css parsing.

According to this idea, we can re-define the layout of android. I wonder if you have ever thought that the layout file of android is very messy or redundant. For a small application, the xml configuration file size is almost equivalent to the size of the entire code file. We can also define html + css layout based on the cocoaui idea.

1: Introduce xml files in the activity.

2: Use dom4j to parse html tags in xml and convert these tags to android controls. For example, convert input (type = button) to Button and input (type = input) convert to edittext, span to textview, and div to layout.

3: parse the css file and define the position of the control on the interface. For example, if you encounter margin: 0 10 0 0, you can get the LinearLayout of the button. layoutParams, and then call the setMargins function to set the margin of the button. In case of padding: 10 0 0 0, the same is true. In case of color, set the backgroundColor of the button.

In this way, the layout of android is not redefined. We can even use the reference css method to define the common css method, which can simplify the layout file. This solves the layout redundancy problem of androidxml.

Of course, this is just an idea. talk is cheap, show code!

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.