"Swift" text mix, insert picture in iOS development in TextField or TextView

Source: Internet
Author: User

In iOS development, we typically enter text in TextField or TextView. When we need to insert a picture, it's actually very simple.We need to take advantage of the attributed text of the Textfield,textview, insert the picture as an attachment

The steps are as follows: (in TextView, the same in OC)

    • Create attachments while setting the font size of TextView (or set in storyboard)
      1         //Set font 2         Textview.font = uifont.systemfontofsize 3         //Create Attachment 4 let         attachment = Nstextattachment ()

    • Set the Picture property of the attachment to the picture you want to insert, convert the attachment to attributed text, and set the size of the attachment

    1. 1         //Set attachment photo 2         attachment.image = UIImage (picture name) 3         //Set Attachment size (-4 This number can be adjusted according to the actual situation, the width of the height can also set their own, here with the font size for reference) 4         attachment.bounds = CGRectMake (0, -4, TextView.font.lineHeight, textView.font.lineHeight) 5         // Convert an attachment to an attributed text of type nsattributedstring 6 let         attstr = nsattributedstring (attachment:attachment)

    • Gets the text in the current TextView, turns it into a variable text, records the position of the cursor, and inserts the attributed text in the previous step
    1. 1        //Get all the text of TextView, turn it into variable text 2         var mutablestr = nsmutableattributedstring (attributedstring: Textview.attributedtext) 3         //Get the current cursor position 4 let         selectedrange = textview.selectedrange5         //Insert text 6         Mutablestr.insertattributedstring (Attstr, atIndex:selectedRange.location)

    • Sets the properties of the new variable text and calculates the new cursor position
    1. 1         //Set font properties for variable text 2         mutablestr.addattribute (Nsfontattributename, value:UIFont.systemFontOfSize), range : Nsmakerange (0,mutablestr.length)) 3         //Remember again the position of the new Cursor 4 let         newselectedrange = Nsmakerange ( selectedrange.location+1, 0)

    • Assign the new text to TextView and restore the cursor position
    • 1         //re-assign the text to 2         textview.attributedtext = MUTABLESTR3         //The position of the recovery cursor (after the code executes, the cursor moves to the last face) 4         Textview.selectedrange = Newselectedrange

"Swift" text mix, insert picture in iOS development in TextField or TextView

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.