問題描述
如何設置文本光標在 TWTweetComposeViewController 中的位置? (How to set where the text cursor is in TWTweetComposeViewController?)
I am using the TWTweetComposeViewController, and when I set the initial text to the title of the screen, the text cursor appears after the hashtag, and I want to have it appear before the hashtag. Is there a way to do something like cursor=(0,0) or something like that? I looked in the documentation and there's nothing there.
For reference, this is what I'm using.
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc]init];
[tweetSheet setInitialText:[NSString stringWithFormat:@"#%@", self.title]];
tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result){
[self dismissModalViewControllerAnimated:YES];
};
[self presentModalViewController:tweetSheet animated:YES];
‑‑‑‑‑
參考解法
方法 1:
I looked for the exact same thing. It can be done with a little hack. This is the source http://www.sunetos.com/items/2012/06/04/cleanly‑accessing‑the‑uitextview‑field‑in‑twtweetcomposeviewcontroller‑2/
According to the author the app was approved for the app store.
// at an appropriate place in your code,
// add and remove the observer for UIKeyboardWillShowNotification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleKeyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
‑ (UIView *) findFirstResponderInView:(UIView*) view
{
if (view.isFirstResponder) {
return view;
}
for (UIView *subview in view.subviews) {
UIView *result = [self findFirstResponderInView:subview];
if (result) {
return result;
}
}
return nil;
}
‑ (void) handleKeyboardWillShow:(NSNotification *) notification;
{
UIView *fr = [self findFirstResponder:self.tweetSheetViewController.view];
if ([fr isKindOfClass:[UITextView class]]) {
UITextView *tv = (UITextView *) fr;
[tv setSelectedRange:NSMakeRange(0,0)];
}
}
Did implement it like this in my app, so if you need further help let me know.
(by Hudson Buddy、jd291)