您的位置:首页 > 产品设计 > UI/UE

UITextView改变选中字的颜色

2016-04-22 15:51 176 查看
<pre name="code" class="objc">- (void)viewDidLoad {
[super viewDidLoad];
UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle:@"改变颜色" action:@selector(changeColor)];
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:menuItem, nil]];
[[UIMenuController sharedMenuController] setTargetRect:self.textView.frame inView:self.view];
[[UIMenuController sharedMenuController] setMenuVisible:YES animated: YES];
// Do any additional setup after loading the view, typically from a nib.
}



-(void)changeColor{
NSLog(@"改变颜色");
NSString *selectString = [self.textView.text substringWithRange:self.textView.selectedRange];
NSLog(@"选中文字为:%@",selectString);
NSMutableAttributedString *abString = [[NSMutableAttributedString alloc]initWithAttributedString:self.textView.attributedText];
[abString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:self.textView.selectedRange];
self.textView.attributedText = abString;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: