向UITextView添加大纲/描边

我想为用户键入的可编辑的UITextView文本添加一个大纲或描边。 完全像memes: http : //t.qkme.me/3oi5rs.jpg

在这里输入图像说明

我必须使用UITextView因为我需要多行支持。 我已经尝试了所有的方法,并得出了我必须使用CoreText的结论。 我得到了几乎所有的解决scheme工作,但卡在文本视图中的文本包装点。 我在UITextViewsubviewdrawRect例程正确地绘制轮廓文本,直到文本换行。 即使当文本用户input被包装,我绘制的大纲文本不。 这是我的drawRect方法的实现: https : //gist.github.com/4498988 。 在第29行,我正在使用char封装:

 CTLineBreakMode linebreakmode = kCTLineBreakByCharWrapping; 

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

我的问题是:如何获取我正确绘制的文本,使其显示为input文本的轮廓?

只需用CSS创build一个string

 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相匹配,以便将其放在顶部,并显示黑色背景的红色发光文字。

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

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

把它变成一个string,并把它加载到你想放置文本的图像上面的UIWebView。

将HTMLstring加载到UIWebView中,并使UIWebView本身透明。

*你也可能需要使你的UIWebView属性和合成它。

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

。H:

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

.M:

 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]; 

编辑:这里是属性和综合的“networking视图”

 @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 
 // 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]; }