iOS Development--Learn to analyze engineering from Tqrichtextviewdemo

Source: Internet
Author: User

http://code4app.com/ios/TQRichTextView/5244fe9c6803fa0862000000


1. First find the Appdelegate class, no matter how complex a project, how many classes, but only one entry is the Appdelegate class Didfinishlaunchingwithoptions method. We can start from here when we read the source code of others. In the Tqrichtextviewdemo project, only one tqviewcontroller is created in this method and Rootviewcontroller is set to window.


2. To see the realization of Tqviewcontroller. The first is also starting from the initialization method, no initwithnib, then look at Viewdidload. In this method, only one Tqrichtextview is created, properties such as Frame,text,backgroundcolor,delegate are set, and then added to the Self.view. After reading the Viewdidload method, found that this is the last method of system call, then combined with the operation of the simulator, we can guess, the text parsing and drawing process is encapsulated in the Tqrichtextview.


3. Go to see Tqrichtextview.

  1. Browse through the header file to see which properties and callable methods are available.
  2. Read the initWithFrame method and find that this is just the initialization of several properties.
  3. Find out if there is a Set,get method for rewriting. Discover the Settext,setfont,settextcolor,setlinespacing method. Reading finds that the Setneedsdisplay method is called each time the value is updated. Note that DrawRect is called every time the four objects are assigned a value.
  4. Continue reading the. m file, Discover the DrawRect method, this is also a system call, then the work of the drawing should be done here. Read the DrawRect method.
    1. In DrawRect, the Analyzetext method is called first, the parameter _text is passed in, and the return value is assigned to _textanalyzed.
      1. To see the Analyzetext method. The array and the dictionary are emptied first, then a result variable of type nsstring is defined and an array of type Nsmutablearray is assigned to Richtextrunsarray.
      2. A method of implementing Tqrichtextemojirun, A pointer to the string and array passed in.
        1. Because this method is TQ Richtextemojirun's class method, all of us go directly to the method without looking at the Tqrichtextemojirun initialization method. Even with a guess we can be sure that this method should be a way to parse the expression in a string. Look at the point, found that if the expression is parsed, then create a tqrichtextemojirun formation, range= expression string in string range,originaltext= expression string, the created object into the incoming array pointer, Finally, replace the expression string in the incoming string with a space and return the string that was replaced by the expression.
        2. by looking at the Tqrichtextemojirun init method, Two variables were set during initialization, from the parent class Tqrichtextbaserun, set tqrichtextruntype and whether to respond to touch. Found Tqrichtexturlrun also inherited from Tqrichtextbaserun.
        3. jump out of this method and go back to Analyzetext and continue looking down.
      3. A method that executes Tqrichtexturlrun, passing in a pointer to result and array.
        1. ibid. , go straight to see the implementation of this method. Similar to the naming format of the Tqrichtextemojirun, the invocation of the method name is similar, we can guess that this method is to parse the URL, then the point of view directly. found that if a matching string is found, a Tqrichtexturlrun object is created, the Range,origintext = URL of Range=url is set, and the object is added to the incoming array, and finally a string is returned.
        2. by looking at the Tqrichtexturlrun init method, Two variables were set when the same initialization was found, from the parent class Tqrichtextbaserun, set tqrichtextruntype and whether to respond to touch. Discover Tqrichtextemojirun inherited from Tqrichtextimagerun,tqrichtextimagerun inherited from Tqrichtextbaserun.
        3. jump out of this method and go back to Analyzetext and continue looking down.
      4. Iterates through the objects in the Richtextrunsarray, each object calls the Setoriginalfont method, passing in the parameter Self.font. By reading the internal implementation of the two methods above, it is clear that the Tqrichtextemojirun and Tqrichtexturlrun objects are saved in Richtextrunsarray, so look at the Setoriginalfont method of the two objects.
        1. This setting is a property inherited from the parent class Tqrichtextbaserun.
        2. Go back to Analyzetext and continue looking down.
      5. Return to result, step out of this method and continue to see DrawRect.
    2. The next step is to create the nsattrbutedstring and assign some properties.
    3. Text processing, traversing objects in Richtextrunsarray, calls the Replacetextwithattributedstring method for each object.
      1. Tqrichtextemojirun not found Replacetextwithattributedstri Ng method, then go to Tqrichtextimagerun and read the method.
        1. incoming Attrst The ring removes the placeholder space character.
        2. create Ctrundelegatecallbacks, The rebate is set at 1.1 times times the width of the originalfont height.
        3. Create a space nsattrbutedstring object, Add the Ctrundelegateref property.
        4. Insert the space Nsattrbutedstring object into the incoming attrstring 。
        5. Look at the Super method and find that a key-value pair is also set on this range. Key for Tqrichtextattribute,value for himself.
      2. Ibid. read the Replacetextwithattributedstring method of Tqrichtexturlrun.
        1. Adds a blue font to the text of the paragraph range.
        2. Call the Super method to add the same Key-value key-value pair as above for the segment range.
      3. Return to continue reading DrawRect.
    4. Here is the preparation of the drawing, do not want to study how to draw the rough time on the line.
    5. Empty Richtextrunrectdic
    6. Draw, do not want to study can also rough time skip.
    7. Find the focus and draw the replaced special text unit.
      1. Walk out each run and take out the value of key tqrichtextattribute in the attributes of the run.
      2. If the value is present, the run is a special run.
      3. Ask for the run frame, rough time skip.
      4. Call the Drawrunwithrect method and assign the return value to the Iddraw variable.
        1. Tqrichtextemojirun's Drawrunwithrect method draws an emoticon image and returns Yes.
        2. The Drawrunwithrect method of Tqrichtexturlrun returns No.
        3. The Drawrunwithrect method of Tqrichtextbaserun returns No.
      5. Determine the Isresponsetouch property of Run, find after finding is Tqrichtextbaserun property, comment is whether to respond to touch
      6. If the response touches
        1. Add a key-value pair to the richtextrunrectdic, and key to Rect,value is run.
    8. Sets the condition of the loop, releasing a variable of type CF. A variable of type CF does not support arc.
  5. Continue to look for ways to overload the system in Tqrichtextview. Cashback Touchesbegan and Touchesend methods. Read these two methods.
    1. To get the point of the touch, the coordinates of the conversion point are the coordinates of the origin point in the lower left corner.
    2. Judging if you can just delegate
    3. Traverse the richtextrunrectdic to determine whether the point is in a rect. If the delegate method is called.
  6. The main function analysis is complete.
  7. Read through the Tqrichtextbaserun to learn about the default values for other properties and some properties.
  8. At this point, the entire Tqrichtextviewdemo basic analysis is complete. The following conclusions can be drawn.
    1. Structural relationships of classes
        1. Tqrichtextemojirun-> tqrichtextimagerun-> Tqrichtextbaserun.
        2. Tqrichtexturlrun-> Tqrichtextbaserun.
        3. Tqrichtextemojirun and Tqrichtexturlrun respectively implement the Analyzetext:runsarray method used to remove the required text from the string and create a Tqrichtextbaserun object to add to the array to save.
        4. The Replacetextwithattributedstring method is overloaded by Tqrichtextemojirun and Tqrichtexturlrun to add attributed properties at a specific range of strings.
        5. Tqrichtextemojirun and Tqrichtexturlrun respectively overloaded the Drawrunwithrect method to implement a custom position and return whether the content was drawn.
    2. Process
      1. Change the properties to Tqrichtextview.
      2. Call DrawRect.
      3. Call the Analyzetext:runsarray method of Tqrichtextemojirun and Tqrichtexturlrun to parse the string.
      4. Create a nsattributedstring.
      5. Adds an attribute to nsattributedstring.
      6. Call the Replacetextwithattributedstring method of Tqrichtextemojirun and Tqrichtexturlrun to add a property to the string.
      7. Coretext draw nsattributedstring.
      8. Invoke the Drawrunwithrect method of Tqrichtextemojirun and Tqrichtexturlrun to implement custom drawing.
      9. Save the run and Rect dictionaries, and determine whether you can click from the dictionary values.
    3. The above can be concluded that to replace the expression and text parsing rules, you just go to Tqrichtextemojirun or Tqrichtexturlrun modify Analyzetext:runsarray, Replacetextwithattributedstring,drawrunwithrect three methods.
      1. Analyzetext:runsarray modify the parsing rules.
      2. Replacetextwithattributedstring Modify the Add property.
      3. Drawrunwithrect Modify Custom Drawing.
    4. To implement a click, simply set Isresponsetouch to Yes and implement Tqrichtextviewdelegate.
  9. tqrichtextview awakefromnib method is not implemented, if you use Xib drag, you cannot add a default property for Tqrichtextview.
    1. workaround, add the Awakefromnib method, and assign a value to the default property in it.
  10. SIZEWITHF Ont and boundingRectWithSize:options:attributes:context: The line spacing is not calculated when the method is calculated, Tqrichtextview's linespacing, if not set to 0, causes the height of the two methods to be calculated to be less than the height required by the Tqrichtextview actual display.
    1. workaround, Tqrichtextview LineSpacing is set to 0.

iOS Development-Learn to analyze engineering from Tqrichtextviewdemo

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.