229 lines
7.6 KiB
Objective-C
229 lines
7.6 KiB
Objective-C
//
|
|
// ViewLable.m
|
|
// ProductApp
|
|
//
|
|
// Created by 工作 on 2024/9/21.
|
|
//
|
|
|
|
#import "ViewLable.h"
|
|
|
|
|
|
@interface ViewLable ()
|
|
///
|
|
@property (nonatomic , strong) UILabel *lbname;
|
|
///
|
|
@property (nonatomic , strong) UIImageView *imgvb;
|
|
///
|
|
@property (nonatomic , strong) NSTimer *timer;
|
|
@property (nonatomic , assign) int inow;
|
|
|
|
@property (strong, nonatomic) NSLayoutManager *layoutManager;
|
|
@property (strong, nonatomic) NSTextStorage *textStorage;
|
|
@property (strong, nonatomic) NSTextContainer *textContainer;
|
|
|
|
@end
|
|
|
|
@implementation ViewLable
|
|
|
|
-(id)initWithFrame:(CGRect)frame
|
|
{
|
|
if(self = [super initWithFrame:frame])
|
|
{
|
|
|
|
self.textStorage = [NSTextStorage new];
|
|
self.layoutManager = [NSLayoutManager new];
|
|
|
|
self.textContainer = [NSTextContainer new];
|
|
|
|
[self.textStorage addLayoutManager:self.layoutManager];
|
|
|
|
[self.layoutManager addTextContainer:self.textContainer];
|
|
|
|
|
|
PGJUILabel *lbname = [[PGJUILabel alloc] init];
|
|
[lbname setTextColor:RGBCOLOR(26, 26, 26)];
|
|
[lbname setTextAlignment:NSTextAlignmentLeft];
|
|
[lbname setFont:[UIFont systemFontOfSize:16]];
|
|
[lbname setNumberOfLines:0];
|
|
[self addSubview:lbname];
|
|
[lbname mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.left.right.top.equalTo(self);
|
|
}];
|
|
_lbname = lbname;
|
|
[self mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.height.offset(18);
|
|
}];
|
|
|
|
UIImageView *imgvb = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 18, 18)];
|
|
[imgvb setImage:[UIImage imageNamed:@"yd_drawTextB"]];
|
|
[self addSubview:imgvb];
|
|
_imgvb = imgvb;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
-(void)setTextColor:(UIColor *)textColor
|
|
{
|
|
self.lbname.textColor = textColor;
|
|
}
|
|
-(void)setTextFont:(UIFont *)textFont
|
|
{
|
|
self.lbname.font = textFont;
|
|
}
|
|
-(void)setFspeed:(float)fspeed
|
|
{
|
|
_fspeed = fspeed;
|
|
}
|
|
-(void)setStrValue:(NSString *)strValue
|
|
{
|
|
_strValue = strValue;
|
|
if(self.isCloseAnimation)
|
|
{
|
|
[self.imgvb setHidden:YES];
|
|
|
|
NSString *str = self.strValue;
|
|
///行间距
|
|
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
|
|
style.lineSpacing = 5;
|
|
style.lineBreakMode = NSLineBreakByWordWrapping;
|
|
style.alignment = NSTextAlignmentLeft;
|
|
|
|
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:str];
|
|
[attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, str.length)];
|
|
|
|
self.lbname.attributedText = attrString;
|
|
|
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
|
float fheight = [Tools getLabelHeightWithText:self.lbname.text width:self.width font:self.lbname.font.pointSize linHeight:5]+1;
|
|
[self mas_updateConstraints:^(MASConstraintMaker *make) {
|
|
make.height.offset(fheight);
|
|
}];
|
|
self.backHeight(fheight,NO);
|
|
});
|
|
}
|
|
else
|
|
{
|
|
[self showText];
|
|
}
|
|
}
|
|
-(void)showText
|
|
{
|
|
[self endAction];
|
|
[self.imgvb setHidden:NO];
|
|
self.inow = 0;
|
|
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:self.fspeed>0?self.fspeed:0.02 target:self selector:@selector(textSpeedAppend) userInfo:nil repeats:YES];
|
|
_timer = timer;
|
|
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
|
|
}
|
|
-(void)textSpeedAppend
|
|
{
|
|
if(self.inow+1>self.strValue.length)
|
|
{
|
|
[self.timer invalidate];
|
|
self.timer = nil;
|
|
[self.imgvb setHidden:YES];
|
|
self.backHeight(self.height,YES);
|
|
return;
|
|
}
|
|
|
|
NSString *str = [self.strValue substringToIndex:self.inow];
|
|
///行间距
|
|
NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
|
|
style.lineSpacing = 5;
|
|
style.lineBreakMode = NSLineBreakByWordWrapping;
|
|
style.alignment = NSTextAlignmentLeft;
|
|
|
|
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:str];
|
|
[attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, str.length)];
|
|
|
|
|
|
self.lbname.attributedText = attrString;
|
|
self.inow++;
|
|
|
|
float fheight = [Tools getLabelHeightWithText:self.lbname.text width:self.width font:self.lbname.font.pointSize linHeight:5]+1;
|
|
[self mas_updateConstraints:^(MASConstraintMaker *make) {
|
|
make.height.offset(fheight);
|
|
}];
|
|
self.backHeight(fheight,NO);
|
|
|
|
CGPoint point = [self boundingRectForLastCharacterInLabel:self.lbname];
|
|
if(point.x>0&&point.y>0)
|
|
{
|
|
self.imgvb.center = point;
|
|
}
|
|
|
|
|
|
NSLog(@"%@",NSStringFromCGPoint(point));
|
|
|
|
}
|
|
- (CGPoint)boundingRectForLastCharacterInLabel:(UILabel *)label {
|
|
NSString *text = label.text;
|
|
//
|
|
// NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
|
|
// style.lineSpacing = 5;
|
|
// style.lineBreakMode = NSLineBreakByWordWrapping;
|
|
// style.alignment = NSTextAlignmentLeft;
|
|
//
|
|
// NSDictionary *attributes = @{NSFontAttributeName: label.font,NSParagraphStyleAttributeName:style}; // 设置字体属性
|
|
//
|
|
// CGRect sz = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, 20) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
|
|
//
|
|
// CGRect linesSz = [text boundingRectWithSize:CGSizeMake(label.frame.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
|
|
//
|
|
// [text sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 20) lineBreakMode:NSLineBreakByWordWrapping];
|
|
//
|
|
// CGPoint lastPoint;
|
|
// if(sz.size.height>=linesSz.size.height)
|
|
// {
|
|
// lastPoint = CGPointMake(label.frame.origin.x+sz.size.width, label.frame.origin.y+9);
|
|
// }
|
|
// else
|
|
// {
|
|
// lastPoint = CGPointMake(label.frame.origin.x+(int)sz.size.width%(int)linesSz.size.width, linesSz.size.height-sz.size.height+9);
|
|
// }
|
|
|
|
[self configWithLabel:label];
|
|
CGRect rect = [self characterRectAtIndex:text.length-1];
|
|
CGPoint lastPoint = CGPointMake(rect.origin.x+rect.size.width/2.0, rect.origin.y+rect.size.height/2.0);
|
|
return lastPoint;
|
|
}
|
|
|
|
-(void)configWithLabel:(UILabel *)label
|
|
{
|
|
self.textContainer.size = label.bounds.size;
|
|
self.textContainer.lineFragmentPadding = 0;
|
|
self.textContainer.maximumNumberOfLines = label.numberOfLines;
|
|
self.textContainer.lineBreakMode = label.lineBreakMode;
|
|
|
|
NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:label.text];
|
|
NSRange textRange = NSMakeRange(0, attributedText.length);
|
|
[attributedText addAttribute:NSFontAttributeName value:label.font range:textRange];
|
|
|
|
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
|
|
paragraphStyle.lineSpacing = 5;
|
|
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
|
|
paragraphStyle.alignment = label.textAlignment;
|
|
|
|
[attributedText addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:textRange];
|
|
[self.textStorage setAttributedString:attributedText];
|
|
}
|
|
-(CGRect)characterRectAtIndex:(NSUInteger)charIndex
|
|
{
|
|
if (charIndex >= self.textStorage.length) {
|
|
return CGRectZero;
|
|
}
|
|
NSRange characterRange = NSMakeRange(charIndex, 1);
|
|
NSRange glyphRange = [self.layoutManager glyphRangeForCharacterRange:characterRange actualCharacterRange:nil];
|
|
return [self.layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:self.textContainer];
|
|
}
|
|
|
|
-(void)endAction
|
|
{
|
|
if(self.timer)
|
|
{
|
|
[self.timer invalidate];
|
|
self.timer = nil;
|
|
}
|
|
}
|
|
@end
|