Analysis on the problems encountered in the mixed layout of micro-blog pages

Source: Internet
Author: User

There are a lot of details to be found in the graphic mix of the emoji keyboard. Sometimes it's hard to correct the reasons why you don't understand them. This article is mainly to collate the various problems and solutions I have encountered for everyone to share. If you have encountered similar problems in the past can be corrected with my method, I hope to be able to help Bo friends. This article uses the Swift language, OC may be uncomfortable, but most of the methods are basically the same is the syntax is different.

Previous review: About Weibo edit page add emoji keyboard

If you do not see this article in the Dong Yuan Blog Park, please click to view the original text.

1. By default each added emoticon is added to the last, but we want to add to the cursor position

Default practice (where Str is the expression string that is transmitted by clicking on an expression through an agent)

         will put the user's choice of expression, splicing to the end!            Textview.text = Textview.text + (str?? "")!

If you want to insert to the cursor position

            Insert emoji text textview.replacerange at user cursor position            (textview.selectedtextrange!, withtext:str!)
2. The icon size is different when added.

WORKAROUND: Set a uniform format for all range

           Set the Text property in the entire property string            let range = Nsmakerange (0, Strm.length)            //Keep the font of the variable property text consistent with the TextView!            Strm.addattribute (Nsfontattributename, Value:textView.font, Range:range)    

3. The cursor jumps to the end of the paragraph after inserting an expression in the middle, because the replacement text is used earlier

Workaround: Record the position of the cursor in advance and then restore the cursor position after replacing the text position

When you restore the cursor should be location+1, if not add 1 is inserted after the cursor in front of the expression

            Record cursor position location corresponding cursor position            var locations = textView.selectedRange.location            //Direct replacement text results will cause the cursor to move to the end            of the text Textview.attributedtext =  StrM            //reset cursor position            textview.selectedrange = nsmakerange (location + 1, 0)

4. But the TextView shows Textview.attrubutetext. Rich Text is an object that cannot be sent directly.

Workaround:

① first in each click on the expression when the Textview.attrubutetext print out can see a large number of printing, the output is a dictionary. The distinction can be seen if the expression, in the dictionary has nsattachment key value. And the text does not

② through the difference above that point, traverse this large batch of printing, print out the dict and range, all the text and emoticons are separated

Printing is:

③ Use this idea, write a textattrubute subclass, and in this method to the expression corresponding to the text symbol assignment, later access

Import Uikitclass sxemotetextattachment:nstextattachment {    ///emoticons with text symbol    var emotestring:string?        Returns a property string    class Func attributestring (Emoticon:emoticon, height:cgfloat), nsattributedstring {        var att Achment = Sxemotetextattachment ()        attachment.image = UIImage (contentsoffile:emoticon.imagepath!)        attachment.emotestring = Emoticon.chs                //Set height        attachment.bounds = CGRectMake (0, -4, height, height)                //2. Attribute text with Image        return nsattributedstring (attachment:attachment)    }}

④ When you click a picture, the calling method returns a property string that is called in this sentence. AttributeString automatically converts emoticons to text symbols

        var attributestring = sxemotetextattachment.attributestring (emoticon, height:font.lineHeight)

⑤ defines a result used to record the string after stitching

    Returns the string after conversion is complete in the text box-(converts emoticons to emoji)    func fulltext (), string {        var result = string () let        textRange = Nsma Kerange (0, Attributedtext.length)                attributedtext.enumerateattributesinrange (TextRange, Options: Nsattributedstringenumerationoptions.allzeros, Usingblock: {(Dict, range, _), Void in                        if let attachment = dict[" Nsattachment "] as? sxemotetextattachment {                //image                result + = attachment.emotestring!            } else {                result + = ( Self.attributedText.string as NSString). Substringwithrange (range)            }        })        println ("micro-Bowenben: \ (Result)")        return result    }

⑥ at the end of the tweet when the change in the parameters, not hair textview.attrubutetext is not hair textview.text. But after the stitching.

    Tweet    @IBAction func sendstatus (sender:uibarbuttonitem) {let        urlstring = "https://api.weibo.com/2/statuses /update.json "                if let token = Accesstoken.loadaccesstoken ()?". access_token{            //Here Call the Fulltext method to return the record and stitch the Let            params = ["Access_token": token, "status": Textview.fulltext ()] Let                        net = Networkmanager.sharedmanager                        Net.requestjson (. POST, urlstring, params) {(result, error)--() in                svprogresshud.showinfowithstatus ("success of Weibo send")                Self.dismissviewcontrolleranimated (True, Completion:nil)}}}    

⑦ to this finish, edit micro-Bournemouth display, and send Weibo text separate governance, non-conflict.

5. When canceling the release, the writing micro-Bo controller quickly retracted, back to the home keyboard only slowly retracted.

Workaround:

    Cancel button Click event    @IBAction func Cancel (sender:uibarbuttonitem) {///                for a better user experience first shrink the keyboard and then indent the text box        Self.textView.resignFirstResponder ()        dismissviewcontrolleranimated (True, Completion:nil)    }

6. In the input box, the expression and text in the horizontal direction is not aligned state, up and down there is a difference

WORKAROUND: Fine-Tune

            Let height = textView.font.lineHeight            attachment.bounds = CGRectMake (0, -4, height, height)

7. The placeholder and send buttons are hidden and lit only when you enter text. No response when clicking Insert Picture

Workaround: Manually let the user invoke the proxy method when the picture is entered

In the Picture Click Proxy Method Emoticonsviewcontrollerdidselectemoticon

        Invoke Proxy method manually-whether you can insert text        if TextView (TextView, ShouldChangeTextInRange:textView.selectedRange, Replacementtext:str !) {            ///Set the text of the input box            if emoticon.chs! = Nil {                ///Textview.settextemoticon (emoticon)                ///manual call from the method of classification Didchage method                Textviewdidchange (textView)                            }else if Emoticon.emoji! = Nil {///the                default is to put the expression into the end, using this line of code to insert the text at the cursor position C10/>textview.replacerange (textview.selectedtextrange!, withtext:emoticon.emoji!)            }        }

Where the textviewdidchange is used to control the button state

    Func Textviewdidchange (textview:uitextview) {let        fulltext = Self.textView.fullText ()                self.textview.placeholderlabel!. Hidden =!fulltext.isempty        sendbutton.enabled =!fulltext.isempty    }

8. When controlling the maximum input number, an expression such as "[Flower heart]" takes up 8 bytes

Turned out to be

        Weibo text is usually limited to 140 characters        if textView.text.lengthOfBytesUsingEncoding (nsutf8stringencoding) + Text.lengthofbytesusingencoding (nsutf8stringencoding) > (+ $) {            return False        }

Change into

        Weibo text typically limits 140 characters        if (Self.textView.fullText () as NSString). Length + (text as NSString). length >            False        }

If you do not see this article in the Dong Yuan Blog Park, please click to view the original text.

Welcome attention!

Analysis on the problems encountered in the mixed layout of micro-blog pages

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.