如何在iOS 7的UITextView中设置属性文本的颜色和对齐方式? [英] How can I set the color and alignment of attributed text in a UITextView in iOS 7?

查看:334
本文介绍了如何在iOS 7的UITextView中设置属性文本的颜色和对齐方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的textViews格式在iOS 6中可以正常工作,但在iOS 7中不再可用.我了解到使用Text Kit进行了很多更改.它真的变得非常令人困惑,我希望有人可以通过帮助我解决诸如此类的简单问题来使它变得更简单.

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.

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

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

我该如何解决?如果这些属性不起作用,那么为什么它们又不存在了?这是textView的创建:

How can I fix this? If these properties take no effect, than why do they exist anymore? Here's the creation of the 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];

推荐答案

很好奇,对于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?

类似的东西:

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;

编辑:首先分配文本,然后更改属性,并以此方式工作.

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

这篇关于如何在iOS 7的UITextView中设置属性文本的颜色和对齐方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆