在设置html格式的文本后,将焦点设置回(使光标闪烁)到UITextView

时间:2013-08-07 04:55:42

标签: ios

我已经实现了像应用程序这样的电子邮件,它将一些数据发送到一个文本视图中提供的电为此,我做了以下事情: 在UITextView上键入的每个字符上,我准备条件字符串并在文本视图下方的Popover中打开一个UITableViewControllerTableViewController显示来自预填充数据源的过滤数据。现在,当用户从TableViewController选择电子邮件时,该名称将传递给父主视图控制器,该控制器同时包含用于UITableView控制器的UITextView和Popover。 Wehn主视图接收电子邮件字符串,它将该文本附加到UITextView。我的问题是,如果我通过将电子邮件附加到当前文本并在becomeFirstResponder上调用UITextView来设置简单文本,那么它将显示光标闪烁。但是如果我使用UITextView setValue:forKey方法设置HTML字符串,其键值为“ ContentToHTMLString ”,则光标在文本末尾不闪烁。如果我再次单击UITextView,则只显示光标。

请就此问题提出建议/意见。请注意,在将文本设置为UITextView之前,我会关闭包含UITableView控制器的弹出控制器。

这里我添加了两个方法,当用户从UITableView中选择电子邮件ID时调用这些方法。如果需要更多详细信息,请与我们联系。请注意,我无法在此处分享完整代码。

    -(NSString*) GetHTMLStringFromString:(NSString*)text
{
    NSString *htmlTag = @"<html>";
    NSString *htmlEndTag = @"</html>";
    NSString *aHrefTag = @"<a href=\"\">";
    NSString *aHrefTagEnd = @"</a>";
    NSString *finalHtmlString = htmlTag;
    NSArray *stringChunks = [text componentsSeparatedByString:@";"];
    for(NSUInteger index = 0; index < stringChunks.count -1;index++)
    {
        finalHtmlString = [finalHtmlString stringByAppendingString:aHrefTag];
        finalHtmlString = [finalHtmlString stringByAppendingString:[stringChunks objectAtIndex:index]];
        finalHtmlString = [finalHtmlString stringByAppendingString:aHrefTagEnd];
        finalHtmlString = [finalHtmlString stringByAppendingString:@";"];
    }
    finalHtmlString = [finalHtmlString stringByAppendingString:htmlTag];
    NSLog(@"\n\nFinal Html String: %@ \n\n",finalHtmlString);
    return finalHtmlString;
}

-(void)HandleUserNameSelected:(NSString*)name
{
    NSLog (@" %s Selected Name: %@\n",__PRETTY_FUNCTION__,name);
    NSString *completeString = self.textView.text;
    //NSArray *namesArray= [completeString componentsSeparatedByString:@";"];
    NSString *userName = @"";
    NSLog(@"Calling FindCriteria from %s",__PRETTY_FUNCTION__);
    userName  = [self FindCriteriaFromString:completeString];
    //NSLog(@"Last username:%@",userName);
    NSRange selectionRange = [completeString rangeOfString:userName options:NSBackwardsSearch];
    //NSRange endselectionRange = [completeString rangeOfString:completeString options:NSBackwardsSearch];
    //NSLog(@" start pos:%d   end pos:%d",selectionRange.location,endselectionRange.length);
    //textView.selectedRange = selectionRange;
    //UITextPosition *start = [textView positionFromPosition:textView.beginningOfDocument offset:selectionRange.location-1];
    //UITextPosition *end = [textView positionFromPosition:textView.beginningOfDocument offset:endselectionRange.length];
    //UITextRange *range = [textView textRangeFromPosition:start toPosition:end];

    NSLog(@"\n\n Total Length %d selction range location %d and length %d \n \n",completeString.length,selectionRange.location,selectionRange.length);
    if((selectionRange.location + selectionRange.length +1) > completeString.length)
    {
        completeString = [completeString stringByReplacingCharactersInRange:selectionRange withString:[name stringByAppendingString:@";"]];
    }
    else
    {
        completeString = [completeString stringByReplacingCharactersInRange:selectionRange withString:name];
    }
    NSString *htmlStr = [self GetHTMLStringFromString:completeString];
    //htmlStr = [htmlStr stringByAppendingString:@" abc"];
    if(self.isSelectionListOpened)
    {
        [self.popController dismissPopoverAnimated:NO];
        self.isSelectionListOpened = NO;
    }
    //self.textView.editable = YES;
   // [self.view becomeFirstResponder];
    //self.textView.text = [self.textView.text stringByAppendingString:@""];
    [self.textView becomeFirstResponder];
    self.textView.editable = YES;
    //NSString *htmlString = [NSString stringWithFormat:@"<html><a href=\"\">%@</a></html>", htmlStr];
    [self.textView setValue:htmlStr forKey:@"ContentToHTMLString"];
    [self.view becomeFirstResponder];
    //self.textView.text = [self.textView.text stringByAppendingString:@""];
    [self.textView becomeFirstResponder];
    self.textView.editable = YES;
   // self.textView.text = completeString;





}

0 个答案:

没有答案