我正在尝试允许用户复制和粘贴我位于 UITextView 中的文本内容。我有一些标题文本,它是粗体,然后是常规的非粗体文本。有什么方法可以设置 UITextView 以便单个全选复制/粘贴对粗体标题和非粗体内容文本都起作用?
目前,我正在添加另一个 UITextView 并将其样式设置为粗体,但这需要 2 个选择/全选复制/粘贴操作:
txtView = [[UITextView alloc] initWithFrame:CGRectMake(0,60,280,300)];
[txtView setFont:[UIFont fontWithName:@"ArialMT" size:14]];
txtView.text = word.definition;
txtView.editable = NO;
txtView.scrollEnabled = YES;
[txtView scrollRangeToVisible:NSMakeRange([txtView.text length], 0)];
txtView.minimumZoomScale = 1;
txtView.maximumZoomScale = 10;
UITextView *wordTitle = [[UITextView alloc] initWithFrame:CGRectMake(0, 10, 320, 50)];
[wordTitle setFont:[UIFont boldSystemFontOfSize:14]];
wordTitle.text = word.term;
wordTitle.textAlignment = UITextAlignmentCenter;
[detailsViewController.view addSubview:wordTitle];
[detailsViewController.view addSubview:txtView];
[txtView release];
txtView = nil;
[htmlView release];
htmlView = nil;
[scrollView release];
scrollView = nil;
[[self navigationController] pushViewController:detailsViewController animated:YES];
[detailsViewController release];
最佳答案
我通过使用 UIWebView 并传入 HTML 字符串解决了这个问题,如下所示:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//...more code here
NSString *htmlDefinition = [word.definition stringByReplacingOccurrencesOfString:@"\n"
withString:@"<br />"];
NSString *htmlString = [NSString stringWithFormat:@"<h2>%@</h2><p>%@</p>",[word term],htmlDefinition];
[htmlView loadHTMLString:htmlString baseURL:nil];
scrollView.contentSize = [htmlView bounds].size;
[scrollView addSubview:htmlView];
[detailsViewController.view addSubview:htmlView];
[htmlView release];
htmlView = nil;
}
关于ios - UITextView 中的简单文本格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9199307/