Files
yuyin_ios/SweetParty/主类/Others/NewTools/UILabel+ChangeSpace.m
2025-08-08 11:05:33 +08:00

121 lines
5.0 KiB
Objective-C
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// UILabel+ChangeSpace.m
// romantic
//
// Created by bj_szd on 2022/4/9.
// Copyright © 2022 romantic. All rights reserved.
//
#import "UILabel+ChangeSpace.h"
@implementation UILabel (ChangeSpace)
+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {
NSString *labelText = label.text;
if (labelText == nil) {
return;
}
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.alignment = NSTextAlignmentJustified;//两端对齐
[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;
if (labelText == nil) {
return;
}
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;
if (labelText == nil) {
return;
}
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];
}
/**
改变label文字中某段文字的颜色大小
label 传入label传入前要有文字
oneW 从第一个文字开始
twoW 到最后一个文字结束
color 颜色
size 尺寸
*/
- (void)LabelAttributedString:(UILabel*)label firstW:(NSString *)oneW size:(CGFloat)size {
// 创建Attributed
NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:label.text];
// 需要改变的最后一个文字的位置
NSRange range = [[noteStr string] rangeOfString:oneW];
// 改变字体大小及类型
[noteStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:range];
// 为label添加Attributed
[label setAttributedText:noteStr];
}
// label底部对齐
- (void)bottomAlignment
{
CGSize size = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
CGRect rect = [self.text boundingRectWithSize:CGSizeMake(self.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil];
self.numberOfLines = 0;
NSInteger newLinesToPad = (self.frame.size.height - rect.size.height)/size.height;
for (NSInteger i = 0; i < newLinesToPad; i ++) {
self.text = [NSString stringWithFormat:@" \n%@",self.text];
}
}
// label顶部对齐
- (void)topAlignment
{
CGSize size = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
CGRect rect = [self.text boundingRectWithSize:CGSizeMake(self.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil];
self.numberOfLines = 0;
NSInteger newLinesToPad = (self.frame.size.height - rect.size.height)/size.height;
for (NSInteger i = 0; i < newLinesToPad; i ++) {
self.text = [self.text stringByAppendingString:@"\n "];
}
}
+ (CGFloat)textHeight:(NSString *)text font:(UIFont *)font width:(CGFloat)width lineSpace:(CGFloat)lineSpace {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
paragraphStyle.lineSpacing = lineSpace;
//Attribute传和label设定的一样
NSDictionary * attributes = @{
NSFontAttributeName:font,
NSParagraphStyleAttributeName: paragraphStyle
};
//这里的sizewidth传label的宽高默认都传MAXFLOAT
CGSize textRect = CGSizeMake(width, MAXFLOAT);
CGFloat textHeight = [text boundingRectWithSize: textRect
options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
attributes:attributes
context:nil].size.height;
return textHeight;
}
@end