TextView设置文字的颜色
1.今天做项目的时候遇到了一个问题,就是无论我怎么设置textView的文字都不管用呀,半天网上找资料呀,这才解决这个问题呀,其实什么问题只要静下心来就可以好好的实现:
以后是实现的代码:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textview;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableAttributedString * attributedTextString = [[NSMutableAttributedString alloc] initWithString:@"hahahahhahahahhiuhgilgliuglugugguglgjlgjgjgljgjgjuguyguydgasjgfglsa,g和环境的还是觉得还是计划经济健康来了来了健康哈哈哈哈哈哈"];
int length = (int)attributedTextString.length;
//设置的是字的背景颜色
//[attributedTextString addAttribute:NSBackgroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, length)];
//设置字的颜色
[attributedTextString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0 ,length)];
//设置的时字的字体及大小
[attributedTextString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:30] range:NSMakeRange(0, length)];
self.textview.attributedText = attributedTextString;
//给键盘上面加一个done(完成用来取消键盘)
//定义一个toolBar添加一done的完成
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
//设置style
[topView setBarStyle:UIBarStyleBlack];
//定义两个flexibleSpace的button,放在toolBar上,这样完成按钮就会在最右边
UIBarButtonItem * button1 =[[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem * button2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:self action:nil];
//定义完成按钮
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(resignKeyboard)];
//在toolBar上加上这些按钮
NSArray * buttonsArray = [NSArray arrayWithObjects:button1,button2,doneButton,nil];
[topView setItems:buttonsArray];
[self.textview setInputAccessoryView:topView];
}
//取消键盘的消失
-(void)resignKeyboard{
[self.textview resignFirstResponder];
}
}