ios - UITextView 中的简单文本格式

标签 ios uitextview uifont

我正在尝试允许用户复制和粘贴我位于 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/

相关文章:

android - 以编程方式在谷歌地图 flutter 上选择标记

ios - shouldAutorotate 未在 View Controller 上调用

ios - 如何从 iOS-Objective-c 中的联系人框架获取生日和周年纪念标签

iOS 无法识别的选择器发送到实例错误

swift - 嵌入 UIScrollView 时使用自动布局使 UITextView 适应内容时出现问题

iphone - 同一文本中的多种颜色

android - Android java getResource.getIdentfier 存在于 IOS Swift 中吗?

ios - fontSize 不能正常工作

iphone - 获取 UIFont 的 maxWidth

ios - boundingRectWithSize为UILabel添加了额外的底部填充