Hello everyone, I would like to know how do I do a textView of this in ios with swift?
Thank you in advance for your attention.
If you are going to show an isolated message, you can do this by using a standard UITextView with a UIImageView behind it.
The TextView has a transparent background and contains only text. ImageView contains the background image, in this case the balloon. In order for the image to not be distorted, use resizableImageWithCapInsets
, which allows you to define the insets of the image edges that will be preserved when the image is resized.
If the balloon has the edges away from the edges, use textContainerInset
to move the text away from the edges. It would look something like this:
class ViewController: UIViewController {
@IBOutlet var textView : UITextView!
@IBOutlet var imageView : UIImageView!
@IBOutlet var textViewHeightConstraint : NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
var image : UIImage = UIImage(named:"test.png")!
image = image.resizableImageWithCapInsets(UIEdgeInsetsMake(20, 100, 30, 80))
imageView.image = image
textView.backgroundColor = UIColor.clearColor()
textView.textContainerInset = UIEdgeInsetsMake(20, 20, 0, 10)
textView.scrollEnabled = false
//redimensiona textView de acordo com o texto
var sizeThatFitsTextView : CGSize = textView.sizeThatFits(CGSizeMake(textView.frame.size.width, CGFloat.max))
textViewHeightConstraint.constant = sizeThatFitsTextView.height
}
}
In the example I created a height constrait for the textView in the storyboard and changed its value according to the height of the text. I also made the height of the imageView always equal to that of the textView, using constraints. If you want you can do all this for the code.
If by chance you implement a list with multiple messages, you can adapt the idea to use UITableViewCell or better yet, use one of several libraries for this purpose, such as