UILable文本属性设置

 NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"NSAttributeString 可以用来设置字体、段落样式,字体颜色,字体背景颜色,可以添加删除线、下划线,可以设置字间距、阴影、空心字、斜体、扁平化"];

    [attributedString addAttribute:NSExpansionAttributeName value:@1 range:NSMakeRange(0, 17)];  // 扁平化

    [attributedString addAttribute:NSObliquenessAttributeName value:@1 range:NSMakeRange(18, 8)];// 倾斜

    

    

    //段落

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    paragraphStyle.firstLineHeadIndent = 80;// 首行缩进

    paragraphStyle.headIndent = 25; // 其它行缩进

    paragraphStyle.lineSpacing = 20;   // 行间距

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedString.length)];// 段落

    

    NSShadow *shadow = [[NSShadow alloc] init];

    shadow.shadowBlurRadius = 5; //模糊度

    shadow.shadowColor = [UIColor redColor];

    shadow.shadowOffset = CGSizeMake(1, 3);

    [attributedString addAttribute:NSVerticalGlyphFormAttributeName value:@(0) range:NSMakeRange(27, 4)];

    [attributedString addAttribute:NSShadowAttributeName value:shadow range:NSMakeRange(27, 4)];

    [attributedString addAttribute:NSStrokeWidthAttributeName value:@(-3.0) range:NSMakeRange(32, 11)];// 边线宽度

    [attributedString addAttribute:NSStrokeColorAttributeName value:[UIColor greenColor] range:NSMakeRange(32, 11)];//边线颜色,需要先设置边线宽度

    [attributedString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(44, 7)]; // 删除线

    [attributedString addAttribute:NSUnderlineStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(52, 3)]; // 下划线

    

    [attributedString setAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Arial-BoldItalicMT" size:18],  NSKernAttributeName:@(10),NSForegroundColorAttributeName:[UIColor redColor], NSBackgroundColorAttributeName:[UIColor yellowColor]} range:NSMakeRange(56, 20)];

    

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 200)];

    label.numberOfLines = 0;

    label.backgroundColor = [UIColor grayColor];

    label.attributedText = attributedString;

    [self.view addSubview:label];


#pragma mark

顶上

- (void)alignTop;


/**

 *  改变行间距

 */

+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space;


/**

 *  改变字间距

 */

+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space;


/**

 *  改变行间距和字间距

 */

+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace;


- (void)alignTop {

    CGSize fontSize = [self.text sizeWithFont:[UIFont systemFontOfSize:19.0 weight:0.2]];

    

    double finalHeight = fontSize.height * self.numberOfLines;

    double finalWidth = self.frame.size.width;    //expected width of label

    CGSize theStringSize = [self.text sizeWithFont:[UIFont systemFontOfSize:19.0 weight:0.2] constrainedToSize:CGSizeMake(finalWidth, finalHeight) lineBreakMode:self.lineBreakMode];

    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;

    for(int i=0; i<newLinesToPad; i++)

        self.text = [self.text stringByAppendingString:@"\n "];

}


+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {

    

    NSString *labelText = label.text;

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    [paragraphStyle setLineSpacing:space];

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];

    label.attributedText = attributedString;

    [label sizeToFit];

    

}


+ (void)changeWordSpaceForLabel:(UILabel *)label WithSpace:(float)space {

    

    NSString *labelText = label.text;

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(space)}];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];

    label.attributedText = attributedString;

    [label sizeToFit];

    

}


+ (void)changeSpaceForLabel:(UILabel *)label withLineSpace:(float)lineSpace WordSpace:(float)wordSpace {

    

    NSString *labelText = label.text;

    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText attributes:@{NSKernAttributeName:@(wordSpace)}];

    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];

    [paragraphStyle setLineSpacing:lineSpace];

    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];

    label.attributedText = attributedString;

    [label sizeToFit];

    

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值