Joe*_*Joe 33 uitextview nsattributedstring ios textkit
我的textViews的格式在iOS 6中运行良好,但不再在iOS 7中运行.我理解使用Text Kit的内容已经发生了很大变化.它变得非常令人困惑,我希望有人可以通过帮助我做一些简单的事来帮助理顺它.
我的静态UITextView最初被分配了一个值textColor和textAlignment属性.然后我做了一个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];
Run Code Online (Sandbox Code Playgroud)
jlh*_*tas 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;
Run Code Online (Sandbox Code Playgroud)
编辑:首先分配文本,然后更改属性,这样就可以了.
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];
Run Code Online (Sandbox Code Playgroud)