简体   繁体   English

在CGRect外部绘制文本。

[英]Draw text outside of CGRect.

I am using Apple's Sample Code TheElements for this question. 我正在使用Apple的示例代码TheElements来解决此问题。 How would I go about drawing text outside of the elementSymbolRectangle. 我将如何在elementSymbolRectangle之外绘制文本。 For example I would like to display the Elements Name, but I need it to be outside of the elementSymbolRectangle. 例如,我想显示元素名称,但是我需要它在elementSymbolRectangle之外。 I am new to programming and would appreciate any help. 我是编程新手,不胜感激。

Thanks 谢谢

 - (id)initWithFrame:(CGRect)frame {

        if (self = [super initWithFrame:frame]) {
            [self setAutoresizesSubviews:YES];
            [self setupUserInterface];

            // set the background color of the view to clearn
            self.backgroundColor=[UIColor clearColor];
        }
        return self;
    }

    - (void)jumpToWikipedia:(id)sender {

        // create the string that points to the correct Wikipedia page for the element name
        NSString *wikiPageString = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/%@", self.element.name];
        if (![[UIApplication sharedApplication] openURL:[NSURL URLWithString:wikiPageString]])
        {
            // there was an error trying to open the URL. for the moment we'll simply ignore it.
        }
    }

    - (void)drawRect:(CGRect)rect {

        // get the background image for the state of the element
        // position it appropriately and draw the image
        //
        UIImage *backgroundImage = [self.element stateImageForAtomicElementView];
        CGRect elementSymbolRectangle = CGRectMake(0, 0, [backgroundImage size].width, [backgroundImage size].height);
        [backgroundImage drawInRect:elementSymbolRectangle];

        // all the text is drawn in white
        [[UIColor whiteColor] set];

        // draw the element number
        UIFont *font = [UIFont boldSystemFontOfSize:32];
        CGPoint point = CGPointMake(10,5);
        [[NSString stringWithFormat:@"%@", self.element.atomicNumber] drawAtPoint:point withFont:font];

        // draw the element symbol
        CGSize stringSize = [self.element.symbol sizeWithFont:font];
        point = CGPointMake((self.bounds.size.width-stringSize.width-10),5);
        [self.element.symbol drawAtPoint:point withFont:font];

This solved my problem. 这解决了我的问题。

UILabel *scoreLabel = [ [UILabel alloc ] initWithFrame:CGRectMake((self.bounds.size.width / 2), -50.0, 100.0, 100.0) ];
scoreLabel.textAlignment =  UITextAlignmentCenter;
scoreLabel.textColor = [UIColor whiteColor];
scoreLabel.backgroundColor = [UIColor blackColor];
scoreLabel.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(36.0)];
[self addSubview:scoreLabel];
scoreLabel.text = [NSString stringWithFormat: @"Test"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将UIImage转换为CIImage以裁剪为CGRect。 AVFoundation - Convert a UIImage to a CIImage to crop to a CGRect. AVFoundation 是否可以在由CATiledLayer支持的UIView子类中的draw(_ rect:CGRect)函数中的矩形边界外画线? - Is it possible to draw a line outside rect bounds in draw(_ rect: CGRect) function in a UIView subclass that is backed by a CATiledLayer? setNeedsDisplay()没有调用draw(_ rect:CGRect) - setNeedsDisplay() is not calling draw(_ rect: CGRect) CGRect 在 viewDidLoad 之外不能正常工作 - CGRect is not working in closure outside of viewDidLoad 使用CoreGraphics在UIView中绘制图像,并在drawRect外部绘制文本 - Draw image in UIView using CoreGraphics and draw text on it outside drawRect 在自定义视图中未调用draw(_ rect:CGRect) - draw(_ rect: CGRect) not getting called in custom view draw(_ rect:CGRect)绘制不超过一定大小 - draw(_ rect: CGRect) not drawing above certain size 使用CAGradientLayer和drawRect:(CGRect)绘制顺序 - Draw order using CAGradientLayer & drawRect:(CGRect) draw(_ rect:CGRect)实际如何工作? - How draw(_ rect: CGRect) actually work? draw(_ rect: CGRect) 在不同的 iOS 版本上的失真 - draw(_ rect: CGRect) distortion on different iOS versions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM