代码之家  ›  专栏  ›  技术社区  ›  Evolve

iOS-如何在UIImageView上添加显示字符串

  •  0
  • Evolve  · 技术社区  · 15 年前

    如何在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);
    }
    

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

    1 回复  |  直到 15 年前
        1
  •  0
  •   kovpas    15 年前

    我将重写UIImageView的drawRect:method:

    - (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] 方法-它工作得很好。