Setting UITextView frame to content size no longer works in Xcode 5 - objective-c

The code snippet below worked to resize a UITextView frame to it's content height, before installing Xcode 5 but it doesn't work since the upgrade:
CGRect frame = _textView.frame;
frame.size.height = _textView.contentSize.height;
_textView.frame = frame;
I've searched and haven't found the fix. Any thoughts?

There's new stuff for this on iOS 7.
To get the "fitted" size used by the text view after it's updated its text, call usedRectForTextContainer: on the textView's layoutManager property, passing the textView's textContainer property as an argument.
Word of warning about scrolling: Be advised, though, that changing the frame size of a text view after it has updated it's text can have unexpected visual bugs if scrolling is disabled on your text view. If this happens, set scrolling enabled before editing the text of the text view, then disabling it after it's updated (if you need scrolling to remain disabled).

To work in iOS 7 (Xcode 5), just:
Give the entire space to receive the text, by setting:
[myTextView setScrollEnabled:YES];
Pass the real text:
myTextView.text = theTextVariable; or myTextView.text = #"The text...";
Autoresize textView:
[myTextView sizeToFit];
Disable scroll:
[myTextView setScrollEnabled:NO];
P.S: myTextView can be use also as self.myTextView or _myTextView
And have fun!

I believe the correct way to force a textView to update its contentSize is by calling
[textView layoutIfNeeded]
However, in iOS 7.0 and 7.1 this seems still not to work reliably unless you first set
textView.layoutManager.allowsNonContiguousLayout = false;
It's not clear to me whether this is a bug or not since I can't really find a good explanation of what "non-contiguous layout" even means.
(My personal use case is updating textView.text = newValue programmatically, then trying to resize the textView appropriately.)

[textView sizeToFit];
Is what you need.
All you need to do is make sure that:
[textView setScrollEnabled:YES];
BEFORE you set the UITextView text content.
You can then:
[textView sizeToFit];
[textView setScrollEnabled:NO];
After you've set the text. Same as writing your own bling function or employing complicated bounding rect methods. Why use something so complicated when the solution is as simple as three lines?
That said, wrap those functions like so:
- (void) setText:(NSString *)theTextToAdd andResizeTheDamnTextView:(UITextView *)textView {
[textView setScrollEnabled:YES];
[textView setText:theTextToAdd];
[textView sizeToFit];
[textView setScrollEnabled:NO];
}
And define it in a per-file or global basis to avoid having to manually write or copy/paste the four lines and call it every time. Just call it as:
[yourTextViewIvar setText:#"DUMMY STRING" andResizeTheDamnTextView:yourTextViewIvar];
If that doesn't work:
[yourTextViewIvar setText:[self setText:#"DUMMY STRING" andResizeTheDamnTextView:yourTextViewIvar]];
And you'll be golden.
I think..
That's Pseudocode. Just FYI.

A easier solution is use that:
[textViewExample sizeToFit];
This work for me.

Related

UITextField IOS7 not setting font color during input

I have some simple code that worked fine under iOS6. In a UITextField when the event editingDidBegin arrived I executed a simple piece of code that changed the color of the text that was entered:
textField.textColor = [UIColor redColor];
Although this code is executed, the text color does not change to red, but remains unchanged at its default color as the text is being entered.
If I call this method on editingDidEnd, the text is entered in the default color, but changes to red when editingDidEnd kicks off the same piece of code:
textField.textColor = [UIColor redColor];
It seems as though I can not change the textColor property when the textField is the first responder.
Any help here would be greatly appreciated.
You should do some subclassing for that
Here is a good solution that works both on ios6 and ios7
http://www.brightec.co.uk/blog/how-change-colour-uitextfields-placeholder-text-ios7-and-still-support-ios6
I found another way of accomplishing this. I used the UITextField delegate method and added the following code. Seems like my original method should have worked moving to IOS7, but this now works fine.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField.textColor = [UIColor redColor];
return YES;
}
[textField setValue:[UIColor redColor] forKeyPath:#"_placeholderLabel.textColor"];
This will change the place holder color of UITextField,

Changing Font in UITextView does not work in iOS7

I have a UITextView with text in it, the View is in a UITableViewCell. I noticed that the font was not quite the same on iOS7 as with iOS6, noting it was set to "system" I decided to specify the exact font/size.
It appeared nothing happened so I thought I would do a better test (big font not used anywhere), like this in my "CellForRowAt....";
cell.newsItemDescription.font = [UIFont fontWithName:#"didot" size:20];
cell.newsItemDescription.text = newsDescriptions[indexPath.row];
In iOS6 it comes out like this;
In iOS7 it comes out like this;
It happens in just a few places in the app but it is very annoying, can't figure out why? I am fast getting to the point where I may use the iOS7 Font/ Size throughout the app.
Some extra info;
The UITextView is resized per cell along with the cell (using springs/struts, i.e. no Auto Layout) and HeightForRow...
The font was setup in Storyboard originally (as system)
This is the same on devices and Simulator
I have a strange behavior in iOS 7. Font is smaller than I expect, if I was set it in to the xib.
If I set font after setting the text it's works for me. Otherwise font is smaller.
Try this:
cell.newsItemDescription.text = newsDescriptions[indexPath.row];
cell.newsItemDescription.font = [UIFont fontWithName:#"didot" size:20];
I had the same issue today with UITextView, and while reading AlKozin's answer, I remembered something: somewhere, sometime I read that since iOS 7, the best practice to set font styles is to set them after the View has loaded. In other words, if I set everything in viewDidLoad: , nothing happens. You have to set up the font of your UITextView in viewWillAppear:, like this:
-(void)viewWillAppear:(BOOL)animated
{
self.myTextView.font = [UIFont fontWithName:#"Baskerville-Italic" size:18.0];
}
I ran into a weird bug (Xcode 7.2.1), where unchecking "Selectable" in IB was causing the UITextView to not adhere to the font settings specified through IB.
This is same issue I was facing.
Making UITextview "selectable" from storyboard will work.
Setting textview.font worked fine for me on iOS 7, but none of these answers, or other similar answers on other SO pages worked for me on iOS 8. The only way I was able to get it working was to use an NSAttributedString.
NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
[pStyle setAlignment:NSTextAlignmentCenter];
tv.attributedText = [[NSAttributedString alloc]
initWithString:text
attributes:#{ NSForegroundColorAttributeName: color,
NSFontAttributeName: [UIFont systemFontOfSize:fontSize],
NSParagraphStyleAttributeName: pStyle,
} ];

UITextView setTextColor changes layout in UITableViewCell

I am trying to use the solution on the following page:
UITextView highlightedTextColor or similar option?
However, I am finding that when I call setTextColor on my UITextView and set the color to anything other than Color blackColor, the content in the UITextView appears shifted, and the color I do set it to doesn't take.
Playing around, I can repeat this behaviour by modifying the following initialization code in my UITableViewCell:
_notesTextView = [[UITextView alloc] initWithFrame:CGRectZero];
[_notesTextView setFont:[UIFont systemFontOfSize:12.0]];
[_notesTextView setTextColor:[UIColor redColor]];
[_notesTextView setUserInteractionEnabled:NO];
[self.contentView addSubview:_notesTextView];
The code above will cause the textView to display offset from what I expect, as compared to when I leave the color defaulted or set to blackColor, and the text doesn't show as red either.
This is so weird - any idea what could be wrong?
Do Not Use CGRectZero as it initiates the frame to some value you might know depending upon situation so if you have created a custom UITableViewCell then just set the frames in init or
-(void)layoutSubViews
{
}
I've confirmed that the textColor does indeed shift the text position (up and down for me). I've also tried it with initWithFrame(50, 20, 250, 31), and still experience the same problem.
What I've discovered fixes this issue is using the property textAlignment and setting it to one of the values: UITextAlignmentCenter, UITextAlignmentLeft, UITextAlignmentRight.

How to toggle autocorrectionType on and off for an existing UITextView

I have a UITextView in my iPhone app for which I want to be able to toggle the autocorrectionType.
When a user is editing the text view, I want the autocorrectionType to be set to UIAutocorrectionTypeYes.
When the text view is not being edited, I want the autocorrectionType to be set to UIAutocorrectionTypeNo (because I don't want any of the red-dotted underlines generated by autocorrection to be visible)
Simply toggling the autocorrectionType like this:
myTextView.autocorrectionType = UITextAutocorrectionTypeYes;
myTextView.autocorrectionType = UITextAutocorrectionTypeNo;
Doesn't seem to work.
Are there limitations on when I can toggle the autocorrectionType or in which situations the changed autocorrectionType takes effect?
EDIT:
To clarify:
There are no issues setting the autocorrectionType when initializing the UITextView. The problem arises when I want to change the autocorrectionType for an existing UITextView. In my case I want to give users the advantage of autocorrection when they edit a UITextView, but don't want any spelling errors pointed out with the red-dotted underlines when the UITextView is not being edited - in part because I am also exporting a UIView containing the UITextView as an image.
The problem is that just changing the value of my UITextView's autocorrectionType property doesn't work.
Here's an easy way to do this for the image export scenario :
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
// Turn spell check on
textView.autocorrectionType = UITextAutocorrectionTypeYes;
return YES;
}
- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
// Turn spell check off and clean up red squiggles.
textView.autocorrectionType = UITextAutocorrectionTypeNo;
NSString *currentText = textView.text;
textView.text = #"";
textView.text = currentText;
return YES;
}
You can try to first hide the keyboard first and then displaying it again. Also update the uitextview. If [UITextView setNeedsDisplay] doesn't work for you, try [UITextView insertText:] and then [UITextView deleteBackward]
[textView resignFirstResponde];
textView.autocorrectionType = UITextAutocorrectionTypeNo;
[textView becomeFirstResponder];
[textView setNeedsDisplay];
or
[textView insertText:#" "];
[textView deleteBackward];
In addition to changing the autocorrection type to UITextAutoCorrectionNo, the UITextView must be forced to reevaluate its correction state. setNeedsRedraw is insufficient but setting the text to itself, e.g.
textView.autocorrectionType = UITextAutocorrectionTypeNo;
textView.text = textView.text;
makes the red dashed lines go away. NOTE: this workaround relies on undocumented behavior and is not guaranteed to work on future iOS releases.
Try calling -setNeedsDisplay on the text view after you've changed the autocorrectionType. This will force the text view to redraw and will hopefully clear the red underlines.
myTextView.autocorrectionType = UITextAutocorrectionTypeNo;
[myTextView setNeedsDisplay];
func activateTextViewAutocorrection() {
textView.autocorrectionType = .default
textView.reloadInputViews()
}
func deactivateTextViewAutocorrection() {
textView.autocorrectionType = .no
textView.reloadInputViews()
}

Creating a label using NSTextField is blurry

I'm trying to create a label programmatically using NSTextField, but it comes out blurry: screenshot
This is my code so far:
NSTextfield *textfield = [[NSTextField alloc] initWithFrame:NSMakeRect(5,5,150,20)];
[texField setStringValue:#"some text here"];
[textField setEditable:NO];
[textField setSelectable:NO];
[textField setBordered:NO]
[textField setDrawsBackground:NO]
I've traced the problem down to the setDrawsBackground line. I've also tried using [textField setBackgroundColor:[NSColor clearColor] as well, but no luck.
By the way, I've adding to a textField to the subview of a view that is a subview of a scrollview. I've also playing with isOpaque on all the view levels, but no luck there again.
Any help is greatly appreciated.
If you have no background (including clear) and your text is a subview of any layer-backed superview (you've turned on "wants layer" in code or in IB to allow animations/transitions), you'll get blurry text. You have to choose either no layer backed view or a label with a solid background color.
I had the same problem, but I have solved it by:
textfield.canDrawSubviewsIntoLayer = true
Make sure you're setting the frame of your NSTextField to something with all integer values.
Use roundf() if necessary.
I was getting a blurry NSTextField, and neither adding a solid background nor removing Core Animation layers from my view hierarchy were options for me. I noticed I was setting the frame of this text field to something with a Y value of 4.5, so the following changes fixed the issue for me:
Blurry label:
_label.frame = NSOffsetRect(_labelFrame,
-0.5 * (someRect.size.width + someConstant),
0.0);
No blur:
_label.frame = NSOffsetRect(_labelFrame,
roundf(-0.5 * (someRect.size.width + someConstant)),
0.0);
(In the above examples, _labelFrame and someRect are NSRects, and someConstant is a CGFloat. As you can see, the calculation I was doing in the second line of the first example was passing a non-integer value to NSOffsetRect).
Since this was my first time subclassing NSView, I had put the above code in the drawRect method instead of the initWithFrame method. I did this because I was following one of the sample applications from Apple's Dev site.
This was also causing my CPU usage to spike when I was scrolling
If you created it via an XIB and it is blurry, I found the fix:
[textField setStringValue:#""];
If I comment this out it goes blurry; if put back it's crystal clear.
Try this below:
textField.drawsBackground = true
textField.backgroundColor = NSColor.white.withAlphaComponent(.leastNormalMagnitude)
Out of the box, but worth to mention. I have wasted by entire day debugging the issue. I am using a non-apple external monitor and this is where the issue is identified. Once i open the app in Mac book pro, it is perfectly fine.
So, the Samsung monitor which i am using might be non-retina.
Simply, add
CanDrawConcurrently = true
property from InterfaceBuilder