当前位置:   article > 正文

YYText实现文本与下划线,删除线偏移_yytext 下划线

yytext 下划线

设置下划线,删除线

主要原理就是通过设置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];
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/729140
推荐阅读