赞
踩
主要原理就是通过设置yy_baselineOffset和yy_textUnderline = [YYTextDecoration decorationWithStyle:YYTextLineStyleSingle];
形成字体偏移,但是下划线保持原位置,来确保下划线与文本之间的偏移量,
**注意!**如果直接设置yy_underlineStyle = NSUnderlineStyleSingle的话,下划线会与文本同时偏移。
// 创建一个NSAttributedString UIFont *font = [UIFont systemFontOfSize:14]; NSMutableAttributedString *Text = [[NSMutableAttributedString alloc] init]; YYLabel *label = [[YYLabel alloc] initWithFrame:CGRectMake(0, 100, 1000, 100)]; label.numberOfLines = 5; label.font = font; NSMutableAttributedString *attr1 = [[NSMutableAttributedString alloc] initWithString:@"粗斜体下滑线偏移"]; attr1.yy_textUnderline = [YYTextDecoration decorationWithStyle:YYTextLineStyleSingle]; attr1.yy_font = [UIFont fontWithDescriptor:[[font fontDescriptor] fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold] size:font.pointSize]; attr1.yy_maximumLineHeight = 22; attr1.yy_minimumLineHeight = 22; CGFloat baselineOffset = (22 - attr1.yy_font.lineHeight) / 4; attr1.yy_baselineOffset = @(-baselineOffset); attr1.yy_textGlyphTransform = CGAffineTransformMake(1, 0, tanf(10 * (CGFloat)M_PI / 180), 1, 0, 0); NSMutableAttributedString *attr2 = [[NSMutableAttributedString alloc] initWithString:@"普通文本+下划线偏移"]; attr2.yy_font = font; attr2.yy_maximumLineHeight = 22; attr2.yy_minimumLineHeight = 22; baselineOffset = (22 - attr2.yy_font.lineHeight) / 4; attr2.yy_baselineOffset = @(baselineOffset); attr2.yy_textUnderline = [YYTextDecoration decorationWithStyle:YYTextLineStyleSingle]; NSMutableAttributedString *attr3 = [[NSMutableAttributedString alloc] initWithString:@"普通文本偏 +下划线不偏"]; attr3.yy_font = font; attr3.yy_maximumLineHeight = 22; attr3.yy_minimumLineHeight = 22; attr3.yy_underlineStyle = NSUnderlineStyleSingle; attr3.yy_baselineOffset = @(baselineOffset); baselineOffset = (22 - attr3.yy_font.lineHeight) / 4; NSMutableAttributedString *attr4 = [[NSMutableAttributedString alloc] initWithString:@"普通文本不偏 下划线不偏\n"]; attr4.yy_font = font; attr4.yy_maximumLineHeight = 22; attr4.yy_minimumLineHeight = 22; attr3.yy_underlineStyle = NSUnderlineStyleSingle; [Text appendAttributedString:attr1]; [Text yy_appendString:@" "]; [Text appendAttributedString:attr2]; [Text yy_appendString:@" "]; [Text appendAttributedString:attr3]; [Text yy_appendString:@" "]; [Text appendAttributedString:attr4]; label.attributedText = Text; [label sizeToFit]; [self.view addSubview:label];
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。