Swift: Remove contentInset for UITextView in TableViewCells - swift

I am attempting to set the contentInsets to zero for my textView that I place inside my tableViewCells so that I can align my text with the label and imageView bubble. My current implementation looks like this.
I adopted the solution in this post but does not seem to work for my case.
Blank space at top of UITextView in iOS 10
The solution in the above post is for uiviews but mine is in a tableViewCell, which automaticallyAdjustsScrollViewInsets is not available in tableViewCell.
Any help here pls?

Setting contentInset to zero will not work since it's applied to UIScrollView.
The spacing that you want to remove is actually controlled by NSTextContainer inside of UITextView.
To remove the spacing just put this in your cell (for example inside awakeFromNib):
override func awakeFromNib() {
super.awakeFromNib()
// Removes UITextView inner spacing
textView.textContainerInset = .zero
textView.textContainer.lineFragmentPadding = 0.0
}

Related

unable to scroll the textview in Swift

I have a TextView as shown below
I am not able to scroll the text view , I have added UITextViewDelegate in UIViewController class as well as set isUserInteractionEnabled property in textViewDidBeginEditing
func textViewDidBeginEditing(_ textView: UITextView) {
textView.backgroundColor = UIColor.lightGray
textView.isEditable = false
textView.isUserInteractionEnabled = true
textView.isScrollEnabled = true
}
What did I need to do?
Also, the scrolling is enabled in attribute inspector
This issue has occurred because the actual UITextView's size was more than screen size as shown below
The real answer is that the UITextView needs its content frame height inferior to its content size height to be able to scroll.
In your case, the content frame height is equal to the content size, so it doesn't scroll.
You just have to set the leading left, trailing, top space and bottom space to the View, but first make sure the Text View is smaller than the actual View (parent).
It starts to get complicated when you have a UITextView on a UIView which is either a view on a UIScrollView or directly on a UIScrollview.
The best thing to do, is to give each of them their own actions when scrolling.
Firstly, make sure you have all of your delegates for EVERYTHING set.
Make sure your UITextViews are not user restricted for what they don't need to be.
What I have to do in one of my published apps is this👇
func scrollViewWillBeginDragging(_ scrollView: UIScrollView)
{
if scrollView == textView1
{
// Do something if you actually want to, or just let textView1 scroll as intended
}
else if scrollView == textView2
{
// Do something if you actually want to, or just let textView2 scroll as intended
}
else if scrollView == zoomingScroll
{
// Do something if you need to or leave it
}
else
{
// Do something with all the other scrollable views if you need to
}
}

Scroll in UIScrollView with tvOS

I stuck with scrolling content inside UIScrollView in my tvOS app.
I have scrollView with height = 400 and width = 400. Inside this scrollview I have non-scrollable UITextView with height = 800 and width = 400. So I want to scroll this text.
But my scroll view is not scrolled, I don't see any scrolling indicators and also my scrollView have isFocused value = false. How can I solve this problem?
p.s. update I created separate ViewController (image below).
It have black ScrollView and white view with big height with label in the middle of it. ScrollView has fixed width and height and there is no scrolling for some reason! I even didn't connect any separate class - just created it from Interface builder.
UIView isn't focusable by default (and as such in tvOS it can't be scrolled) . You have to subclass it to override canBecomeFocused:
class myUIView: UIView {
override var canBecomeFocused: Bool {
return true
}
}
Then in your example use the class myUIView instead of UIView for your long white view. Setting the right constraints you wouldn't need to design the views out of the controller view boundaries in IB either. Check this answer with the linked gist to see how to build the constraints.
I think you can use the
func gestureRecognizer (_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool
method to judge the view and scroll it
According to the view you touch to do something to intercept
There is some way to fix your problem.
Please check your scrollview in storyboard, is your scrolling or vertical indicator is checked?
U can make your textview scrollable if u just want to scroll the textview content
There is problem in your constraint, you need to cleary add the height, vertical space top, vertical space bottom from your textview to your scrollview.
Hope these advice can help you
So you have to set the scrollview's content size larger than the scrollview bounds, so you should set contentsize to (400, 800), probaly in you xib

Swift, Auto Resize Custom Table View Cells

In my app, I have a table view with an image, label and text view in each cell. I would like to be able to auto-resize the cells depending on the amount of content in the text view. (The text view is the lower most text.)
So far, I have added the correct constraints; leading, trailing, top and bottom to the text view and have disabled scrolling and editing.
In my tableViewController.swift file, I have written this code:
override func viewWillAppear(_ animated: Bool) {
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
}
However, this is not working as when I add more text to the text view, it just cuts off.
Maybe this has something to do with the card like look, I have got a UIView in each cell acting as a card.
I honestly don't know what I am doing wrong
A picture is below of what my app looks like and if anyone could help that would be greatly appreciated
Check if your constraints are like this(based on your image) :
imageView : set to Top and Leading of your container, with fix height and width values.
label : you can set it to top and horizontal space of your image, with fix height and width as well.
textView : leading to image, top space to label, trailing and bottom to container.
And keep using
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
in your viewWillAppear()
Update for swift 4.2
Use:
UITableView.automaticDimension
Instead of:
UITableViewAutomaticDimension
Make sure that the content mode is set to Scale To Fill of your UITextView
Make sure that there are no height constraints for the UITextView and the card UIView
Try to add the estimated height into viewDidLoad:
override func viewDidLoad() {
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
}
Maybe the AutoHeight is not working because of the UIView above the UITextView. Try to call the sizeToFit and layoutIfNeeded methods for the UIView in the cellForRowAt:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier", for: indexPath) as! CustomTableViewCell
cell.vwCard.sizeToFit()
cell.vwCard.layoutIfNeeded()
return cell
}
You can also try the sizeToFit and layoutIfNeeded as well for the UITextView.
Hope this works........
Set bottom of your textView with bottom of that white UIView and make sure that white UIView has left,right,top and bottom constraints :)
Same type of example is explained here programmatically....

Setting left inset of empty uitableviewcells (placeholder cells) to 0 to eliminate left padding?

I was able to find an answer in stack overflow that led me to successfully eliminate the default left padding occurring on my cell separator by setting left inset = 0 in the storyboard of Xcode. However the empty cells beneath my populated custom cells still have padding on their left separators.
How can I fix this?
In your storyboard for the Table, Set the separator inset
And finally in awakeFromNib for custom table cell
Swift 3.x
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
self.layoutMargins = UIEdgeInsets.zero
self.preservesSuperviewLayoutMargins = false
}
For Swift 2.x
self.layoutMargins = UIEdgeInsetsZero
self.preservesSuperviewLayoutMargins = false
Output

How to automatically adjust the width of a centered UITextfield in Swift while editing?

I have a custom UITextfield that is x-centered to its superview via a constraint. It also has a height-constraint and a nice thin line right under the text that has the same width as the textfield itself (like I said: custom).
I now want to keep the UITextfield centered while editing its content, adjusting itself in width to its string content.
What right now happens is that it keeps its width while editing, cropping its overlapping content.
My code is kind of
// on textfields editing change event
#IBAction func nameFieldEditingChanged(sender: AnyObject) {
// set width of textfield to width of textstring
checkUserNameTextfieldWidth()
}
func checkUserNameTextfieldWidth(){
println("checkUserNameTextfieldWidth \(userName.text) / \(userName.frame.size.width) / \(view.bounds.width)")
// something has to happen here I guess...
}
Thnx!
You have to use UITextView instead of UITextField and apply its sizeThatFits method (see this response)