Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完善autolayout高度自适应处理 #174

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
35 changes: 23 additions & 12 deletions TYAttributedLabelDemo/TYAttributedLabel/TYAttributedLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ @interface TYAttributedLabel ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong) UIColor *saveLinkColor;
@end

@implementation TYAttributedLabel
@implementation TYAttributedLabel {
CGFloat _autoLayoutWidth;
}

#pragma mark - init

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
{
Expand Down Expand Up @@ -369,14 +371,14 @@ - (void)longPress:(UILongPressGestureRecognizer *)sender
__typeof (self) __weak weakSelf = self;
bool didPressContainer = [_textContainer enumerateRunRectContainPoint:point viewHeight:CGRectGetHeight(self.frame) successBlock:^(id<TYTextStorageProtocol> 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
Expand All @@ -395,7 +397,7 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
found = YES;
}];
}

if (!found) {
[super touchesBegan:touches withEvent:event];
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions TYAttributedLabelDemo/TYAttributedLabel/TYTextContainer.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions TYAttributedLabelDemo/TYAttributedLabel/TYViewStorage.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down