Browse Source

added Nullability for YYTextView, and updated comments

ibireme 9 years ago
parent
commit
93c4e73f62
4 changed files with 35 additions and 42 deletions
  1. 1 1
      YYText/YYLabel.h
  2. 6 21
      YYText/YYLabel.m
  3. 23 20
      YYText/YYTextView.h
  4. 5 0
      YYText/YYTextView.m

+ 1 - 1
YYText/YYLabel.h

@@ -51,7 +51,7 @@
 @property (nullable, nonatomic, copy) NSString *text;
 @property (nullable, nonatomic, copy) NSString *text;
 
 
 /**
 /**
- The font of the text. Default is 12-point system font.
+ The font of the text. Default is 17-point system font.
  Set a new value to this property also causes the new font to be applied to the entire `attributedText`.
  Set a new value to this property also causes the new font to be applied to the entire `attributedText`.
  Get the value returns the font at the head of `attributedText`.
  Get the value returns the font at the head of `attributedText`.
  */
  */

+ 6 - 21
YYText/YYLabel.m

@@ -69,9 +69,6 @@ static dispatch_queue_t YYLabelGetReleaseQueue() {
 
 
 @implementation YYLabel
 @implementation YYLabel
 
 
-@synthesize font = _font;
-@synthesize textColor = _textColor;
-
 #pragma mark - Private
 #pragma mark - Private
 
 
 - (void)_updateIfNeeded {
 - (void)_updateIfNeeded {
@@ -294,7 +291,9 @@ static dispatch_queue_t YYLabelGetReleaseQueue() {
 - (void)_updateOuterTextProperties {
 - (void)_updateOuterTextProperties {
     _text = [_innerText yy_plainTextForRange:NSMakeRange(0, _innerText.length)];
     _text = [_innerText yy_plainTextForRange:NSMakeRange(0, _innerText.length)];
     _font = _innerText.yy_font;
     _font = _innerText.yy_font;
+    if (!_font) _font = [self _defaultFont];
     _textColor = _innerText.yy_color;
     _textColor = _innerText.yy_color;
+    if (!_textColor) _textColor = [UIColor blackColor];
     _textAlignment = _innerText.yy_alignment;
     _textAlignment = _innerText.yy_alignment;
     _lineBreakMode = _innerText.yy_lineBreakMode;
     _lineBreakMode = _innerText.yy_lineBreakMode;
     NSShadow *shadow = _innerText.yy_shadow;
     NSShadow *shadow = _innerText.yy_shadow;
@@ -337,6 +336,8 @@ static dispatch_queue_t YYLabelGetReleaseQueue() {
     _debugOption = [YYTextDebugOption sharedDebugOption];
     _debugOption = [YYTextDebugOption sharedDebugOption];
     [YYTextDebugOption addDebugTarget:self];
     [YYTextDebugOption addDebugTarget:self];
     
     
+    _font = [self _defaultFont];
+    _textColor = [UIColor blackColor];
     _textVerticalAlignment = YYTextVerticalAlignmentCenter;
     _textVerticalAlignment = YYTextVerticalAlignmentCenter;
     _numberOfLines = 1;
     _numberOfLines = 1;
     _lineBreakMode = NSLineBreakByTruncatingTail;
     _lineBreakMode = NSLineBreakByTruncatingTail;
@@ -564,14 +565,6 @@ static dispatch_queue_t YYLabelGetReleaseQueue() {
     }
     }
 }
 }
 
 
-- (UIFont *)font
-{
-    if (!_font) {
-        _font = [self _defaultFont];
-    }
-    return _font;
-}
-
 - (void)setFont:(UIFont *)font {
 - (void)setFont:(UIFont *)font {
     if (!font) {
     if (!font) {
         font = [self _defaultFont];
         font = [self _defaultFont];
@@ -588,14 +581,6 @@ static dispatch_queue_t YYLabelGetReleaseQueue() {
     }
     }
 }
 }
 
 
-- (UIColor *)textColor
-{
-    if (!_textColor) {
-        _textColor = [UIColor blackColor];
-    }
-    return _textColor;
-}
-
 - (void)setTextColor:(UIColor *)textColor {
 - (void)setTextColor:(UIColor *)textColor {
     if (!textColor) {
     if (!textColor) {
         textColor = [UIColor blackColor];
         textColor = [UIColor blackColor];
@@ -1003,13 +988,13 @@ static dispatch_queue_t YYLabelGetReleaseQueue() {
         CGPoint point = CGPointZero;
         CGPoint point = CGPointZero;
         if (verticalAlignment == YYTextVerticalAlignmentCenter) {
         if (verticalAlignment == YYTextVerticalAlignmentCenter) {
             if (layout.container.isVerticalForm) {
             if (layout.container.isVerticalForm) {
-                point.x = (size.width - boundingSize.width) * 0.5;
+                point.x = -(size.width - boundingSize.width) * 0.5;
             } else {
             } else {
                 point.y = (size.height - boundingSize.height) * 0.5;
                 point.y = (size.height - boundingSize.height) * 0.5;
             }
             }
         } else if (verticalAlignment == YYTextVerticalAlignmentBottom) {
         } else if (verticalAlignment == YYTextVerticalAlignmentBottom) {
             if (layout.container.isVerticalForm) {
             if (layout.container.isVerticalForm) {
-                point.x = (size.width - boundingSize.width);
+                point.x = -(size.width - boundingSize.width);
             } else {
             } else {
                 point.y = (size.height - boundingSize.height);
                 point.y = (size.height - boundingSize.height);
             }
             }

+ 23 - 20
YYText/YYTextView.h

@@ -21,6 +21,8 @@
 #import "YYTextAttribute.h"
 #import "YYTextAttribute.h"
 #endif
 #endif
 
 
+NS_ASSUME_NONNULL_BEGIN
+
 @class YYTextView;
 @class YYTextView;
 
 
 /**
 /**
@@ -72,7 +74,7 @@
 /// @name Accessing the Delegate
 /// @name Accessing the Delegate
 ///=============================================================================
 ///=============================================================================
 
 
-@property (nonatomic, weak) id<YYTextViewDelegate> delegate;
+@property (nullable, nonatomic, weak) id<YYTextViewDelegate> delegate;
 
 
 
 
 #pragma mark - Configuring the Text Attributes
 #pragma mark - Configuring the Text Attributes
@@ -85,21 +87,21 @@
  Set a new value to this property also replaces the text in `attributedText`.
  Set a new value to this property also replaces the text in `attributedText`.
  Get the value returns the plain text in `attributedText`.
  Get the value returns the plain text in `attributedText`.
  */
  */
-@property (nonatomic, copy) NSString *text;
+@property (null_resettable, nonatomic, copy) NSString *text;
 
 
 /**
 /**
  The font of the text. Default is 12-point system font.
  The font of the text. Default is 12-point system font.
  Set a new value to this property also causes the new font to be applied to the entire `attributedText`.
  Set a new value to this property also causes the new font to be applied to the entire `attributedText`.
  Get the value returns the font at the head of `attributedText`.
  Get the value returns the font at the head of `attributedText`.
  */
  */
-@property (nonatomic, strong) UIFont *font;
+@property (nullable, nonatomic, strong) UIFont *font;
 
 
 /**
 /**
  The color of the text. Default is black.
  The color of the text. Default is black.
  Set a new value to this property also causes the new color to be applied to the entire `attributedText`.
  Set a new value to this property also causes the new color to be applied to the entire `attributedText`.
  Get the value returns the color at the head of `attributedText`.
  Get the value returns the color at the head of `attributedText`.
  */
  */
-@property (nonatomic, strong) UIColor *textColor;
+@property (nullable, nonatomic, strong) UIColor *textColor;
 
 
 /**
 /**
  The technique to use for aligning the text. Default is NSLeftTextAlignment.
  The technique to use for aligning the text. Default is NSLeftTextAlignment.
@@ -124,20 +126,20 @@
  When a range of text is detected by the `dataDetectorTypes`, this value would be
  When a range of text is detected by the `dataDetectorTypes`, this value would be
  used to modify the original attributes in the range.
  used to modify the original attributes in the range.
  */
  */
-@property (nonatomic, copy) NSDictionary *linkTextAttributes;
+@property (nullable, nonatomic, copy) NSDictionary *linkTextAttributes;
 
 
 /**
 /**
  The attributes to apply to links at highlight state. Default is a gray border.
  The attributes to apply to links at highlight state. Default is a gray border.
  When a range of text is detected by the `dataDetectorTypes` and the range was touched by user,
  When a range of text is detected by the `dataDetectorTypes` and the range was touched by user,
  this value would be used to modify the original attributes in the range.
  this value would be used to modify the original attributes in the range.
  */
  */
-@property (nonatomic, copy) NSDictionary *highlightTextAttributes;
+@property (nullable, nonatomic, copy) NSDictionary *highlightTextAttributes;
 
 
 /**
 /**
  The attributes to apply to new text being entered by the user.
  The attributes to apply to new text being entered by the user.
  When the text view's selection changes, this value is reset automatically.
  When the text view's selection changes, this value is reset automatically.
  */
  */
-@property (nonatomic, copy) NSDictionary *typingAttributes;
+@property (nullable, nonatomic, copy) NSDictionary *typingAttributes;
 
 
 /**
 /**
  The styled text displayed by the text view.
  The styled text displayed by the text view.
@@ -147,7 +149,7 @@
  @discussion It only support the attributes declared in CoreText and YYTextAttribute.
  @discussion It only support the attributes declared in CoreText and YYTextAttribute.
  See `NSAttributedString+YYText` for more convenience methods to set the attributes.
  See `NSAttributedString+YYText` for more convenience methods to set the attributes.
  */
  */
-@property (nonatomic, copy) NSAttributedString *attributedText;
+@property (nullable, nonatomic, copy) NSAttributedString *attributedText;
 
 
 /**
 /**
  When `text` or `attributedText` is changed, the parser will be called to modify the text.
  When `text` or `attributedText` is changed, the parser will be called to modify the text.
@@ -156,13 +158,13 @@
  
  
  See `YYTextParser` protocol for more information.
  See `YYTextParser` protocol for more information.
  */
  */
-@property (nonatomic, strong) id<YYTextParser> textParser;
+@property (nullable, nonatomic, strong) id<YYTextParser> textParser;
 
 
 /**
 /**
  The current text layout in text view (readonly).
  The current text layout in text view (readonly).
  It can be used to query the text layout information.
  It can be used to query the text layout information.
  */
  */
-@property (nonatomic, strong, readonly) YYTextLayout *textLayout;
+@property (nullable, nonatomic, strong, readonly) YYTextLayout *textLayout;
 
 
 
 
 #pragma mark - Configuring the Placeholder
 #pragma mark - Configuring the Placeholder
@@ -175,21 +177,21 @@
  Set a new value to this property also replaces the text in `placeholderAttributedText`.
  Set a new value to this property also replaces the text in `placeholderAttributedText`.
  Get the value returns the plain text in `placeholderAttributedText`.
  Get the value returns the plain text in `placeholderAttributedText`.
  */
  */
-@property (nonatomic, copy) NSString *placeholderText;
+@property (nullable, nonatomic, copy) NSString *placeholderText;
 
 
 /**
 /**
  The font of the placeholder text. Default is same as `font` property.
  The font of the placeholder text. Default is same as `font` property.
  Set a new value to this property also causes the new font to be applied to the entire `placeholderAttributedText`.
  Set a new value to this property also causes the new font to be applied to the entire `placeholderAttributedText`.
  Get the value returns the font at the head of `placeholderAttributedText`.
  Get the value returns the font at the head of `placeholderAttributedText`.
  */
  */
-@property (nonatomic, strong) UIFont *placeholderFont;
+@property (nullable, nonatomic, strong) UIFont *placeholderFont;
 
 
 /**
 /**
  The color of the placeholder text. Default is gray.
  The color of the placeholder text. Default is gray.
  Set a new value to this property also causes the new color to be applied to the entire `placeholderAttributedText`.
  Set a new value to this property also causes the new color to be applied to the entire `placeholderAttributedText`.
  Get the value returns the color at the head of `placeholderAttributedText`.
  Get the value returns the color at the head of `placeholderAttributedText`.
  */
  */
-@property (nonatomic, strong) UIColor *placeholderTextColor;
+@property (nullable, nonatomic, strong) UIColor *placeholderTextColor;
 
 
 /**
 /**
  The styled placeholder text displayed by the text view (when the text view is empty).
  The styled placeholder text displayed by the text view (when the text view is empty).
@@ -199,7 +201,7 @@
  @discussion It only support the attributes declared in CoreText and YYTextAttribute.
  @discussion It only support the attributes declared in CoreText and YYTextAttribute.
  See `NSAttributedString+YYText` for more convenience methods to set the attributes.
  See `NSAttributedString+YYText` for more convenience methods to set the attributes.
  */
  */
-@property (nonatomic, copy) NSAttributedString *placeholderAttributedText;
+@property (nullable, nonatomic, copy) NSAttributedString *placeholderAttributedText;
 
 
 
 
 #pragma mark - Configuring the Text Container
 #pragma mark - Configuring the Text Container
@@ -216,7 +218,7 @@
  An array of UIBezierPath objects representing the exclusion paths inside the 
  An array of UIBezierPath objects representing the exclusion paths inside the 
  receiver's bounding rectangle. Default value is nil.
  receiver's bounding rectangle. Default value is nil.
  */
  */
-@property (nonatomic, copy) NSArray *exclusionPaths;
+@property (nullable, nonatomic, copy) NSArray *exclusionPaths;
 
 
 /**
 /**
  Whether the receiver's layout orientation is vertical form. Default is NO.
  Whether the receiver's layout orientation is vertical form. Default is NO.
@@ -228,13 +230,13 @@
  The text line position modifier used to modify the lines' position in layout.
  The text line position modifier used to modify the lines' position in layout.
  See `YYTextLinePositionModifier` protocol for more information.
  See `YYTextLinePositionModifier` protocol for more information.
  */
  */
-@property (nonatomic, copy) id<YYTextLinePositionModifier> linePositionModifier;
+@property (nullable, nonatomic, copy) id<YYTextLinePositionModifier> linePositionModifier;
 
 
 /**
 /**
  The debug option to display CoreText layout result.
  The debug option to display CoreText layout result.
  The default value is [YYTextDebugOption sharedDebugOption].
  The default value is [YYTextDebugOption sharedDebugOption].
  */
  */
-@property (nonatomic, copy) YYTextDebugOption *debugOption;
+@property (nullable, nonatomic, copy) YYTextDebugOption *debugOption;
 
 
 
 
 #pragma mark - Working with the Selection and Menu
 #pragma mark - Working with the Selection and Menu
@@ -325,7 +327,7 @@
  @discussion If set the value while first responder, it will not take effect until 
  @discussion If set the value while first responder, it will not take effect until 
  'reloadInputViews' is called.
  'reloadInputViews' is called.
  */
  */
-@property (readwrite, retain) UIView *inputView;
+@property (nullable, readwrite, retain) UIView *inputView;
 
 
 /**
 /**
  The custom accessory view to display when the text view becomes the first responder.
  The custom accessory view to display when the text view becomes the first responder.
@@ -334,7 +336,7 @@
  @discussion If set the value while first responder, it will not take effect until
  @discussion If set the value while first responder, it will not take effect until
  'reloadInputViews' is called.
  'reloadInputViews' is called.
  */
  */
-@property (readwrite, retain) UIView *inputAccessoryView;
+@property (nullable, readwrite, retain) UIView *inputAccessoryView;
 
 
 /**
 /**
  If you use an custom accessory view without "inputAccessoryView" property,
  If you use an custom accessory view without "inputAccessoryView" property,
@@ -344,8 +346,9 @@
 
 
 @end
 @end
 
 
-
 // Notifications, see UITextView's documentation for more information.
 // Notifications, see UITextView's documentation for more information.
 UIKIT_EXTERN NSString *const YYTextViewTextDidBeginEditingNotification;
 UIKIT_EXTERN NSString *const YYTextViewTextDidBeginEditingNotification;
 UIKIT_EXTERN NSString *const YYTextViewTextDidChangeNotification;
 UIKIT_EXTERN NSString *const YYTextViewTextDidChangeNotification;
 UIKIT_EXTERN NSString *const YYTextViewTextDidEndEditingNotification;
 UIKIT_EXTERN NSString *const YYTextViewTextDidEndEditingNotification;
+
+NS_ASSUME_NONNULL_END

+ 5 - 0
YYText/YYTextView.m

@@ -1821,6 +1821,7 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
     if (_text == text || [_text isEqualToString:text]) return;
     if (_text == text || [_text isEqualToString:text]) return;
     [self willChangeValueForKey:@"text"];
     [self willChangeValueForKey:@"text"];
     _text = text.copy;
     _text = text.copy;
+    if (!_text) _text = @"";
     [self didChangeValueForKey:@"text"];
     [self didChangeValueForKey:@"text"];
     self.accessibilityLabel = _text;
     self.accessibilityLabel = _text;
 }
 }
@@ -1885,6 +1886,7 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
     if (_attributedText == attributedText || [_attributedText isEqual:attributedText]) return;
     if (_attributedText == attributedText || [_attributedText isEqual:attributedText]) return;
     [self willChangeValueForKey:@"attributedText"];
     [self willChangeValueForKey:@"attributedText"];
     _attributedText = attributedText.copy;
     _attributedText = attributedText.copy;
+    if (!_attributedText) _attributedText = [NSAttributedString new];
     [self didChangeValueForKey:@"attributedText"];
     [self didChangeValueForKey:@"attributedText"];
 }
 }
 
 
@@ -1941,6 +1943,9 @@ typedef NS_ENUM(NSUInteger, YYTextMoveDirection) {
     self.multipleTouchEnabled = NO;
     self.multipleTouchEnabled = NO;
     [super setDelegate:self];
     [super setDelegate:self];
     
     
+    _text = @"";
+    _attributedText = [NSAttributedString new];
+    
     // UITextInputTraits
     // UITextInputTraits
     _autocapitalizationType = UITextAutocapitalizationTypeSentences;
     _autocapitalizationType = UITextAutocapitalizationTypeSentences;
     _autocorrectionType = UITextAutocorrectionTypeDefault;
     _autocorrectionType = UITextAutocorrectionTypeDefault;