ShannonChenCHN / iOSDevLevelingUp

A journey of leveling up iOS development skills and knowledge.
364 stars 105 forks source link

文本绘制和排版 #30

Open ShannonChenCHN opened 7 years ago

ShannonChenCHN commented 7 years ago
ShannonChenCHN commented 7 years ago

延伸阅读:

ShannonChenCHN commented 7 years ago

字体

ShannonChenCHN commented 6 years ago

NSAttributedString

ShannonChenCHN commented 6 years ago

常见问题 1: 如何实现两个字体大小不同的 UILabel 的文字 baseline 对齐?(如图所示)

837cd5c5-aacf-4111-8786-198d6f760d03

方式一:通过 Auto Layout 实现

NSLayoutConstraint *layoutConstraint = [NSLayoutConstraint 
    constraintWithItem:_textLabel_1 
             attribute: NSLayoutAttributeBaseline
             relatedBy:NSLayoutRelationEqual 
                toItem:_textLabel_2 
             attribute:NSLayoutAttributeBaseline 
            multiplier:1.0f
              constant:0];
[self.view addConstraint:layoutConstraint];

使用 Masonry 实现:

[_priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.mas_right).offset(-kLeftRightPadding);
            make.baseline.equalTo(_priceTitleLabel.mas_baseline);
        }];

方式二:通过获取字体的 descent 值,来计算 label 中文字的 baseline 的位置

参考:

ShannonChenCHN commented 6 years ago

常见问题 2:给 UILabel 中的文字添加删除线

方法一:通过 NSStrikethroughStyleAttributeName 设置富文本属性

NSAttributedString *attrStr = 
[[NSAttributedString alloc] initWithString:priceText               
                              attributes:
@{NSFontAttributeName: [UIFont systemFontOfSize:20.f],       
  NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#5bcec0"],     
  NSStrikethroughStyleAttributeName: @(NSUnderlineStyleSingle), 
  NSStrikethroughColorAttributeName:[UIColor colorWithHexString:@"#5bcec0"]}];
self.priceLabel.attributedText = attrStr;

方法二:自定义 UILabel,重写 drawRect: 方法

@interface YHLineThroughLabel : UILabel

@property (nonatomic, assign) BOOL offsetForTextStrikeThrough;    ///< 删除线效果的偏移量
@property (nonatomic, assign) CGFloat lineWidthForStrikeThrough;  ///< 删除线的线宽

@end

@implementation YHLineThroughLabel

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _lineWidthForStrikeThrough = 0.5;
        _offsetForTextStrikeThrough = 0;
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, self.textColor.CGColor);
    CGRect strikeLineFrame = CGRectMake(0,
                                        (self.font.lineHeight - self.lineWidthForStrikeThrough) * 0.5 + self.offsetForTextStrikeThrough,
                                        rect.size.width,
                                        self.lineWidthForStrikeThrough);
    CGContextFillRect(context, strikeLineFrame);
}

@end

参考

ShannonChenCHN commented 6 years ago

常见问题3:如何给 UILabel 的文字添加阴影效果?

方法1:直接设置 UILabel 的 shadowColorshadowOffset

UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 30, 300, 50)];
label.text = @"UILabel文字阴影效果";
//阴影颜色
label.shadowColor = [UIColor redColor];
//阴影偏移  x,y为正表示向右下偏移
label.shadowOffset = CGSizeMake(1, 1);
[self.view addSubview:label];

方法二:设置 layer 的阴影。UILabel 提供的设置阴影的属性比较少,如果要进行更多的设置,就要在layer层进行设置,但要把背景色设置为透明。

        self.nameLabel = [[UILabel alloc] init];
        _nameLabel.numberOfLines = 1;
        _nameLabel.font = [UIFont systemFontOfSize:AUTO_SIZE_320(14)];
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        _nameLabel.textColor = HEXCOLOR(0xFFFFFF);
        _nameLabel.layer.shadowColor = [UIColor blackColor].CGColor;
        _nameLabel.layer.shadowOffset = CGSizeZero;
        _nameLabel.layer.shadowOpacity = 0.4;
        _nameLabel.layer.shadowRadius = 4;
        [self.contentView addSubview:_nameLabel];

方法3:通过设置 NSAttributedString 富文本字符串的 shadow 属性来设置阴影。

    NSShadow *shadow = [[NSShadow alloc]init];
    shadow.shadowBlurRadius = 1.0;
    shadow.shadowOffset = CGSizeMake(1, 1);
    shadow.shadowColor = [UIColor redColor];
    [_attributedString addAttribute:NSShadowAttributeName
                              value:shadow
                              range:NSMakeRange(1, _attributedString.length-1)];

参考:

ShannonChenCHN commented 6 years ago

Icon Font

1. 什么是 Icon Font?

Icon Font 是一种矢量图标,可以以字体的形式展示 icon。

2. 为什么要使用 Icon Font?

3. 如何使用?

推荐阅读

ShannonChenCHN commented 5 years ago

如何展示带有超链接的文字

NSMutableAttributedString 中有一个 NSLinkAttributeName 属性。

参考

ShannonChenCHN commented 5 years ago

如何展示首行“缩进”的文字

image

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.headIndent = 15;
    style.firstLineHeadIndent = 0;