Skip to content

Instantly share code, notes, and snippets.

@hanfengs hanfengs/cache.m
Last active Sep 18, 2019

What would you like to do?
TSStemLabel *stem = [[TSStemLabel alloc] init];
stem.numberOfLines = 0;
stem.text = @" $$CCTV$$ reported that every year Chinese people throw away a lot of food. Luckily, a number of people have realized the importance of saving food. {Last November}, Li Hong, a waitress in a restaurant in $$Nanjing$$, lost her job because she took some leftover food home for her son. Many people stood by her side.@What should we do in our daily life to waste less food? Here are two tips:@1. Don’t order too much food. If you can’t eat all the food you order, take the rest of it home.@2. Don’t keep too much food at home, especially for {vegetables} and fruit.In my opinion, everybody must save food.";
CGFloat height = stem.stemHeight;
[self addSubview:stem];
[stem mas_makeConstraints:^(MASConstraintMaker *make) {
#pragma mark- Label中添加textField
- (void)setupSentence:(TSSentencePracticeModel *)model{
// ----------------容器muText----------------------
if (![model.stems containsString:@"_____"] || !model.answer) {
NSArray *stemArr = [model.stems componentsSeparatedByString:@"_____"];
NSArray *tempArr = [model.answer componentsSeparatedByString:@", "];
NSMutableArray *answerArr = [NSMutableArray array];
for (NSString *str in tempArr) {
NSString *transformedAnswer = [NSString stringWithFormat:@"%@",[str stringByReplacingOccurrencesOfString:@" " withString:@"S"]];
[answerArr addObject:transformedAnswer];
NSString *stem0 = stemArr[0];
NSMutableString *muText = [[NSMutableString alloc] initWithString:[stem0 stringByReplacingOccurrencesOfString:@"@" withString:@"\n"]];
for (NSInteger i = 0; i < answerArr.count; i++) {
[muText appendString:answerArr[i]];
NSString *stems = stemArr[i + 1];
[muText appendString:[stems stringByReplacingOccurrencesOfString:@"@" withString:@"\n"]];
// self.lbl_title.hidden = NO;
// self.lbl_title.text = muText;
NSMutableAttributedString *mutabStr = [self getMutableAttributedStringWithString:muText];
mutabStr.yy_font = KFont_Bold(18);
mutabStr.yy_color = KColorLearnTitle;
YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(self.lbl_title.bounds.size.width, MAXFLOAT)];
YYTextLayout *textLayout = [YYTextLayout layoutWithContainer:container text:mutabStr];
stemLab = [YYLabel new];
stemLab.numberOfLines = 0;
// stemLab.font = KFont_Medium(20);
// stemLab.backgroundColor = kColorMainStyle;
// stemLab.textColor = [UIColor blackColor];
stemLab.textLayout = textLayout;
stemLab.textVerticalAlignment = YYTextVerticalAlignmentTop;
[self addSubview:stemLab];
[stemLab mas_makeConstraints:^(MASConstraintMaker *make) {
self.textFieldArr = [NSMutableArray arrayWithCapacity:0];
NSString *stemStr0 = stemArr[0];
NSInteger loc = stemStr0.length;
for (NSInteger i = 0; i < answerArr.count; i++) {
NSString *stemStr = stemArr[i + 1];
NSString *answerStr = answerArr[i];
CGRect textFiedSize = [textLayout rectForRange:[YYTextRange rangeWithRange:NSMakeRange(loc, answerStr.length)]];
UITextField *tf = [[UITextField alloc] initWithFrame:textFiedSize];
// tf.adjustsFontSizeToFitWidth = YES;
tf.font = KFont_Bold(17);
tf.textColor = kColorGapTFText;
tf.tintColor = kColorMainStyle;
tf.borderStyle = UITextBorderStyleNone;
tf.textAlignment = NSTextAlignmentCenter;
tf.autocapitalizationType = UITextAutocapitalizationTypeNone;
tf.backgroundColor = KColorLearnFlowLightBG;
tf.delegate = self;
[stemLab addSubview:tf];
UIView * onLine = [[UIView alloc] initWithFrame:CGRectMake(0, tf.frame.size.height, tf.frame.size.width, 1)];
onLine.backgroundColor = kColorView_Light;
[tf addSubview:onLine];
loc += (stemStr.length + answerStr.length);
[self.textFieldArr addObject:tf];
-(NSMutableAttributedString *)getMutableAttributedStringWithString:(NSString *)string
NSMutableAttributedString *arrstr2 = [[NSMutableAttributedString alloc] initWithString:string];
[arrstr2 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:20] range:arrstr2.yy_rangeOfAll];
[arrstr2 addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:arrstr2.yy_rangeOfAll];
return arrstr2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.