文本视图中的文本编辑 xcode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14641435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
text editing in text view xcode
提问by user1845209
I have a text view in which text is displayed like this:
我有一个文本视图,其中的文本显示如下:
how are you?
Fine
Now if i set font for text view, then the same font is displayed for the two lines(ques and answer), however i want question to be displayed in one font and answer in some other font. How can i do this?
现在,如果我为文本视图设置字体,那么两行(问题和答案)将显示相同的字体,但是我希望问题以一种字体显示,并以其他字体显示。我怎样才能做到这一点?
I set font like this:
我这样设置字体:
textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 80, 300, 440)];
textView.layer.borderColor = [UIColor blackColor].CGColor;
[textView setFont:[UIFont fontWithName:@"TimesNewRomanPS-ItalicMT" size:14]];
textView.layer.borderWidth = 1.0;
textView.autoresizesSubviews = YES;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:textView];
Thanks in advance!!
提前致谢!!
回答by Srikar Appalaraju
From the UITextView class reference:
In iOS 6 and later, this class supports multiple text styles through use of the attributedText property. (Styled text is not supported in earlier versions of iOS.) Setting a value for this property causes the text view to use the style information provided in the attributed string. You can still use the font, textColor, and textAlignment properties to set style attributes, but those properties apply to all of the text in the text view.
This class does not support multiple styles for text. The font, color, and text alignment attributes you specify always apply to the entire contents of the text view. To display more complex styling in your application, you need to use a UIWebView object and render your content using HTML.
在 iOS 6 及更高版本中,此类通过使用 attributesText 属性支持多种文本样式。(iOS 的早期版本不支持样式文本。)为此属性设置值会导致文本视图使用属性字符串中提供的样式信息。您仍然可以使用 font、textColor 和 textAlignment 属性来设置样式属性,但这些属性适用于文本视图中的所有文本。
此类不支持多种文本样式。您指定的字体、颜色和文本对齐属性始终适用于文本视图的全部内容。要在您的应用程序中显示更复杂的样式,您需要使用 UIWebView 对象并使用 HTML 呈现您的内容。
So you cannot have two on the same page for iOS 5 or less because it is not supported. Just use a webview and an HTML file. for iOS6 maybe you can try using attributedText
property of UITextView. This is available under iOS 6. Never tried it though.
因此,对于 iOS 5 或更低版本,您不能在同一页面上有两个,因为它不受支持。只需使用 webview 和 HTML 文件。对于 iOS6,也许您可以尝试使用attributedText
UITextView 的属性。这在 iOS 6 下可用。但从未尝试过。
Or have 2 different UITextView's (its ugly but thats what it is).
或者有 2 个不同的 UITextView(它很丑,但就是这样)。
回答by Kyle Fang
I'm guessing you wanted to create a chatroom like app?
我猜你想创建一个类似应用程序的聊天室?
if so, I recommend make it a UITableView. And then make different Cells to match different styles.
如果是这样,我建议将其设为 UITableView。然后制作不同的Cell来搭配不同的风格。
回答by josh-fuggle
You can use attributed strings to achieve this, for example:
您可以使用属性字符串来实现这一点,例如:
NSMutableAttributedString *para1 = [[NSMutableAttributedString alloc] initWithString:@"How are you?"];
NSMutableAttributedString *para2 = [[NSMutableAttributedString alloc] initWithString:@"\nFine"];
[para2 setAttributes:@{ NSForegroundColorAttributeName : [UIColor blueColor]} range:NSMakeRange(0, para2.length)];
[para1 insertAttributedString:para2 atIndex:para1.length];
self.textLabel.attributedText = para1;
Or with a single attributed string:
或者使用单个属性字符串:
NSMutableAttributedString *para1 = [[NSMutableAttributedString alloc] initWithString:@"How are you?\nFine"];
// Get the range of the last line in the string
__block NSRange range;
[para1.mutableString enumerateLinesUsingBlock:^(NSString *line, BOOL *stop) {
range = [para1.mutableString rangeOfString:line];
}];
[para1 setAttributes:@{ NSForegroundColorAttributeName : [UIColor blueColor] } range:range];
self.textLabel.attributedText = para1;
Both examples result in:
这两个例子都导致: