UITextView 中的简单文本格式

发布于 2025-01-03 17:43:45 字数 1116 浏览 2 评论 0原文

我试图允许用户复制并粘贴位于 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];

I am trying allow users to copy and paste my text content that sits in a UITextView. I have some title text, which is bold, followed by regular, unbolded text. Any way to set up a UITextView such that a single select-all copy/paste will work on both bold title and non-bold content text?

Currently, I am adding another UITextView and styling it as bold but this requires 2 select/select-all copy/paste operations:

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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

路还长,别太狂 2025-01-10 17:43:45

我通过使用 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;


}

I solved this by using a UIWebView and passing in a HTML string like so:

- (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;


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文