34

我的 textViews 的格式在 iOS 6 中运行良好,但在 iOS 7 中不再适用。我理解 Text Kit 的大部分底层内容都发生了变化。它变得非常令人困惑,我希望有人可以通过帮助我解决如此简单的事情来帮助理顺它。

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

我怎样才能解决这个问题?如果这些属性不起作用,那么它们为什么会存在呢?下面是 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];
4

1 回答 1

67

奇怪的是,这些属性被考虑到UILabel但不是UITextView

你为什么不只是添加颜色和对齐属性到属性字符串,就像你使用字体的方式一样?

就像是:

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;

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

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];
于 2013-10-12T23:21:11.897 回答