【问题标题】:iOS - How to add display a string on a UIImageViewiOS - 如何在 UIImageView 上添加显示字符串
【发布时间】:2010-10-16 07:27:36
【问题描述】:

如何在 UIImage 视图上显示字符串?

好的,我现在知道了:

- (void)viewDidLoad {
  [super viewDidLoad];
  playerPositions = [[NSArray alloc] initWithObjects:box1,box2,box3,box4,nil];
  [box3 drawRect:box3.frame];
}

- (void)drawRect:(CGRect)rect {
  [super drawRect:rect];

  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextSetFontSize(context, 11.5);

  CGContextSelectFont(context, "Helvetica", 10.5, kCGEncodingMacRoman);
  CGContextShowText(context, "Tys", 5);
  CGContextShowTextAtPoint(context, 50, 60, "value", 5);
}

但我测试时没有显示任何文字。

【问题讨论】:

  • 我不确定问题出在哪里 - 我发布的代码对我来说效果很好。另外,请确保 CGContextShowText 和 CGContextShowText 中的最后一个参数是您要绘制的文本的 strlen。
  • 顺便说一句,在 viewDidLoad 中直接调用 drawRect 是完全错误的。如果你想重绘组件,你应该调用 [box3 setNeedsDisplay];
  • 您也可以尝试在此处查看用法示例:codesearch.google.com/…

标签: iphone ios uiimageview


【解决方案1】:

我会覆盖 UIImageView 的 drawRect: 方法:

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFontSize(context, 11.5)

    CGContextSelectFont(context, "Helvetica", 10.5, kCGEncodingMacRoman)
    CGContextShowText(context, "value", 5)
    CGContextShowTextAtPoint(context, 50, 60, "value", 5)
}

更新:

昨天我遇到了同样的问题。以 [NSString drawAtPoint:withFont] 方法结束 - 它完美运行。

【讨论】:

  • 我无法轻松地将代码粘贴到这些 cmets 中,因此我将其编辑到问题中。现在我有了这段代码,我该如何有效地调用它?还没有为我工作,见上文。我哪里错了?
猜你喜欢
  • 2012-11-13
  • 2015-10-05
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多