UILabel常见使用.md

行数为一行:自适应宽度


self.titleLbl.font = [UIFont systemFontOfSize:14.0];
self.titleLbl.numberOfLines = 1;//
CGSize size = [self.titleLbl.text sizeWithAttributes:
               [NSDictionary dictionaryWithObjectsAndKeys:
                [UIFont systemFontOfSize:14.0],NSFontAttributeName, nil]];
self.titleLbl.frame = CGRectMake(10, self.frame.size.height-size.height-4,
                                 size.width, size.height);

text和attributeText的关系

设置attributeText时,不要再设置text,否则会attributeText的属性会影响text;

设置label旋转

  • 方法一: 使用transform属性,但是整个view都会旋转.
    self.titleLbl.transform = CGAffineTransformMakeRotation(0.2)
    UILabel常见使用.md_第1张图片
    Snip20210322_166.png
  • 方法二: 使用NSMutableAttributedString, 设置NSObliquenessAttributeName
    NSString *titleLblText = @"+240";
    NSMutableAttributedString *titleAttrStr = [[NSMutableAttributedString alloc] initWithString:titleLblText];
    [titleAttrStr addAttribute:NSObliquenessAttributeName value:@(0.5) range:NSMakeRange(0, titleLblText.length)];
    self.titleLbl.attributedText = titleAttrStr;
    Snip20210322_168.png

你可能感兴趣的:(UILabel常见使用.md)