Swift text box TextView examples of mixed graphics

Source: Internet
Author: User
Tags mixed


When we use a text box (Uitextview), we may want to insert some pictures in it, in addition to entering text. Or some of the text mixed with the content needs to be displayed. This only needs to be achieved by TextView of the attributed text. J Insert the picture as an attachment.

This article demonstrates how to implement TextView by using examples, and optionally insert a picture pattern to keep the original size or adaptive size (these can be mixed). )

1, Effect chart

(1) Do not change the size of the inserted picture

(2) Keep the picture consistent with the line height. This way, the picture will not hold a large line, and will be consistent with the size of the text. Suitable for inserting an expression icon.


(3) Let the picture fill up one line. Suitable for the insertion of ordinary pictures or large images.

2, sample code

Import Uikit

Class Viewcontroller:uiviewcontroller {

Text area displayed by graphic and mixed rows
@IBOutlet weak var textview:uitextview!

Text Size
Let Textviewfont = Uifont.systemfontofsize (22)

Override Func Viewdidload () {
Super.viewdidload ()

Initialize display default content
Insertstring ("Welcome!")
InsertPicture (UIImage (named: "icon")!, Mode:. Fittextline)
Insertstring ("\ n Welcome access:")
InsertPicture (UIImage (named: "logo")!
InsertPicture (UIImage (named: "BG")!, Mode:. Fittextview)
}

Insert text
Func insertstring (text:string) {
Gets all the text of the TextView and turns it into a mutable text
Let Mutablestr = nsmutableattributedstring (AttributedString:textView.attributedText)
Get the current cursor position
Let Selectedrange = Textview.selectedrange
Insert text
Let Attstr = nsattributedstring (String:text)
Mutablestr.insertattributedstring (Attstr, atIndex:selectedRange.location)

Set font properties for variable text
Mutablestr.addattribute (Nsfontattributename, Value:textviewfont,
Range:nsmakerange (0,mutablestr.length))
Remember the location of the new cursor again
Let Newselectedrange = Nsmakerange (selectedrange.location + attstr.length, 0)

Assigning text to a new value
Textview.attributedtext = Mutablestr
Restores the position of the cursor (after the preceding code executes, the cursor moves to the last side)
Textview.selectedrange = Newselectedrange
}

Insert Picture
Func insertpicture (image:uiimage, Mode:imageattachmentmode =. Default) {
Gets all the text of the TextView and turns it into a mutable text
Let Mutablestr = nsmutableattributedstring (AttributedString:textView.attributedText)

Create a picture attachment
Let imgattachment = Nstextattachment (Data:nil, Oftype:nil)
var imgattachmentstring:nsattributedstring
Imgattachment.image = Image

Set how pictures are displayed
if mode = = =. Fittextline {
As big as text
Imgattachment.bounds = CGRectMake (0, -4, textview.font!. Lineheight,
textview.font!. Lineheight)
else if mode = =. Fittextview {
A full line.
Let ImageWidth = textview.frame.width-10
Let ImageHeight = Image.size.height/image.size.width*imagewidth
Imgattachment.bounds = CGRectMake (0, 0, imagewidth, imageheight)
}

imgattachmentstring = nsattributedstring (attachment:imgattachment)

Get the current cursor position
Let Selectedrange = Textview.selectedrange
Insert text
Mutablestr.insertattributedstring (imgattachmentstring, atIndex:selectedRange.location)
Set font properties for variable text
Mutablestr.addattribute (Nsfontattributename, Value:textviewfont,
Range:nsmakerange (0,mutablestr.length))
Remember the location of the new cursor again
Let Newselectedrange = Nsmakerange (selectedrange.location+1, 0)

Assigning text to a new value
Textview.attributedtext = Mutablestr
Restores the position of the cursor (after the preceding code executes, the cursor moves to the last side)
Textview.selectedrange = Newselectedrange
Move the scroll bar (make sure the cursor is in the viewable area)
Self.textView.scrollRangeToVisible (Newselectedrange)
}

Insert Picture 1: Keep the original size
@IBAction func BtnClick1 (sender:anyobject) {
InsertPicture (UIImage (named: "logo")!
}

Insert Picture 2: adapt to row height
@IBAction func BtnClick2 (sender:anyobject) {
InsertPicture (UIImage (named: "icon")!, Mode:. Fittextline)
}

Insert Picture 3: Fit TextView width
@IBAction func BtnClick3 (sender:anyobject) {
InsertPicture (UIImage (named: "BG")!, Mode:. Fittextview)
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

The size style of the inserted picture attachment
Enum Imageattachmentmode {
Case default//defaults (does not change size)
Case Fittextline//make size fit to row height
Case Fittextview//make size fit TextView
}

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.