【问题标题】:drawTextInRect: in UITextView classdrawTextInRect:在 UITextView 类中
【发布时间】:2014-02-24 10:37:50
【问题描述】:

我使用 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 创建类似的解决方案?

【问题讨论】:

    标签: ios iphone ios7 uitextview


    【解决方案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;
    

    享受

    【讨论】:

    • 如何设置填充颜色?笔划内的颜色?因为这里会自动显示 clearColor
    【解决方案2】:

    彼得,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;
    

    【讨论】:

      猜你喜欢
      • 2010-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多