如何在 iOS 7 的 UITextView 中设置属性文本的颜色和对齐方式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19339879/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:18:52  来源:igfitidea点击:

How can I set the color and alignment of attributed text in a UITextView in iOS 7?

iosuitextviewnsattributedstringtextkit

提问by Joe

The formatting of my textViews worked fine in iOS 6, but no longer in iOS 7. I understand with Text Kit much of the under the hood stuff has changed. It's become really quite confusing, and I'm hoping someone can help straighten it out a bit by helping me with something as simple as this.

我的 textViews 的格式在 iOS 6 中运行良好,但在 iOS 7 中不再有效。我理解 Text Kit 的大部分底层内容已经改变。它变得非常令人困惑,我希望有人可以通过帮助我解决像这样简单的事情来帮助理顺它。

My static UITextView originally was assigned a value for it's textColorand textAlignmentproperties. Then I made a NSMutableAttributedString, assigned it an attributes, then assigned it to the textView's attributedTextproperty. The alignment and color no longer take effect in iOS 7.

我的静态 UITextView 最初为其textColortextAlignment属性分配了一个值。然后我做了一个NSMutableAttributedString,为其分配了一个属性,然后将其分配给了 textView 的attributedText属性。对齐方式和颜色在 iOS 7 中不再生效。

How can I fix this? If these properties take no effect, than why do they exist anymore? Here's the creation of the textView:

我怎样才能解决这个问题?如果这些属性不起作用,那么它们为什么还存在?下面是 textView 的创建:

UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)];
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];

NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"];
UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60];
[title addAttribute:NSParagraphStyleAttributeName value:font range:NSMakeRange(0, title.length)];
titleView.attributedText = title;

[self.view addSubview:titleView];

回答by jlhuertas

Curious, the properties are taken into account for UILabelbut not for UITextView

好奇,属性被考虑到UILabel但不考虑UITextView

Why don't you just add attributes for color and alignment to the attributed string similar to the way you are doing with the font?

为什么不像处理字体那样直接为属性字符串添加颜色和对齐属性?

Something like:

就像是:

NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"];
UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];

//add color
[title addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, title.length)];

//add alignment
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentCenter];
[title addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, title.length)];

titleView.attributedText = title;

Edit:Assign the text first, then change the properties and this way it works.

编辑:首先分配文本,然后更改属性,这样就可以工作了。

UITextView *titleView = [[UITextView alloc]initWithFrame:CGRectMake(0, 90, 1024, 150)];

//create attributed string and change font
NSMutableAttributedString *title = [[NSMutableAttributedString alloc]initWithString:@"Welcome"];
UIFont *font = [UIFont fontWithName:@"Avenir-Light" size:60];
[title addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, title.length)];

//assign text first, then customize properties
titleView.attributedText = title;
titleView.textAlignment = NSTextAlignmentCenter;
titleView.textColor = [UIColor whiteColor];