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..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 @@ -88,7 +90,7 @@ - (instancetype)initWithTextContainer:(TYTextContainer *)textContainer - (void)setupProperty { if (self.backgroundColor == nil) { - self.backgroundColor = [UIColor whiteColor]; + self.backgroundColor = [UIColor clearColor]; } self.userInteractionEnabled = YES; _highlightedLinkColor = nil; @@ -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 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; 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]; }