This question already has answers here:
UITextView data change swift
(6 answers)
Closed 4 years ago.
Text Editor
What I'm Doing
Text View
I am attempting to create a UITextView that is a code editor on IOS and I am trying to make different types of code be different colors so everything isn't just one color (such as comments would be grey).
Problem
The problem I am having is that I am linking the Text View like this:
#IBOutlet weak var textView: UITextView!
and I need to detect when the textView has been changed do you know of any way to do this?
To do this, you need to conform to the UITextViewDelegate protocol and then set the textView's delegate to self.
So, after UIViewController on the beginning of this view controller, you need to add the protocol conformance. Example
class MyViewController: UIViewController, UITextViewDelegate {
then set the delegate in viewDidLoad():
override func viewDidLoad() {
super.viewDidLoad()
textView.delegate = self
}
Now, you're set up for success. All of the delegate methods on UITextViewDelegate are optional, so you only need to implement the ones you want. In this case, the one you want to implement is func textViewDidChange(_ textView: UITextView)
That method will get triggered every single time the text in the text field updates.
Create Editing Changed #IBAction from your UITextView in storyBoard into your viewController.
Related
I have created this layout using UITableView header and I'm trying to move profile picture when scrolling up and removing full name and Phone number. First screen UI is what I have created and it is made in tableview header and
I want to achieve a functionality of a second screen when scrolling up.
You need add UIScrollViewDelegate to your tableView.
Table view inherit UIScrollView and their UIScrollViewDelegate methods.
So... you need set like this:
Somewhere set delegate:
tableView.delegate = self
(I think you mb already implement this because you need to implement UITableViewDelegate methods)
and implement UIScrollViewDelegate method:
extension YourControllerOrAnotherDelegateClass: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
// use scrollView.contentOffset to calculate when you need to update navbar
}
}
inside this method you can get content offset of your scrollView and update nav bar when you needed.
In a collectionView, I'm using several textFields with different issues. All have an "extended keyboard" with custom UIBarButtons. As I need to reuse it a lot, I solved my problem with using an extension, quite similar to this one : https://stackoverflow.com/a/31010668/8162027.
My buttons are always the same: "cancel" and "Done".
But the func linked must return different things. Is there a way to override those #objc func for these 2 buttons donePressed() and cancelPressed() from my UIcollectionView?
Here is a screenshot of my buttons:
https://imgur.com/BvXrwMh
Well, it'd day 5, I have tried everything to make this work. Setup is simple
I am trying to write a custom keyboard.
Which is having the CollectionView, CollectionViewCell is powered by xib
I have one label, which would hold the emoji when running.
I did ready every single article here where IBOutlet is nil.
Nothing is pointing to the direction that I haven't tried.
class CategoryCollectionCell: UICollectionViewCell {
#IBOutlet weak var emojiLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
}
Outlet is connected to the class.
KeyboardViewController is CollectionViewData source and delegate.
I am able to write the same code and UIViewController is able to get the collectionView Cells and showing the emojis.
I have run out of options. So I am posting here.
There is nothing extraordinary that I am doing here. Except its a keyboard extension.
Also, SIGQUIT is the biggest problem, any suggestions what is causing that. as I haven't able to put a finger on it.
I was asked to help out in a project with swift and work in xcode on a short timescale, neither which I have worked with before.
The task is getting a picture to be "zoomable". And after some researched I found that putting a UIScrollView on top of the image will suffice. Now, since the image view already exists and is integrated in the code on a level I do not dare triffle with, with some constraits and what not.
I dont want to start the process all over with a new image view and later try to hook it into the code. Mostly because the image view is inside a table cell inside of a table view.
What I have done is:
On the storyboard
Put the image view inside the view on the storyboard.
Assigned the delegate of the scroll view to the table cell.
I couldnt quite figure out how to constraint these so I mostly use 'Add missing constraints'.
In code for the TableViewCell
Inherited the UIScrollViewDelegate
made a new var with #IBOutlet weak var scrollView: UIScrollView!
Since I am in a controller, i cannot override viewDidLoad function, so I implemented that and set minimum and maximumZoomScale to some values.
Implemented a viewForZooming function that returns the UIImageView
I figured out somewhat how I can use the constraints and properties of the ImageView to resize and stuff, but the regardless of what I try to do, I cannot get the "zoom" to work.
Is there any property that the ImageView could have that is messing this up, what should I check for?
After reading and trying to figure out, what finally made it work was this:
The ScrollView was made parent in the Interface Builder to the ImageView
I magically made the constraints to fit the ScrollView aligned to everything around it mostly by Add missing constraints when prompted with constraint errors.
Rightclicked the ScrollView then for the delegate, clicked the + sign and dragged that to the UITableViewCell.
I also did the same for the Referencing Outlet
And code for the UITableViewCell is this:
class ClothesItemTableViewCell: UITableViewCell, UIScrollViewDelegate {
#IBOutlet weak var scrollView: UIScrollView!
var theImage: UIImage? {
set {
clothinhImageView.image = newValue
} get {
return clothingImageView.image
}
}
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return clothingImageView
}
}
Might not be perfectly clear, and I couldnt find the exact article that made the work done. But there are plenty that tries to solve this issue in different ways, and somehow this worked. Hopes it helpes somebody!
I have imported UICollectionViewDelege, UICollectionViewDatasource and UIScrollViewDelegate.
I have set my collection views delegate and datasource to self.
But scrollViewDidScroll isnt being called, my scroll view goes horizontal if that makes a difference?
Why is scrollViewDidScroll not being called if collection views use the scrollview inside them?
I was moving form swift 2 to swift 3 and what did the trick is the following:
swift 2:
public func scrollViewDidScroll(scrollView: UIScrollView)
to swift 3:
public func scrollViewDidScroll(_ scrollView: UIScrollView)
So basically the "_" underscore did the trick. What is annoying is that swift compiler didn't complain about it!!!
As for inheritance UICollectionViewDelegate should do the trick.
I had to add information that my controller conforms to UIScrollViewDelegate protocol in order to make it work
I've also encountered this issue. I started off by setting the delegate to self inside of storyboard, at that point scrollViewDidScroll wasn't getting invoked (but functions like collectionView willDisplayCell were, as expected). After some playing around I've noticed that setting delegate programmatically fixed the issue.