Selaa lähdekoodia

add convenience methods for text highlight

ibireme 9 vuotta sitten
vanhempi
commit
49617ce3a4

+ 23 - 11
Demo/YYTextDemo/YYTextAttributeExample.m

@@ -124,21 +124,33 @@
     {
         NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"Link"];
         one.yy_font = [UIFont boldSystemFontOfSize:30];
-        one.yy_color = [UIColor colorWithRed:0.093 green:0.492 blue:1.000 alpha:1.000];
         one.yy_underlineColor = one.yy_color;
         one.yy_underlineStyle = NSUnderlineStyleSingle;
         
-        YYTextBorder *border = [YYTextBorder new];
-        border.cornerRadius = 3;
-        border.insets = UIEdgeInsetsMake(0, -4, 0, -4);
-        border.fillColor = [UIColor colorWithWhite:0.000 alpha:0.220];
+        /// 1. you can set a highlight with these code
+        /*
+            one.yy_color = [UIColor colorWithRed:0.093 green:0.492 blue:1.000 alpha:1.000];
+
+            YYTextBorder *border = [YYTextBorder new];
+            border.cornerRadius = 3;
+            border.insets = UIEdgeInsetsMake(-2, -1, -2, -1);
+            border.fillColor = [UIColor colorWithWhite:0.000 alpha:0.220];
+            
+            YYTextHighlight *highlight = [YYTextHighlight new];
+            [highlight setBorder:border];
+            highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
+                [_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
+            };
+            [one yy_setTextHighlight:highlight range:one.yy_rangeOfAll];
+        */
         
-        YYTextHighlight *highlight = [YYTextHighlight new];
-        [highlight setBorder:border];
-        highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) {
-            [_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
-        };
-        [one yy_setTextHighlight:highlight range:one.yy_rangeOfAll];
+        /// 2. or you can use the convenience method
+        [one yy_setTextHighlightRange:one.yy_rangeOfAll
+                                color:[UIColor colorWithRed:0.093 green:0.492 blue:1.000 alpha:1.000]
+                      backgroundColor:[UIColor colorWithWhite:0.000 alpha:0.220]
+                            tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
+                                [_self showMessage:[NSString stringWithFormat:@"Tap: %@",[text.string substringWithRange:range]]];
+                            }];
         
         [text appendAttributedString:one];
         [text appendAttributedString:[self padding]];

+ 14 - 0
YYText/String/YYTextAttribute.h

@@ -288,6 +288,7 @@ typedef void(^YYTextAction)(UIView *containerView, NSAttributedString *text, NSR
  (set or remove) the original attributes in the range for display.
  */
 @interface YYTextHighlight : NSObject <NSCoding, NSCopying>
+
 /**
  Attributes that you can apply to text in an attributed string when highlight.
  Key:   Same as CoreText/YYText Attribute Name.
@@ -295,8 +296,21 @@ typedef void(^YYTextAction)(UIView *containerView, NSAttributedString *text, NSR
  */
 @property (nonatomic, copy) NSDictionary *attributes;
 
+/**
+ Creates a highlight object with specified attributes.
+ 
+ @param attributes The attributes which will replace original attributes when highlight,
+        If the value is NSNull, it will removed when highlight.
+ */
 + (instancetype)highlightWithAttributes:(NSDictionary *)attributes;
 
+/**
+ Convenience methods to create a default highlight with the specifeid background color.
+ 
+ @param color The background border color.
+ */
++ (instancetype)highlightWithBackgroundColor:(UIColor *)color;
+
 // Convenience methods below to set the `attributes`.
 - (void)setFont:(UIFont *)font;
 - (void)setColor:(UIColor *)color;

+ 11 - 0
YYText/String/YYTextAttribute.m

@@ -390,6 +390,17 @@ YYTextAttributeType YYTextAttributeGetType(NSString *name){
     return one;
 }
 
++ (instancetype)highlightWithBackgroundColor:(UIColor *)color {
+    YYTextBorder *highlightBorder = [YYTextBorder new];
+    highlightBorder.insets = UIEdgeInsetsMake(-2, -1, -2, -1);
+    highlightBorder.cornerRadius = 3;
+    highlightBorder.fillColor = color;
+    
+    YYTextHighlight *one = [self new];
+    [one setBackgroundBorder:highlightBorder];
+    return one;
+}
+
 - (void)setAttributes:(NSDictionary *)attributes {
     _attributes = attributes.mutableCopy;
 }

+ 49 - 0
YYText/Utility/NSAttributedString+YYText.h

@@ -1308,6 +1308,55 @@
 - (void)yy_setTextBlockBorder:(YYTextBorder *)textBlockBorder range:(NSRange)range;
 - (void)yy_setTextRubyAnnotation:(YYTextRubyAnnotation *)ruby range:(NSRange)range NS_AVAILABLE_IOS(8_0);
 
+
+#pragma mark - Convenience methods for text highlight
+///=============================================================================
+/// @name Convenience methods for text highlight
+///=============================================================================
+
+/**
+ Convenience method to set text highlight
+ 
+ @param range           text range
+ @param color           text color (pass nil to ignore)
+ @param backgroundColor text background color when highlight
+ @param userInfo        user information dictionary (pass nil to ignore)
+ @param tapAction       tap action when user tap the highlight (pass nil to ignore)
+ @param longPressAction long press action when user long press the highlight (pass nil to ignore)
+ */
+- (void)yy_setTextHighlightRange:(NSRange)range
+                           color:(UIColor *)color
+                 backgroundColor:(UIColor *)backgroundColor
+                        userInfo:(NSDictionary *)userInfo
+                       tapAction:(YYTextAction)tapAction
+                 longPressAction:(YYTextAction)longPressAction;
+
+/**
+ Convenience method to set text highlight
+ 
+ @param range           text range
+ @param color           text color (pass nil to ignore)
+ @param backgroundColor text background color when highlight
+ @param tapAction       tap action when user tap the highlight (pass nil to ignore)
+ */
+- (void)yy_setTextHighlightRange:(NSRange)range
+                           color:(UIColor *)color
+                 backgroundColor:(UIColor *)backgroundColor
+                       tapAction:(YYTextAction)tapAction;
+
+/**
+ Convenience method to set text highlight
+ 
+ @param range           text range
+ @param color           text color (pass nil to ignore)
+ @param backgroundColor text background color when highlight
+ @param userInfo        tap action when user tap the highlight (pass nil to ignore)
+ */
+- (void)yy_setTextHighlightRange:(NSRange)range
+                           color:(UIColor *)color
+                 backgroundColor:(UIColor *)backgroundColor
+                        userInfo:(NSDictionary *)userInfo;
+
 #pragma mark - Utilities
 ///=============================================================================
 /// @name Utilities

+ 38 - 0
YYText/Utility/NSAttributedString+YYText.m

@@ -1280,6 +1280,44 @@ return style. _attr_;
     [self yy_setAttribute:YYTextGlyphTransformAttributeName value:value range:range];
 }
 
+- (void)yy_setTextHighlightRange:(NSRange)range
+                           color:(UIColor *)color
+                 backgroundColor:(UIColor *)backgroundColor
+                        userInfo:(NSDictionary *)userInfo
+                       tapAction:(YYTextAction)tapAction
+                 longPressAction:(YYTextAction)longPressAction {
+    YYTextHighlight *highlight = [YYTextHighlight highlightWithBackgroundColor:backgroundColor];
+    highlight.userInfo = userInfo;
+    highlight.tapAction = tapAction;
+    highlight.longPressAction = longPressAction;
+    if (color) [self yy_setColor:color range:range];
+    [self yy_setTextHighlight:highlight range:range];
+}
+
+- (void)yy_setTextHighlightRange:(NSRange)range
+                           color:(UIColor *)color
+                 backgroundColor:(UIColor *)backgroundColor
+                       tapAction:(YYTextAction)tapAction {
+    [self yy_setTextHighlightRange:range
+                         color:color
+               backgroundColor:backgroundColor
+                      userInfo:nil
+                     tapAction:tapAction
+               longPressAction:nil];
+}
+
+- (void)yy_setTextHighlightRange:(NSRange)range
+                           color:(UIColor *)color
+                 backgroundColor:(UIColor *)backgroundColor
+                        userInfo:(NSDictionary *)userInfo {
+    [self yy_setTextHighlightRange:range
+                         color:color
+               backgroundColor:backgroundColor
+                      userInfo:userInfo
+                     tapAction:nil
+               longPressAction:nil];
+}
+
 - (void)yy_insertString:(NSString *)string atIndex:(NSUInteger)location {
     [self replaceCharactersInRange:NSMakeRange(location, 0) withString:string];
     [self yy_removeDiscontinuousAttributesInRange:NSMakeRange(location, string.length)];