drawTextInRect:在UITextView类中

时间:2014-01-31 16:28:54

标签: ios iphone ios7 uitextview

我使用UITextField类中的drawTextInRect:方法创建轮廓文本(带有黑色边框/存储效果的文本)。

但我需要类似于在UITextView中创建相同的轮廓文本,但此处不包含drawTextInRect:方法。我怎样才能为textView做这个?

这是我在UITextField类中使用的代码:

- (void)drawTextInRect:(CGRect)rect {
    CGContextRef c = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(c, 1);
    CGContextSetLineJoin(c, kCGLineJoinRound);

    CGContextSetTextDrawingMode(c, kCGTextFill);
    self.textColor = [UIColor whiteColor];
    [super drawTextInRect:rect];

    CGContextSetTextDrawingMode(c, kCGTextStroke);
    self.textColor = [UIColor blackColor];
    [super drawTextInRect:rect];
}

如何在UITextView类中为textView创建类似的解决方案?

2 个答案:

答案 0 :(得分:1)

您的目标是iOS 6.0+吗? 这样的事情可能会起作用,或许:

        NSAttributedString *yourString = [[NSAttributedString alloc] initWithString:@"Your text" attributes:@{
                                          NSStrokeColorAttributeName:[UIColor redColor],
                                          NSStrokeWidthAttributeName:[NSNumber numberWithFloat:1.0],
                                          NSFontAttributeName:[UIFont systemFontOfSize:24.0f]
                                          }];
        yourTextField.attributedText = yourString;

享受

答案 1 :(得分:0)

Peter,对NSStrokeWidthAttributeName使用负值。在这种情况下,您可以为文本设置任何填充颜色。

    NSAttributedString *yourString = [[NSAttributedString alloc] initWithString:@"Your text" attributes:@{
                                              NSStrokeColorAttributeName:[UIColor redColor],
                                              NSStrokeWidthAttributeName:[NSNumber numberWithFloat:-1.0],
                                              NSFontAttributeName:[UIFont systemFontOfSize:24.0f],
                                              NSForegroundColorAttributeName:[UIColor greenColor]
                                              }];
            yourTextField.attributedText = yourString;