UITextView, NSAttributedString, and Custom Attributes

I searched a lot on Stack Overflow, but couldn't find a solution. Perhaps I just misinterpreted some of the answers.

I created UITextView, and I use NSAttributedStringsto work with a UITextView, which is fine.

Now, after adding a custom attribute, I'm stuck.

Where can I connect to display my custom attribute in UITextView? Is there a delegate method, or do I need to create my own UITextViewand overwrite the method?

+4
source share
3 answers

You can customize NSLayoutManagerand implement it -drawGlyphsForGlyphRange:atPoint:.

,

textView init:

NSTextStorage *textStorage = [NSTextStorage new];
CustomLayoutManager *layoutManager = [[CustomLayoutManager alloc] init];
CGSize containerSize = CGSizeMake(self.view.bounds.size.width,  CGFLOAT_MAX);
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:containerSize];

textContainer.widthTracksTextView = YES;
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];

self.textView = [[UITextView alloc] initWithFrame:yourFrame textContainer:textStorage];

:

NSMutableAttributedString *mAttrStr = [[NSMutableAttributedString alloc] initWithString:@"SampleText"];
[mAttrStr addAttribute:YourCustomAttrbuteName value:[UIColor redColor] range:NSMakeRange(0, mAttrStr.length)];  //for example, you want a custom background with a corner radius
[self.textView.textStorage appendAttributedString:mAttrStr];

CustomLayoutManager.m

-(void)drawGlyphsForGlyphRange:(NSRange)glyphsToShow atPoint:(CGPoint)origin {
    NSRange range = [self characterRangeForGlyphRange:glyphsToShow
                                 actualGlyphRange:NULL];
    //enumerate custom attribute in the range
    [self.textStorage enumerateAttribute:YourCustomAttributeName inRange:range options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
        if (value) {
            UIColor *color = value; //the color set above

            NSRange glyphRange = [self glyphRangeForCharacterRange:range
                                          actualCharacterRange:NULL];
            NSTextContainer *container = [self textContainerForGlyphAtIndex:glyphRange.location
                                        effectiveRange:NULL];

            //draw background
            CGContextRef context = UIGraphicsGetCurrentContext();
            CGContextSaveGState(context);
            CGContextTranslateCTM(context, origin.x, origin.y);
            [color setFill];
            CGRect rect = [self boundingRectForGlyphRange:glyphRange inTextContainer:container];

            //UIBezierPath with rounded
            UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:100];
            [path fill];
            CGContextRestoreGState(context);
            //end draw

            [super drawGlyphsForGlyphRange:range atPoint:origin];
        }
        else {
            [super drawGlyphsForGlyphRange:range atPoint:origin];
        }

    }];
}

"SampleText" .

+3

, ,

    let attributedString = NSMutableAttributedString(string:"Test string to add attributes")
    attributedString.addAttributes([NSForegroundColorAttributeName:UIColor.greenColor()], range: NSMakeRange(0, attributedString.string.characters.count))
    textView.attributedText = attributedString

Objective-C

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:@"Test string to add attributes"];
[attributedString addAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]} range:NSMakeRange(0, attributedString.string.length)];
textView.attributedText = attributedString;

, .

-1

attributes textView, , UITextView initmethod - , UITextView !! custom attributes , , . , .

-1

Source: https://habr.com/ru/post/1653899/


All Articles