首页 \ 问答 \ 如果 :(为什么没有赋值?)(If : (Why is there no value assigned?))

如果 :(为什么没有赋值?)(If : (Why is there no value assigned?))

任何人都可以告诉为什么在if begin:的条件之后没有分配值if begin:而只是用冒号结束? 我可以知道它是什么意思吗?

if begin:
    begin = int(begin)
    end = int(raw_input("End:"))
    print "word[", begin,":",end,"] ",
    print word[begin:end]

raw_input("\n\nPress the Enter Key to exit.") 

谢谢!


Can anybody tell why is there no value assigned to begin after the condition at if begin: and just end with a colon instead? May I know what does it mean?

if begin:
    begin = int(begin)
    end = int(raw_input("End:"))
    print "word[", begin,":",end,"] ",
    print word[begin:end]

raw_input("\n\nPress the Enter Key to exit.") 

Thank You!


原文:https://stackoverflow.com/questions/29001294
更新时间:2022-10-24 13:10

最满意答案

在你的问题中有两个问题:

上面的代码不起作用。 为什么?

起初,我认为文本没有出现,因为它是用白色绘制的。 虽然它可能仍然如此,但这不是唯一的问题。

您将文本绘制到(r)中的矩形与您正在绘制的图层的边界相关。即,如果要绘制图层的左上角,则矩形的原点应为(0, 0)。 上面的代码仍然没有工作。

如果你检查CGSize drawInRect:withFont:... returns(渲染文本的大小),你可以看到渲染文本的大小是0。 这表示矩形绘制的高度为100是不够的。 增加矩形的高度解决了这个问题,现在文本是可见的。 如果你在图层中绘制一些文本,也许最明显的矩形是图层的边界( CGRect r = [layer bounds]; )。

我做了什么来实现它的工作:

  • 改变了文字颜色
  • r原点更改为(0,0)
  • r高度增加

有什么不同 [...] ?

首先,你要将文本绘制到图层的图形上下文中(drawLayer:inContext:应该做什么),但在秒示例中,您将向图层的层次结构中添加另一图层,而不是在图层中绘制图层。 每当您的图层重新绘制时,都会添加一个新的文本图层。 所以实际上没有任何东西被绘制到图层上下文中。

接下来发生的事情是,在图层自己绘制之后(空),图层的子图层将自己绘制出来,并且它们的内容将在图层顶部进行组合。 这就是为什么你会看到文字图层的文字。

如果您添加这样的文本图层,您可以通过不必进行任何手动绘图来提高代码效率。 这就要求文本图层在某个早期点(只有一次)被添加到图层中,并且您不需要执行任何自定义绘图(它通常很慢)。


There are two questions in you question:

The above code is not working. Why?

At first I thought that the text didn't show up because it was drawn with white on white. While it may still have been so, that is not the only problem.

The rectangle that you are drawing your text into (r) is relative to the bounds of the layer you are drawing in. I.e. if you want to draw in the top left corner of the layer, the origin of the rect should be (0, 0). The above code is still not working though.

If you inspect the CGSize that drawInRect:withFont:... returns (the size of the rendered text) you can see that the size of the rendered text is 0 wide. This is an indication that the height of 100 for the rectangle to draw in is not enough. Increasing the height of the rectangle solves this and now the text is visible. If you are drawing some text inside a layer, maybe the most sensible rectangle to draw in is the bounds of the layer (CGRect r = [layer bounds];).

What I did to make it work:

  • Changed text color
  • Changed origin of r to (0,0)
  • Increased height of r

What is the difference [...] ?

In the first you are drawing text into the graphics context of the layer (what drawLayer:inContext: is supposed to do) but in the seconds example you are adding another layer to the hierarchy of the layer instead of drawing inside it. Every time your layer will redraw, a new text layer will be added to it. So nothing is actually drawn into the layers graphics context.

What happens next, after your layer has drawn itself (empty), is that the sublayers of your layers draw themselves and their content is composed on top of your layer. That is why you see the text of the text layer.

If you add a text layer like this you can make your code more efficient by not having to do any manual drawing. This requires that the text layer is added to your layer at some earlier point (only once) and that you don't do any custom drawing (it's often slow).

相关问答

更多
  • 不要使用字符串作为属性字典键,使用框架提供的常量。 由NSFontAttributeName替换@"NSFontAttributeName"用NSFontAttributeName替换@"NSFontAttributeName" 。 Do not use strings for your attributes dictionary keys, use the constants provided by the framework. Replace @"NSFontAttributeName" by NSFo ...
  • 垂直对齐,你必须自己做((视图的高度+字符串的高度)/ 2)。 水平对齐可以做: NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; style.alignment = NSTextAlignmentCenter; NSDictionary *attr = [NSDictionary dictionaryWithObject:style forKey:NSParagraphStyle ...
  • 设置文本的段落样式(包括换行模式,文本对齐等)有一个关键。 来自文档 : NSParagraphStyleAttributeName 此属性的值为NSParagraphStyle对象。 使用此属性将多个属性应用于一系列文本。 如果不指定此属性,则该字符串使用默认段落属性,由NSParagraphStyle的defaultParagraphStyle方法NSParagraphStyle 。 所以,您可以尝试以下操作: UIFont *font = [UIFont fontWithName:@"Courier" ...
  • 我的书提出了两点建议。 如果您知道掩码绘图的边界路径,则可以使用CGPathContainsPoint 。 或者,如果所讨论的图层在外部是透明的并且在内部是不透明的,则可以检查点击位置处的像素以查看它是否透明。 http://www.apeth.com/iOSBook/ch18.html#_hit_testing 向下滚动到“图纸的命中测试”部分。 My book gives two suggestions. If you know the bounding path of the mask drawing ...
  • 如果阴影是静态的,那么最好的方法是使用两个UIImageView 。 它比CALayer更聪明,如何处理静态图像(虽然我不知道这是否会在这里产生影响),否则将具有与CALayer相同的好处,例如在GPU上完成所有合成在CPU上(如您的方法2所要求的那样)。 If the shadows are static, then the best way is to use two UIImageViews. It's even smarter than CALayer about how to deal with ...
  • 在你的问题中有两个问题: 上面的代码不起作用。 为什么? 起初,我认为文本没有出现,因为它是用白色绘制的。 虽然它可能仍然如此,但这不是唯一的问题。 您将文本绘制到(r)中的矩形与您正在绘制的图层的边界相关。即,如果要绘制图层的左上角,则矩形的原点应为(0, 0)。 上面的代码仍然没有工作。 如果你检查CGSize drawInRect:withFont:... returns(渲染文本的大小),你可以看到渲染文本的大小是0。 这表示矩形绘制的高度为100是不够的。 增加矩形的高度解决了这个问题,现在文本是 ...
  • FWIW, 我经常很快地将CALayer“打开和关闭”,没有任何问题。 所以你可以做到这一点,如果你想! 删除它们并快速重新创建它们似乎不会导致任何问题。 (这不是一个很大的资源用户,我从来没有见过这样做的其他问题。)所以,如果你想要的话,那么一定要这么做! 我其实不明白“命名”图层的含义 - 当然, 就像iVars他们有一个名字 ! 你必须给你的所有CALayers命名 。 以下是一些典型的产品代码(来自.h文件): CALayer *rearLayer; CALayer ...
  • 通常,您希望尽可能避免覆盖drawRect,尤其是对于您正在设置动画的CALayer对象。 这往往导致非常可怕的表现。 你是什么意思“drawRect:方法只绘制屏幕上的内容?” 它只绘制当前可见的字符串部分? I ended up using an NSTimer. Not the most beautiful solution, but at least it works.
  • 这是绘制文本的代码,但这不是您通常在iOS中向屏幕添加文本的方式。 在iOS中,用于向屏幕添加文本的模型通常只是将UILabel添加到视图控制器的视图中,例如,在viewDidLoad ,您可以执行以下操作: - (void)viewDidLoad { [super viewDidLoad]; UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds]; label.textColor = [UIColor r ...
  • 您需要使用drawInRect:withAttributes:之类的方法之一,并使用属性字符串属性来设置线截断样式。 尝试: NSMutableParagraphStyle *ps = [[NSMutableParagraphStyle alloc] init]; [ps setLineBreakMode:NSLineBreakByTruncatingTail]; [attributes setObject:ps forKey:NSParagraphStyleAttributeName]; You nee ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)