2017-08-08 2 views
0

Je développe une application dans laquelle j'ai un texte ainsi qu'un UIImage à l'intérieur d'un UITextView et j'essaie d'aligner le texte ainsi que l'image verticalement et à gauche dans la direction horizontale, comment puis-je y parvenir?Alignement vertical et horizontal du texte et de l'UIImage dans un UITextView

let image = UIImage(named: "Downward Arrow - 01.png") //Step (10) This is where the image is defined. 

let attachment = NSTextAttachment() // Step (10) inserting a NSTextAttachment in order to attach the image in a UITextView. 

let scaledImage = image?.scaleImageToSize(img: image!, size: CGSize(width: 30, height: 30)) // this line of code is needed in order to resize the size of the image. 

attachment.image = scaledImage // Step (10) setting the image to be attached to the scaled one that we just obtained out of the previous code. 

//put your NSTextAttachment into and attributedString 

let attString = NSAttributedString(attachment: attachment) // Step (10). 

testTextView.textStorage.insert(attString, at: testTextView.selectedRange.location) // Step (10). 
+0

vous utilisez '' UITextField' ou UITextView'? – GIJOW

+0

J'utilise un UITextView –

Répondre

0

Vous pouvez avoir quelque chose comme cela en utilisant bord empiècements

let image = UIImage(named: "Downward Arrow - 01.png") //Step (10) This is where the image is defined. 

let attachment = NSTextAttachment() // Step (10) inserting a NSTextAttachment in order to attach the image in a UITextView. 

let scaledImage = image?.scaleImageToSize(img: image!, size: CGSize(width: 30, height: 30)) // this line of code is needed in order to resize the size of the image. 

let vCenter = ((testTextView.frame.size.height - 30)/2) 
let padding = UIEdgeInsets(top: vCenter, left: 0, bottom: 0, right: 0) 
testTextView.textContainerInset = padding 

attachment.image = scaledImage // Step (10) setting the image to be attached to the scaled one that we just obtained out of the previous code. 

//put your NSTextAttachment into and attributedString 

let pic = NSAttributedString(attachment: attachment) // Step (10). 
let text = NSAttributedString(string: "YOUR TEXT") 
let attString = NSMutableAttributedString() 
attString.append(pic) 
attString.append(text) 

testTextView.textStorage.insert(attString, at: testTextView.selectedRange.location) // Step (10). 

Résultat: enter image description here

+0

Merci GIJOW pour votre réponse rapide qui a été très utile pour résoudre mon problème et aligner le UIImage à l'intérieur de UITextView verticalement. Cependant, ce que je cherche est disons que j'ai ce qui suit: let testText = "Bonjour, comment allez-vous" alors ma question est comment puis-je ajouter ce texte à la UIImage ci-joint dans UITextView puis aligner les deux verticalement, à en même temps alignant le texte à gauche et l'image à droite? MERCI BEAUCOUP! –

+0

désolé ne peut pas comprendre – GIJOW

+0

let textInsideTextView = "Bonjour tout le monde"; let image = UIImage (nommé: "Flèche vers le bas - 01.png") // Etape (10) C'est ici que l'image est définie. let attachment = NSTextAttachment() // Etape (10) insertion d'un NSTextAttachment pour joindre l'image dans un UITextView. let scaledImage = image? .scaleImageToSize (img: image !, taille: CGSize (largeur: 30, hauteur: 30)) // cette ligne de code est nécessaire pour redimensionner la taille de l'image. attachment.image = scaledImage // Etape (10) définissant l'image à joindre à l'image mise à l'échelle que nous venons d'obtenir avec le code précédent. –