Trying to increase the size of a UITextView programmatically depending on what text has been inputted into it. The problem is, textViewDidChange: will only recognize new line characters if I input something after inputting them. For example, I'm trying to input the following:
Hi, my\n name is Jimmy.
This is what I'll get if I print this in NSLog in textViewDidChange:
Log: H
Log: Hi
Log: Hi,
Log: Hi, m
Log: Hi, my
Log: Hi, my (here goes the new line, but it's not recognized)
Log: Hi, my
n (it's recognized on this line, only after something is inputted after it).
If I try to input two newlines, only the first one will be recognized and only after I input the second one. Is this some sort of a bug or am I missing something, is there a way to fix this? This used to work properly on iOS 6.
EDIT --
To clarify the problem a bit more, I do get notified that the text has been changed, the problem is the new text is an empty character instead of new line.
EDIT --
It seems that currently the only proper way to find the fitting size for UITextView is by using its sizeThatFits: method while passing it the bounding size. Also if you're changing UITextView's proportions programmatically, it would be wise to remove any autoresizing masks from it.
Works for me on both iOS 6 and iOS 7. Keep in mind that NSLog will sometimes not show new lines at the end of log like you would expect, so try to log something else after textView.text, for example:
- (void)textViewDidChange:(UITextView *)textView
{
NSLog(#"TextView text changed to: \"%#\"", textView.text);
}
If it really doesn't log new lines, then it does sound like a bug that you should report.
Related
I was wondering how WhatsApp handles the time shown in every message.
For those who don't know:
If the message is very short, the text and time are in the same row. (tine in the end of row)
If the message is long, the time is in the bottom right corner - the text wrapped around it.
if you have some idea how to do it please let me know
Here is the requested screenshot (please ignore checkmarks):
You can use a single attributed string with different text attributes for the main message, and the timestamp. iOS will then internally do all this for you automatically.
Otherwise you can use exclusion paths which are a part of TextKit.
I am currently using iOS 8's provided keyboard extension's textDocumentProxy.documentContextBeforeInput to get the text that a user has entered in order to perform some sort of modification to it.
I noticed that while the method documentContextBeforeInput would work fine for any chucks of texts, as soon as periods "." are inserted, documentContextBeforeInput is only able to return text that occurs after the period.
For example, testing the following text: "This is sentence 1. And this is sentence 2" When using documentContextBeforeInput with placing the cursor at the end, it will only return "And this is sentence 2", and not the entire text that has come before it.
Has anyone also encountered this issue, and if so have you discovered a workaround?
Thanks!
I'm porting a Mac app to the iPhone and I've run into an unexpected problem.
On the Mac there's a text field that is automatically pre-selected (= first responder) when a dialog shows up. The text field shows the text you entered in the field the last time and the text is pre-selected so that if you just start typing it gets cleared away. If you want to edit the existing text instead you just hit the forwards or backwards arrow.
On the iPhone this behavior seems very hard to implement. The text view shows up with the old text and I can even get it to pre-select but whatever I do the result is not quite right.
When I use
[aTextView setMarkedText: myText
selectedRange: newRange];
the text does show up as marked and if I just start typing the old text goes away. However there's no equivalent to the cursor keys on iOS, so I cannot NOT erase the text.. which is hardly the point.
What kind of iOS idiom would be appropriate for giving the option to either edit or overwrite existing text?
Best regards,
Frank
Try this code in your UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
dispatch_async(dispatch_get_main_queue(), ^{
[textView selectAll:self];
});
return YES;
}
I have a UITextView which should auto detect phone numbers. The detection works fine, however if the text is changed the link will call the previous number, even though the actual text has changed. What is the problem? Is this another iOS 7 bug (I have experienced quite a few with UITextView?
This is a bug in iOS 7.
What you can try is resetting the text to nil, before setting the new string.
_textView.text = nil;
_textView.text = yourText;
This will reset the text view and correct phone number detection should work again.
I've got two UITextViews containing data that should be recongised by the data detection, however whilst one works fine on both device and simulator there's one that only works under Simulator. I've attempted trashing the build from my device, cleaning the product down, removing derived data and nothing seems to resolve the inconsistency.
Link detection was enabled within Interface Builder, the data is passed in with a NSString stringWithFormat: formatted string and set with UITextView setText:. Set the same way for both, so there's no difference there, but it just doesn't seem to work correctly for one of them.
EDIT: On the device if I tap on one of the items that should detect as a link, it'll then turn blue and do link detection. I'm not setting any custom fonts or colours that could have an impact.
It appears that the trick is to setScrollable:NO. Seems to fix the problem, although if you need scrolling, I'm not sure what the answer will be...
Apparently this issue is caused by how iOS is currently handling the UITextView links. It is creating an NSAttributedString that turns sections of the text blue ( when the view contains a link ). So I've figured out that this bug only occurs when a link is the first text in the AttributedString, i.e. the first text in the text view. So it's easily fixed by prepending an whitespace to your text before setting it. Or overriding setText to " " + text;
Hope this helps guys