### Manually Installing YYText Source: https://github.com/ibireme/yytext/blob/master/README.md This snippet describes the manual installation process for YYText, which involves downloading source files, adding them to an Xcode project, linking required frameworks, and importing the main header. ```Objective-C #import "YYText.h" ``` -------------------------------- ### Installing YYText with Carthage Source: https://github.com/ibireme/yytext/blob/master/README.md This snippet details the process of integrating YYText into an iOS project using Carthage. It involves adding the framework to the Cartfile, updating Carthage, and then importing the main header. ```Shell github "ibireme/YYText" ``` ```Shell carthage update --platform ios ``` ```Objective-C #import ``` -------------------------------- ### Installing YYText with CocoaPods Source: https://github.com/ibireme/yytext/blob/master/README.md This snippet outlines the steps to integrate YYText into an iOS project using CocoaPods. It involves adding the pod to the Podfile and running the installation commands, followed by importing the main header. ```Shell pod 'YYText' ``` ```Shell pod install ``` ```Shell pod update ``` ```Objective-C #import ``` -------------------------------- ### Implementing Custom Text Parsers with YYText (Objective-C) Source: https://github.com/ibireme/yytext/blob/master/README.md This example demonstrates how to create and attach custom text parsers, such as `YYTextSimpleEmoticonParser` and `YYTextSimpleMarkdownParser`, to `YYLabel` or `YYTextView`. It shows how to configure an emoticon parser with image mappings and how to apply a markdown parser for rich text display. ```Objective-C // 1. Create a text parser YYTextSimpleEmoticonParser *parser = [YYTextSimpleEmoticonParser new]; NSMutableDictionary *mapper = [NSMutableDictionary new]; mapper[@":smile:"] = [UIImage imageNamed:@"smile.png"]; mapper[@":cool:"] = [UIImage imageNamed:@"cool.png"]; mapper[@":cry:"] = [UIImage imageNamed:@"cry.png"]; mapper[@":wink:"] = [UIImage imageNamed:@"wink.png"]; parser.emoticonMapper = mapper; YYTextSimpleMarkdownParser *parser = [YYTextSimpleMarkdownParser new]; [parser setColorWithDarkTheme]; MyCustomParser *parser = ... // custom parser // 2. Attach parser to label or text view YYLabel *label = ... label.textParser = parser; YYTextView *textView = ... textView.textParser = parser; ``` -------------------------------- ### Customizing Text Highlights and Handling Interactions in Objective-C Source: https://github.com/ibireme/yytext/blob/master/README.md This comprehensive example demonstrates advanced text highlighting with custom configurations and user interaction handling. It shows how to create a YYTextHighlight object with a custom background border and color, assign a tap action, and then apply it to a specific text range. Furthermore, it illustrates how to capture tap and long-press actions directly on YYLabel or through YYTextViewDelegate methods, providing granular control over user interactions with highlighted text. ```Objective-C // 1. Create a 'highlight' attribute for text. YYTextBorder *border = [YYTextBorder borderWithFillColor:[UIColor grayColor] cornerRadius:3]; YYTextHighlight *highlight = [YYTextHighlight new]; [highlight setColor:[UIColor whiteColor]]; [highlight setBackgroundBorder:highlightBorder]; highlight.tapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) { NSLog(@"tap text range:..."); // you can also set the action handler to YYLabel or YYTextView. }; // 2. Add 'highlight' attribute to a range of text. [attributedText yy_setTextHighlight:highlight range:highlightRange]; // 3. Set text to label or text view. YYLabel *label = ... label.attributedText = attributedText YYTextView *textView = ... textView.attributedText = ... // 4. Receive user interactive action. label.highlightTapAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) { NSLog(@"tap text range:..."); }; label.highlightLongPressAction = ^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect) { NSLog(@"long press text range:..."); }; @UITextViewDelegate - (void)textView:(YYTextView *)textView didTapHighlight:(YYTextHighlight *)highlight inRange:(NSRange)characterRange rect:(CGRect)rect { NSLog(@"tap text range:..."); } - (void)textView:(YYTextView *)textView didLongPressHighlight:(YYTextHighlight *)highlight inRange:(NSRange)characterRange rect:(CGRect)rect { NSLog(@"long press text range:..."); } ``` -------------------------------- ### Applying Attributed Text to YYLabel and YYTextView in Objective-C Source: https://github.com/ibireme/yytext/blob/master/README.md This example illustrates how to create and apply attributed strings to YYLabel and YYTextView. It shows the process of initializing an NSMutableAttributedString, setting various text attributes like font, color, and line spacing using YYText extensions, and finally assigning the attributed string to the attributedString property of YYLabel or YYTextView. This allows for rich text formatting beyond basic plain text. ```Objective-C // 1. Create an attributed string. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"Some Text, blabla..."]; // 2. Set attributes to text, you can use almost all CoreText attributes. text.yy_font = [UIFont boldSystemFontOfSize:30]; text.yy_color = [UIColor blueColor]; [text yy_setColor:[UIColor redColor] range:NSMakeRange(0, 4)]; text.yy_lineSpacing = 10; // 3. Set to YYLabel or YYTextView. YYLabel *label = [YYLabel new]; label.frame = ... label.attributedString = text; YYTextView *textView = [YYTextView new]; textView.frame = ... textView.attributedString = text; ``` -------------------------------- ### Adjusting Text Line Position with YYTextLinePositionModifier (Objective-C) Source: https://github.com/ibireme/yytext/blob/master/README.md This example illustrates how to modify text line positions using `YYTextLinePositionSimpleModifier`. It shows both convenience methods for directly setting the modifier on `YYLabel` and a more controlled approach by applying it to `YYTextContainer` before layout generation, allowing for precise control over line height. ```Objective-C // Convenience methods: // 1. Create a text line position modifier, implements `YYTextLinePositionModifier` protocol. // 2. Set it to label or text view. YYTextLinePositionSimpleModifier *modifier = [YYTextLinePositionSimpleModifier new]; modifier.fixedLineHeight = 24; YYLabel *label = [YYLabel new]; label.linePositionModifier = modifier; // Fully control YYTextLinePositionSimpleModifier *modifier = [YYTextLinePositionSimpleModifier new]; modifier.fixedLineHeight = 24; YYTextContainer *container = [YYTextContainer new]; container.size = CGSizeMake(100, CGFLOAT_MAX); container.linePositionModifier = modifier; YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:text]; YYLabel *label = [YYLabel new]; label.size = layout.textBoundingSize; label.textLayout = layout; ``` -------------------------------- ### Embedding Text Attachments (Image, View, Layer) in Objective-C Source: https://github.com/ibireme/yytext/blob/master/README.md This snippet demonstrates how to embed various types of content as attachments within an NSMutableAttributedString for display in YYLabel or YYTextView. It shows examples of attaching a UIImage, a UIView (specifically a UISwitch), and a CALayer using yy_attachmentStringWithContent:. For each attachment, it specifies content mode, size, and vertical alignment relative to the surrounding font, allowing for rich mixed-content display. ```Objective-C NSMutableAttributedString *text = [NSMutableAttributedString new]; UIFont *font = [UIFont systemFontOfSize:16]; NSMutableAttributedString *attachment = nil; // UIImage attachment UIImage *image = [UIImage imageNamed:@"dribbble64_imageio"]; attachment = [NSMutableAttributedString yy_attachmentStringWithContent:image contentMode:UIViewContentModeCenter attachmentSize:image.size alignToFont:font alignment:YYTextVerticalAlignmentCenter]; [text appendAttributedString: attachment]; // UIView attachment UISwitch *switcher = [UISwitch new]; [switcher sizeToFit]; attachment = [NSMutableAttributedString yy_attachmentStringWithContent:switcher contentMode:UIViewContentModeBottom attachmentSize:switcher.size alignToFont:font alignment:YYTextVerticalAlignmentCenter]; [text appendAttributedString: attachment]; // CALayer attachment CASharpLayer *layer = [CASharpLayer layer]; layer.path = ... attachment = [NSMutableAttributedString yy_attachmentStringWithContent:layer contentMode:UIViewContentModeBottom attachmentSize:switcher.size alignToFont:font alignment:YYTextVerticalAlignmentCenter]; [text appendAttributedString: attachment]; ``` -------------------------------- ### Initializing YYLabel and YYTextView (Basic Usage) in Objective-C Source: https://github.com/ibireme/yytext/blob/master/README.md This snippet demonstrates the basic initialization and property setting for YYLabel and YYTextView. It shows how to configure common text display properties like frame, font, color, alignment, line break mode, and number of lines for YYLabel, and similar properties along with data detector types and placeholder text for YYTextView. These components are designed as replacements for UILabel and UITextView respectively. ```Objective-C // YYLabel (similar to UILabel) YYLabel *label = [YYLabel new]; label.frame = ... label.font = ... label.textColor = ... label.textAlignment = ... label.lineBreakMode = ... label.numberOfLines = ... label.text = ... // YYTextView (similar to UITextView) YYTextView *textView = [YYTextView new]; textView.frame = ... textView.font = ... textView.textColor = ... textView.dataDetectorTypes = ... textView.placeHolderText = ... textView.placeHolderTextColor = ... textView.delegate = ... ``` -------------------------------- ### Implementing Asynchronous Text Layout and Rendering with YYText (Objective-C) Source: https://github.com/ibireme/yytext/blob/master/README.md This snippet demonstrates how to enable asynchronous display for `YYLabel` to improve performance. It also shows how to perform text layout with `YYTextLayout` on a background thread and then update the UI on the main thread, which is crucial for complex text rendering and maintaining UI responsiveness. ```Objective-C // If you have performance issues, // you may enable the asynchronous display mode. YYLabel *label = ... label.displaysAsynchronously = YES; // If you want to get the highest performance, you should do // text layout with `YYTextLayout` class in background thread. YYLabel *label = [YYLabel new]; label.displaysAsynchronously = YES; label.ignoreCommonProperties = YES; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Create attributed string. NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:@"Some Text"]; text.yy_font = [UIFont systemFontOfSize:16]; text.yy_color = [UIColor grayColor]; [text yy_setColor:[UIColor redColor] range:NSMakeRange(0, 4)]; // Create text container YYTextContainer *container = [YYTextContainer new]; container.size = CGSizeMake(100, CGFLOAT_MAX); container.maximumNumberOfRows = 0; // Generate a text layout. YYTextLayout *layout = [YYTextLayout layoutWithContainer:container text:text]; dispatch_async(dispatch_get_main_queue(), ^{ label.size = layout.textBoundingSize; label.textLayout = layout; }); }); ``` -------------------------------- ### Enabling Debug Options for YYText Layout Visualization (Objective-C) Source: https://github.com/ibireme/yytext/blob/master/README.md This snippet shows how to configure and set shared debug options for `YYText` to visualize text layout components. It allows developers to inspect baselines, Core Text frames, line fills, and glyph borders, which is highly useful for debugging layout issues and understanding text rendering. ```Objective-C // Set a shared debug option to show text layout result. YYTextDebugOption *debugOptions = [YYTextDebugOption new]; debugOptions.baselineColor = [UIColor redColor]; debugOptions.CTFrameBorderColor = [UIColor redColor]; debugOptions.CTLineFillColor = [UIColor colorWithRed:0.000 green:0.463 blue:1.000 alpha:0.180]; debugOptions.CGGlyphBorderColor = [UIColor colorWithRed:1.000 green:0.524 blue:0.000 alpha:0.200]; [YYTextDebugOption setSharedDebugOption:debugOptions]; ``` -------------------------------- ### Calculating Text Layout and Querying Properties in YYText (Objective-C) Source: https://github.com/ibireme/yytext/blob/master/README.md This snippet demonstrates how to calculate text layout using `YYTextLayout` for a given `NSAttributedString` and container size. It also shows how to retrieve bounding box information and query specific text layout properties like line index, position, and text ranges, finally displaying the layout in a `YYLabel`. ```Objective-C NSAttributedString *text = ... CGSize size = CGSizeMake(100, CGFLOAT_MAX); YYTextLayout *layout = [YYTextLayout layoutWithContainerSize:size text:text]; // get text bounding layout.textBoundingRect; // get bounding rect layout.textBoundingSize; // get bounding size // query text layout [layout lineIndexForPoint:CGPointMake(10,10)]; [layout closestLineIndexForPoint:CGPointMake(10,10)]; [layout closestPositionToPoint:CGPointMake(10,10)]; [layout textRangeAtPoint:CGPointMake(10,10)]; [layout rectForRange:[YYTextRange rangeWithRange:NSMakeRange(10,2)]]; [layout selectionRectsForRange:[YYTextRange rangeWithRange:NSMakeRange(10,2)]]; // text layout display YYLabel *label = [YYLabel new]; label.size = layout.textBoundingSize; label.textLayout = layout; ``` -------------------------------- ### Setting Text Highlight with Convenience Method in Objective-C Source: https://github.com/ibireme/yytext/blob/master/README.md This snippet demonstrates a convenient way to add text highlighting to an NSMutableAttributedString using yy_setTextHighlightRange:. It allows specifying a range, text color, background color, and a tap action block directly. The tapAction block provides a callback when the highlighted text is tapped, enabling interactive elements within the text. ```Objective-C [text yy_setTextHighlightRange:range color:[UIColor blueColor] backgroundColor:[UIColor grayColor] tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){ NSLog(@"tap text range:..."); }]; ``` -------------------------------- ### Controlling Text Container Properties in YYLabel and YYTextView (Objective-C) Source: https://github.com/ibireme/yytext/blob/master/README.md This snippet illustrates how to customize the text container's shape and behavior for `YYLabel` and `YYTextView`. It covers setting custom paths for text flow, exclusion paths for text wrapping around specific areas, and adjusting text insets and vertical form for layout control. ```Objective-C YYLabel *label = ... label.textContainerPath = [UIBezierPath bezierPathWith...]; label.exclusionPaths = @[[UIBezierPath bezierPathWith...];,...]; label.textContainerInset = UIEdgeInsetsMake(...); label.verticalForm = YES/NO; YYTextView *textView = ... textView.exclusionPaths = @[[UIBezierPath bezierPathWith...];,...]; textView.textContainerInset = UIEdgeInsetsMake(...); textView.verticalForm = YES/NO; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.