UITextView中的简单文本格式

时间:2012-02-08 18:32:05

标签: ios uitextview uifont

我正在尝试允许用户复制和粘贴位于UITextView中的文本内容。我有一些标题文本,这是粗体,然后是常规的,未加粗的文本。有什么方法可以设置UITextView,以便单个select-all复制/粘贴可以同时使用粗体标题和非粗体内容文本?

目前,我正在添加另一个UITextView并将其设置为粗体,但这需要2个select / select-all复制/粘贴操作:

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

1 个答案:

答案 0 :(得分:0)

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


}