Giter VIP home page Giter VIP logo

凡几多's Projects

-pick-word-change-color icon -pick-word-change-color

用UITextView实现: IOS7新特性:UITextView的textStorage属性 UITextView的textStorage属性继承自NSMutibleAttributeString,所以,你可以通过修改textStorage的值来更改UItextView某一字段的字体或颜色。 例如: [self.myTextView.textStorage addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, 3)]; 以上是将UITextView的前面三个字符的字体改成红色。 //英语TextView,智慧化学习取词用 UITextView *lyricEnPickLabel = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)]; [lyricEnPickLabel setTag:3000]; lyricEnPickLabel.editable = NO;//不可编辑 lyricEnPickLabel.scrollEnabled = NO; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(screenTouchWordAndChangeColor:)]; [lyricEnPickLabel addGestureRecognizer:tap]; /** * 屏幕取词加变色 */ -(void)screenTouchWordAndChangeColor:(UITapGestureRecognizer*)tap{ UITextView *textView = (UITextView*)tap.self.view; if (tap.state == UIGestureRecognizerStateRecognized) { CGPoint tappedPoint = [tap locationInView:textView]; NSUInteger idx = [textView closestCharacterIndexToPoint:tappedPoint]; UITextGranularity granularity =UITextGranularityWord; UITextRange* textRange = [textView selectionRangeWithGranularity:granularity atIndex:idx]; NSLog(@"tappedPoint = %@ idx = %lu granularity = %ld textRange = %@",NSStringFromCGPoint(tappedPoint),(unsigned long)idx,(long)granularity,textRange); self.selectWord = [textView textInRange:textRange]; NSLog(@"取词: %@",self.selectWord); //转化成NSRange NSInteger startOffset=[textView offsetFromPosition:textView.beginningOfDocument toPosition:textRange.start]; NSInteger endOffset=[textView offsetFromPosition:textView.beginningOfDocument toPosition:textRange.end]; _selectionRange = NSMakeRange(startOffset, endOffset - startOffset); NSLog(@"_selectionRange = %@",NSStringFromRange(_selectionRange)); //NSNumber *wordLocation = [NSNumber numberWithInt:_selectionRange.location]; NSString *wordLocation = [NSString stringWithFormat:@"%lu",(unsigned long)_selectionRange.location]; if (_selectRangeDic.count > 0) {//如果取词字典里有数据 NSMutableDictionary *tempDic = _selectRangeDic;//把取词字典传给临时字典 int i = 0; for (NSString *wordKey in [tempDic allKeys]) { i++; if ([wordKey isEqualToString:wordLocation]) {//如果取词字典里已经存在此单词 [_selectRangeDic removeObjectForKey:wordLocation];//从字典里移除此元素 //恢复单词原本颜色 [textView.textStorage addAttribute:NSForegroundColorAttributeName value:[Constants highLightColor] range:_selectionRange];//前景色 break; } if (tempDic.count == i) {//如果循环到了最后一个,说明取词字典不存在此单词 [_selectRangeDic setObject:self.selectWord forKey:wordLocation];//把取词的位置与内存存入字典 //变为红色 [textView.textStorage addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:_selectionRange];//前景色 } } }else{//如果取词数组没数据 [_selectRangeDic setObject:self.selectWord forKey:wordLocation]; //变为红色 [textView.textStorage addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:_selectionRange];//前景色 //[textView.textStorage addAttribute:NSBackgroundColorAttributeName value:[UIColor clearColor] range:_selectionRange ];//背景色 } } NSString * sepratorString = @" ,,。.?!:\"“”-()'‘"; NSCharacterSet * sepratorSet = [NSCharacterSet characterSetWithCharactersInString:sepratorString]; //根据标点符号拆分成数组 NSMutableArray * splitStr = [textView.text componentsSeparatedByCharactersInSet:sepratorSet]; NSLog(@"splitStr拆分以后 === %@",splitStr); //去掉拆分以后的数组中出现的""元素,不知道怎么出现的""元素。 NSArray *array = [NSArray arrayWithArray:splitStr]; for (NSString *item in array) { if ([@"" isEqualToString: item]) { [splitStr removeObject:item]; } } NSLog(@"splitStr === %@",splitStr); _wordCount = (int)splitStr.count;//单词总数 //得分 _wisdomLearnScore = 100 - _selectRangeDic.count * 100/_wordCount; NSLog(@"智慧化学习得分为:%ld",(long)_wisdomLearnScore); UITableViewCell *cell = (UITableViewCell *)[_repeatView.repeatTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:sen_num]]; if (_selectRangeDic.count > 0) {//如果取词数组有数据,则显示提交按钮 //显示提交按钮 for (UIButton *submitBtn in [cell.contentView subviews]) { if (submitBtn.tag == 5) { submitBtn.hidden = NO; } } }else{ //隐藏提交按钮 for (UIButton *submitBtn in [cell.contentView subviews]) { if (submitBtn.tag == 5) { submitBtn.hidden = YES; } } } _userAnswerStr = @""; //取词拼接成字符串 for (NSString *wordKey in [_selectRangeDic allKeys]) { NSString *wordValue = [_selectRangeDic objectForKey:wordKey]; _userAnswerStr = [NSString stringWithFormat:@"%@ %@",_userAnswerStr,wordValue]; } NSLog(@"%@",_userAnswerStr); for (UILabel *nLabel in [cell.contentView subviews]) { if (nLabel.tag == 4){//请在句中点击未听懂的单词/展开时的中文 nLabel.text = _userAnswerStr; [nLabel setTextColor:[UIColor redColor]]; } } }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.