diff --git a/YYText/YYLabel.h b/YYText/YYLabel.h index 99cb1c64..dc4324cc 100644 --- a/YYText/YYLabel.h +++ b/YYText/YYLabel.h @@ -137,7 +137,7 @@ NS_ASSUME_NONNULL_BEGIN /** When `text` or `attributedText` is changed, the parser will be called to modify the text. - It can be used to add code highlighting or emoticon replacement to text view. + It can be used to add code highlighting or emoticon replacement to text view. The default value is nil. See `YYTextParser` protocol for more information. @@ -202,10 +202,10 @@ NS_ASSUME_NONNULL_BEGIN /** The preferred maximum width (in points) for a multiline label. - @discussion This property affects the size of the label when layout constraints - are applied to it. During layout, if the text extends beyond the width - specified by this property, the additional text is flowed to one or more new - lines, thereby increasing the height of the label. If the text is vertical + @discussion This property affects the size of the label when layout constraints + are applied to it. During layout, if the text extends beyond the width + specified by this property, the additional text is flowed to one or more new + lines, thereby increasing the height of the label. If the text is vertical form, this value will match to text height. */ @property (nonatomic) CGFloat preferredMaxLayoutWidth; @@ -236,7 +236,7 @@ NS_ASSUME_NONNULL_BEGIN /** When user long press the highlight range of text, this action will be called. - The default value is nil. + The default value is nil. */ @property (nullable, nonatomic, copy) YYTextAction highlightLongPressAction; @@ -248,7 +248,7 @@ NS_ASSUME_NONNULL_BEGIN /** A Boolean value indicating whether the layout and rendering codes are running - asynchronously on background threads. + asynchronously on background threads. The default value is `NO`. */ @@ -256,21 +256,21 @@ NS_ASSUME_NONNULL_BEGIN /** If the value is YES, and the layer is rendered asynchronously, then it will - set label.layer.contents to nil before display. + set label.layer.contents to nil before display. The default value is `YES`. @discussion When the asynchronously display is enabled, the layer's content will be updated after the background render process finished. If the render process can not finished in a vsync time (1/60 second), the old content will be still kept - for display. You may manually clear the content by set the layer.contents to nil + for display. You may manually clear the content by set the layer.contents to nil after you update the label's properties, or you can just set this property to YES. */ @property (nonatomic) BOOL clearContentsBeforeAsynchronouslyDisplay; /** - If the value is YES, and the layer is rendered asynchronously, then it will add - a fade animation on layer when the contents of layer changed. + If the value is YES, and the layer is rendered asynchronously, then it will add + a fade animation on layer when the contents of layer changed. The default value is `YES`. */ @@ -278,7 +278,7 @@ NS_ASSUME_NONNULL_BEGIN /** If the value is YES, then it will add a fade animation on layer when some range - of text become highlighted. + of text become highlighted. The default value is `YES`. */ @@ -286,7 +286,7 @@ NS_ASSUME_NONNULL_BEGIN /** Ignore common properties (such as text, font, textColor, attributedText...) and - only use "textLayout" to display content. + only use "textLayout" to display content. The default value is `NO`. @@ -298,7 +298,7 @@ NS_ASSUME_NONNULL_BEGIN /* Tips: - 1. If you only need a UILabel alternative to display rich text and receive link touch event, + 1. If you only need a UILabel alternative to display rich text and receive link touch event, you do not need to adjust the display mode properties. 2. If you have performance issues, you may enable the asynchronous display mode @@ -337,6 +337,13 @@ NS_ASSUME_NONNULL_BEGIN @end +/// 扩展 +@interface YYLabel () + +@property (nonatomic, assign) BOOL adjustFitWidth; + +@end + #else // TARGET_INTERFACE_BUILDER IB_DESIGNABLE diff --git a/YYText/YYLabel.m b/YYText/YYLabel.m index 360393a4..45117095 100644 --- a/YYText/YYLabel.m +++ b/YYText/YYLabel.m @@ -1023,6 +1023,16 @@ - (void)setDisplaysAsynchronously:(BOOL)displaysAsynchronously { #pragma mark - AutoLayout +- (void)layoutSubviews { + [super layoutSubviews]; + if (self.adjustFitWidth == false) { + return; + } + if (self.preferredMaxLayoutWidth != self.bounds.size.width) { + self.preferredMaxLayoutWidth = self.bounds.size.width; + } +} + - (void)setPreferredMaxLayoutWidth:(CGFloat)preferredMaxLayoutWidth { if (_preferredMaxLayoutWidth == preferredMaxLayoutWidth) return; _preferredMaxLayoutWidth = preferredMaxLayoutWidth;