Files
yuyin_ios/SweetParty/主类/Others/NewTools/UILabel+ChangeSpace.m

121 lines
5.0 KiB
Mathematica
Raw Normal View History

2025-08-08 11:05:33 +08:00
//
// 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];
// labelAttributed
[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;
//Attributelabel
NSDictionary * attributes = @{
NSFontAttributeName:font,
NSParagraphStyleAttributeName: paragraphStyle
};
//sizewidthlabelMAXFLOAT
CGSize textRect = CGSizeMake(width, MAXFLOAT);
CGFloat textHeight = [text boundingRectWithSize: textRect
options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading
attributes:attributes
context:nil].size.height;
return textHeight;
}
@end