ios - 向 UITextView 添加轮廓/描边

标签 ios objective-c core-graphics uitextview core-text

我想在用户键入时向可编辑的 UITextView 文本添加轮廓或描边。完全像模因:http://t.qkme.me/3oi5rs.jpg

enter image description here

我必须使用 UITextView 因为我需要多行支持。我已经尝试了所有方法并得出结论,我必须使用 CoreText。我得到了几乎所有的解决方案,但卡在了 textview 中的文本换行的位置。我在 UITextViewsubview 中的 drawRect 例程正确绘制轮廓文本,直到文本换行。即使用户输入的文本被换行,我绘制的轮廓文本也不会换行。这是我对 drawRect 方法的实现:https://gist.github.com/4498988 .在第 29 行,我使用字符包装:

CTLineBreakMode linebreakmode = kCTLineBreakByCharWrapping;

我已经尝试过 wordwrapping 选项(这也是默认选项)没有用。

我的问题是: 如何让我绘制的文本正确换行,使其显示为键入文本的轮廓?

最佳答案

// make your UITextView as a subclass of UITextView
// and override -drawRect: method in your UITextView subclass 
- (void)drawRect:(CGRect)rect
{
    self.textColor = [UIColor clearColor];

    [self setTypingAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.font, NSFontAttributeName, _textBaseColor, NSForegroundColorAttributeName, nil ]]; // _textBaseColor is any color of your choice
    CGRect newRect = CGRectMake(0, 0, CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetTextDrawingMode(context, kCGTextStroke);
    // Make the thickness of the outline shadow. and increase the y position of shadow rect by 2.
    CGContextSetLineWidth(context, (TEXTOUTLINE_PERCENT/100 * self.font.pointSize)+1); // TEXTOUTLINE_PERCENT can be 25.
    CGContextSetLineJoin(context, kCGLineJoinRound);
    CGContextSetLineCap(context, kCGLineCapButt);
    CGContextSetStrokeColorWithColor(context, [UIColor clearColor].CGColor);
    CGRect shadowrect = newRect;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
    {
        shadowrect.origin.y += 0.5;
    }
    else
    {
        shadowrect.origin.y += 2;
    }

    [self.text drawInRect:shadowrect withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.font, NSFontAttributeName, SHADOW_COLOR , NSForegroundColorAttributeName, nil]]; // SHADOW_COLOR can be [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.3]

    // Make the thickness of the outline border of the text.
    CGContextSetLineWidth(context, TEXTOUTLINE_PERCENT/100 * self.font.pointSize); // TEXTOUTLINE_PERCENT can be 25.
    CGContextSetStrokeColorWithColor(context, [[UIColor clearColor] CGColor]);
    [self.text drawInRect:newRect withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.font, NSFontAttributeName, self.textOutlineColor , NSForegroundColorAttributeName,  nil]];

    // Draw filled text. This is the actual text.
    CGContextSetTextDrawingMode(context, kCGTextFill);
    CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
    [self.text drawInRect:newRect withAttributes:[NSDictionary dictionaryWithObjectsAndKeys:self.font, NSFontAttributeName, _textBaseColor, NSForegroundColorAttributeName,_textBaseColor, NSStrokeColorAttributeName, nil]];
    [super drawRect:rect];
}

关于ios - 向 UITextView 添加轮廓/描边,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14250148/

相关文章:

ios - React Native TextInput 两个 textContentType、emailAddress 和 username

android - 是否有适用于 Android 和 iPhone 的框架

ios - 通过点击 iOS 中的红色减号图标删除 tableview 单元格

ios - 绘制一个带有一些阴影偏移的圆边平行四边形

iPhone 在 Quartz 绘图与预烘焙图像方面的性能差异(我猜可以简化为 Quartz 与 Quartz)

ios - UITableViewStyleGrouped 不起作用

iphone - 将 uitableview 重新加载到底部单元格

iphone - 如何打印单元格标签中的数组值?

objective-c - 来回切换 NSTextView 的文本存储

cocoa-touch - 在 UITableViewCell 中使用渐变背景