gpt4 book ai didi

ios - 向 UITextView 添加轮廓/描边

转载 作者:可可西里 更新时间:2023-11-01 04:53:48 27 4
gpt4 key购买 nike

我想在用户键入时向可编辑的 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com