6

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

在此处输入图像描述

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

CTLineBreakMode linebreakmode = kCTLineBreakByCharWrapping;

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

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

4

3 回答 3

1
// 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];
}
于 2015-04-06T05:57:05.593 回答
0

在可编辑的文本字段 (TextArea) 中制作一个带有红色发光文本的 HTML 文件。

将所有元素的背景设置为透明。

将其制成一个字符串并将其加载到 UIWebView 中,该 UIWebView 位于您要放置文本的图像之上。

将 HTML 字符串加载到 UIWebView 中,并使 UIWebView 本身透明。

*此外,您可能需要将 UIWebView 设为属性并对其进行综合。

这是代码(没有综合和属性)

。H:

@interface ViewController : UIViewController <uiwebviewdelegate></uiwebviewdelegate> {
    IBOutlet UIWebView *webView;
    //... other objects you already have can go here too..
}

米:

self.webView.delegate = self;
NSString *htmlstring = [[NSString alloc] initWithFormat:@"<html><body style='background-color: transparent;'><textarea name='myTextArea'cols='20' rows='10' style='text-shadow: 0 0 0.2em #F87, 0 0 0.2em #F87; background-color:transparent; border:none'>You Can Edit This Text.</textarea></body></html>"];
[self.webView loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
self.webView.opaque = NO;
self.webView.backgroundColor = [UIColor clearColor];

编辑:这是“webView”的属性和合成代码

@property(nonatomic,retain)IBOutlet UIWebView *nubrowser; //put in .h file at end of "@Interface" function after the closing curly bracket ("}")

@synthesize webView; //put in .m file directly after "@implementation" function
于 2013-01-10T18:12:54.037 回答
0

只需用 CSS 制作一个字符串

NSString *htmlstring = [[NSString alloc] initWithFormat:@"<html><font style=\"text-shadow: 0 0 0.2em #F87, 0 0 0.2em #F87\">%@</font></html>", YourTextView.text, nil];

^这将使 TextView 中的任何文本在网页上发光。(如果你愿意,你也可以给它一个黑色背景......使用这个:

NSString *htmlstring = [[NSString alloc] initWithFormat:@"<html><body bgcolor=\"#000000\"><font style=\"text-shadow: 0 0 0.2em #F87, 0 0 0.2em #F87\">%@</font></body></html>", YourTextView.text, nil];

现在将其保存为 html 文件并将其加载到 UIWebView 中。或者通过 javascript 将其注入 URL 并将该 URL 加载到 UIWebView 中。

设置 UIWebView 框架和位置以匹配您的 UITextView 的框架和位置,这样它就会在它上面,并显示带有黑色背景的红色发光文本。

于 2013-01-10T04:12:02.660 回答