0

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





}
4

0 回答 0