From 8214342f5b6f96030f21ded252fd62c113866b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=91=E8=89=B2=E9=98=9F=E9=95=BF?= Date: Sat, 19 Dec 2020 17:14:56 +0800 Subject: [PATCH 1/3] Update TYViewStorage.m MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增在 iOS 9 以上系统兼容 RTL 方向排版 --- TYAttributedLabelDemo/TYAttributedLabel/TYViewStorage.m | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/TYAttributedLabelDemo/TYAttributedLabel/TYViewStorage.m b/TYAttributedLabelDemo/TYAttributedLabel/TYViewStorage.m index 4af2032..96862a1 100755 --- a/TYAttributedLabelDemo/TYAttributedLabel/TYViewStorage.m +++ b/TYAttributedLabelDemo/TYAttributedLabel/TYViewStorage.m @@ -45,6 +45,14 @@ - (void)drawStorageWithRect:(CGRect)rect // 设置frame 注意 转换rect CoreText context coordinates are the opposite to UIKit so we flip the bounds CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformMakeTranslation(0, _superView.bounds.size.height), 1.f, -1.f); rect = CGRectApplyAffineTransform(rect, transform); + + //iOS 9 以上系统兼容 RTL 排版方向 + if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) { + if (UIUserInterfaceLayoutDirectionRightToLeft == [UIView userInterfaceLayoutDirectionForSemanticContentAttribute:_superView.semanticContentAttribute]) { + rect.origin.x = rect.origin.x - rect.size.width; + } + } + [_view setFrame:rect]; [_superView addSubview:_view]; } From f58c5094aba806a8e1246cc0d4a14668f3e19fc4 Mon Sep 17 00:00:00 2001 From: Captain Black Date: Thu, 16 Mar 2023 18:24:32 +0800 Subject: [PATCH 2/3] =?UTF-8?q?bugfix:=20=E4=BF=AE=E6=94=B9=E5=AD=97?= =?UTF-8?q?=E4=BD=93=E8=AE=BE=E7=BD=AE=E4=B8=8D=E6=98=AF=E7=9B=AE=E6=A0=87?= =?UTF-8?q?=E5=AD=97=E4=BD=93=E7=9A=84=E9=97=AE=E9=A2=98=20modify:=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=BB=98=E8=AE=A4=E8=AE=BE=E7=BD=AE=E8=B7=9F?= =?UTF-8?q?=E7=B3=BB=E7=BB=9FUILabel=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TYAttributedLabel/NSMutableAttributedString+TY.m | 2 +- TYAttributedLabelDemo/TYAttributedLabel/TYAttributedLabel.m | 2 +- TYAttributedLabelDemo/TYAttributedLabel/TYTextContainer.m | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/TYAttributedLabelDemo/TYAttributedLabel/NSMutableAttributedString+TY.m b/TYAttributedLabelDemo/TYAttributedLabel/NSMutableAttributedString+TY.m index 16e2f04..386f3b5 100644 --- a/TYAttributedLabelDemo/TYAttributedLabel/NSMutableAttributedString+TY.m +++ b/TYAttributedLabelDemo/TYAttributedLabel/NSMutableAttributedString+TY.m @@ -41,7 +41,7 @@ - (void)addAttributeFont:(UIFont *)font range:(NSRange)range { [self removeAttribute:(NSString*)kCTFontAttributeName range:range]; - CTFontRef fontRef = CTFontCreateWithName((CFStringRef)font.fontName, font.pointSize, nil); + CTFontRef fontRef = CTFontCreateWithName((CFStringRef)font.familyName, font.pointSize, nil); if (nil != fontRef) { [self addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)fontRef range:range]; diff --git a/TYAttributedLabelDemo/TYAttributedLabel/TYAttributedLabel.m b/TYAttributedLabelDemo/TYAttributedLabel/TYAttributedLabel.m index 62e39d0..7cfdff6 100755 --- a/TYAttributedLabelDemo/TYAttributedLabel/TYAttributedLabel.m +++ b/TYAttributedLabelDemo/TYAttributedLabel/TYAttributedLabel.m @@ -88,7 +88,7 @@ - (instancetype)initWithTextContainer:(TYTextContainer *)textContainer - (void)setupProperty { if (self.backgroundColor == nil) { - self.backgroundColor = [UIColor whiteColor]; + self.backgroundColor = [UIColor clearColor]; } self.userInteractionEnabled = YES; _highlightedLinkColor = nil; diff --git a/TYAttributedLabelDemo/TYAttributedLabel/TYTextContainer.m b/TYAttributedLabelDemo/TYAttributedLabel/TYTextContainer.m index 6ecb1df..3efb4fa 100755 --- a/TYAttributedLabelDemo/TYAttributedLabel/TYTextContainer.m +++ b/TYAttributedLabelDemo/TYAttributedLabel/TYTextContainer.m @@ -97,9 +97,9 @@ - (NSAttributedString *)createAttributedString #pragma mark - setter - (void)setupProperty { - _font = [UIFont systemFontOfSize:15]; - _characterSpacing = 1; - _linesSpacing = 2; + _font = [UIFont systemFontOfSize:17]; + _characterSpacing = 0; + _linesSpacing = 0; _paragraphSpacing = 0; _textAlignment = kCTLeftTextAlignment; _lineBreakMode = kCTLineBreakByCharWrapping; From 7f0e0943e9575c8f9720476451016ab2cc6c7216 Mon Sep 17 00:00:00 2001 From: Captain Black Date: Thu, 16 Mar 2023 19:38:49 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feature:=20=E6=96=B0=E5=A2=9E=5FautoLayoutW?= =?UTF-8?q?idth=E6=88=90=E5=91=98=E5=8F=98=E9=87=8F=EF=BC=8C=E8=AE=B0?= =?UTF-8?q?=E5=BD=95autolayout=E5=B8=83=E5=B1=80=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E7=BA=A6=E6=9D=9F=E5=AE=BD=E5=BA=A6=EF=BC=8C=E7=84=B6=E5=90=8E?= =?UTF-8?q?=E5=86=8D=E8=B0=83=E7=94=A8invalidateIntrinsicContentSize?= =?UTF-8?q?=E9=87=8D=E6=96=B0=E8=AE=A1=E7=AE=97=E8=87=AA=E8=BA=AB=E7=9A=84?= =?UTF-8?q?=E5=AE=BD=E9=AB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TYAttributedLabel/TYAttributedLabel.m | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/TYAttributedLabelDemo/TYAttributedLabel/TYAttributedLabel.m b/TYAttributedLabelDemo/TYAttributedLabel/TYAttributedLabel.m index 7cfdff6..319f704 100755 --- a/TYAttributedLabelDemo/TYAttributedLabel/TYAttributedLabel.m +++ b/TYAttributedLabelDemo/TYAttributedLabel/TYAttributedLabel.m @@ -54,7 +54,9 @@ @interface TYAttributedLabel () @property (nonatomic, strong) UIColor *saveLinkColor; @end -@implementation TYAttributedLabel +@implementation TYAttributedLabel { + CGFloat _autoLayoutWidth; +} #pragma mark - init @@ -166,9 +168,9 @@ - (void)drawRect:(CGRect)rect { default: break; } - + CGFloat contextHeight = MAX(CGRectGetHeight(self.bounds) , _textContainer.textHeight); - // 跟很多底层 API 一样,Core Text 使用 Y翻转坐标系统,而且内容的呈现也是上下翻转的,所以需要通过转换内容将其翻转 + // 跟很多底层 API 一样,Core Text 使用 Y翻转坐标系统,而且内容的呈现也是上下翻转的,所以需要通过转换内容将其翻转 CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetTextMatrix(context, CGAffineTransformIdentity); CGContextTranslateCTM(context, 0, contextHeight + verticalOffset); @@ -187,7 +189,7 @@ - (void)drawRect:(CGRect)rect { // this code quote M80AttributedLabel - (void)drawText: (NSAttributedString *)attributedString - frame:(CTFrameRef)frame + frame:(CTFrameRef)frame rect: (CGRect)rect context: (CGContextRef)context { @@ -369,14 +371,14 @@ - (void)longPress:(UILongPressGestureRecognizer *)sender __typeof (self) __weak weakSelf = self; bool didPressContainer = [_textContainer enumerateRunRectContainPoint:point viewHeight:CGRectGetHeight(self.frame) successBlock:^(id textStorage){ if (_delegateFlags.textStorageLongPressedOnStateAtPoint) { - [weakSelf.delegate attributedLabel:weakSelf textStorageLongPressed:textStorage onState:sender.state atPoint:point]; + [weakSelf.delegate attributedLabel:weakSelf textStorageLongPressed:textStorage onState:sender.state atPoint:point]; } }]; // 非响应容器区域响应长按事件 if (didPressContainer == NO && [weakSelf respondsToSelector:@selector(attributedLabel:lableLongPressOnState:atPoint:)]) { [weakSelf.delegate attributedLabel:weakSelf lableLongPressOnState:sender.state atPoint:point]; } - + } #pragma mark - touches action @@ -395,7 +397,7 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event found = YES; }]; } - + if (!found) { [super touchesBegan:touches withEvent:event]; } @@ -581,9 +583,9 @@ - (void)fillSelectionAreaInRect:(CGRect)rect radius:(CGFloat)radius bgColor:(UIC CGContextSetFillColorWithColor(context, bgColor.CGColor); CGContextDrawPath(context, kCGPathFill); -// CGContextRef context = UIGraphicsGetCurrentContext(); -// CGContextSetFillColorWithColor(context, bgColor.CGColor); -// CGContextFillRect(context, rect); + // CGContextRef context = UIGraphicsGetCurrentContext(); + // CGContextSetFillColorWithColor(context, bgColor.CGColor); + // CGContextFillRect(context, rect); } #pragma mark - get Right Height @@ -618,7 +620,7 @@ - (void)setPreferredMaxLayoutWidth:(CGFloat)preferredMaxLayoutWidth - (CGSize)intrinsicContentSize { - return [self getSizeWithWidth:_preferredMaxLayoutWidth]; + return [self getSizeWithWidth:(_preferredMaxLayoutWidth == 0 ? _autoLayoutWidth : _preferredMaxLayoutWidth)]; } #pragma mark - set right frame @@ -798,6 +800,15 @@ - (void)setIsWidthToFit:(BOOL)isWidthToFit [_textContainer setIsWidthToFit:isWidthToFit]; } +- (void)layoutSubviews { + if (_preferredMaxLayoutWidth == 0 && + _autoLayoutWidth != CGRectGetWidth(self.frame)) { + _autoLayoutWidth = CGRectGetWidth(self.frame); + [self invalidateIntrinsicContentSize]; + } + [super layoutSubviews]; +} + @end #pragma mark - append attributedString