- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
快速版:为什么 UILabel 的“center”属性的值在其 Size 属性绑定(bind)到其父容器的顶部和底部之间发生变化,我该怎么做才能改变或者绕过这种行为?
包含详细信息的长版:我正在像这样在两个标签之间画一条线(黄线):
为了计算直线的终点,我使用了 UILabels 的中心属性(在这张照片中,标签表示 2.5kHz 和 20Hz),然后是标签高度的 + 或 - 一半。此技术在下面的示例/图片中效果很好。然而,在我将屏幕尺寸切换为 iPhone5(更高)的设计阶段,我通过在尺寸检查中更改它来将“20 Hz”标签绑定(bind)到底部而不是顶部,如下面两张图片所示。请注意,在这两种情况下,原点仍在左上角:
这会改变 center 属性的行为方式,因此会产生以下行:
我在这里重新定位了 UILabel 只是为了演示。请注意,行尾应位于标签顶部的中心:
我打印了标签的中心点。一次绑定(bind)到顶部,然后再次绑定(bind)到底部:
绑定(bind)到顶部{290, 326.5}
绑定(bind)到底部{290, 370.5}
我尝试了一种解决方法,即使用 origin.y 并从那里进行偏移,但效果相同。
我希望它在完成后看起来像这样,但在更高的 iPhone 5 屏幕上:
编辑:根据@ott 的要求,添加计算直线终点的代码片段
- (void)viewDidLoad
{
[super viewDidLoad];
CGPoint begin = self.frequencyMaxLabel.center;
begin.y += self.frequencyMaxLabel.frame.size.height/2.0;
CGPoint end = self.frequencyMinLabel.center;
end.y -= self.frequencyMinLabel.frame.size.height/2.0;
// This is an object of two CGPoints to represent a line
VWWLine* frequenciesLine = [[VWWLine alloc]
initWithBegin:begin
andEnd:end];
// Pass the line to a UIView subclass where it is drawn
[self.configView setLineFrequencies:frequenciesLine];
[frequenciesLine release];
// .... other code truncated for example
}
以及画线的代码(在 UIView 子类中,来自上面代码段的 self.configView)
- (void)drawRect:(CGRect)rect
{
CGContextRef cgContext = UIGraphicsGetCurrentContext();
CGContextBeginPath(cgContext);
CGContextSetLineWidth(cgContext, 2.0f);
CGFloat yellowColor[4] = {1.0, 1.0, 0.0, 1.0};
CGContextSetStrokeColor(cgContext, yellowColor);
[self drawLineWithContext:cgContext
fromPoint:self.lineFrequencies.begin
toPoint:self.lineFrequencies.end];
CGContextStrokePath(cgContext);
//...... truncated. Draw the other lines next
}
最佳答案
在系统针对当前设备和界面方向布置您的 View 之前,您会收到 viewDidLoad
消息。将该代码移动到 viewDidLayoutSubviews
。
此外,您可能想查看 CAShapeLayer
。
关于iphone - ULabel 的 center 属性的值根据它绑定(bind)到的边而变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14081299/
这个问题在这里已经有了答案: Flexbox: Align between bottom and center? [duplicate] (2 个答案) 关闭 6 年前。 我正在尝试使用 flexb
用于居中元素的标签是 或 ? 我知道 或 标签不再使用,但哪一个是正确的? 我很迷惑。 Visual Studio 代码说 错了,但只有适用于 Microsoft Edge。 编辑:Microsof
我使用的是标准匹配用户界面和两台 iPad iOS6。问题是当我在第一台设备中创建新的匹配项时,第二台设备应该在我查看匹配用户界面时看到现有的匹配项,但事实并非如此。我确定我的代码是正确的。这是方法:
在我的项目的两个不同的类(都扩展 JFrame)中,我尝试这样做: header = new JLabel("Header"); header.setHorizontalAlignme
我已经在我的网站中实现了以下 jQuery 视差效果(取自 http://www.brandaiddesignco.com/insights/parallax-scrolling-made-easy/
每当我在我的 css 中居中文本时,它也会使图像居中。这是为什么?文本位于一个 div(“左”)内,图像位于第二个 div(“右”)内。这是 HTML - BANKS 还有
我现在有这段代码: 1 enE jk 5.6 mtE hp 6.4 hpE eb HML reE pm 514 hpE e
我只是制作一个页面或类似的东西。我想让我的按钮居中,但它不起作用。 我尝试用不同的方法将它居中,但它不起作用。看看代码。它有但它并没有将它置于整个页面的中心但只是喜欢......我不知道。照片:htt
我想知道如何将此页面中的字形居中,它们是使用@font-face 的图标字体,但即使在我将 text-align:center 应用于包含每个字形的 anchor 之后,它们也不是居中。 它们显示为左
考虑以下 HTML 片段, Hyperlink 如上所示,由于“...”标签,所有内容都将居中对齐。但是,我不希望带有“id=three”的 HTML 元素的中心对齐
我有以下 html: Site Map Privacy Policy Terms & Condi
我在将我正在处理的布局中的按钮上的文本居中时遇到问题。请查看下面的屏幕截图(在 eclipse 的“图形布局”选项卡中截取): 我不确定是什么原因造成的。我试着玩弄按钮的各个布局属性,但这没有任何效果
我正在尝试使用 Bootstrap 组装页脚。由于某些原因,尽管确保应用了 text-align: center;,但页脚链接看起来略微偏离中心。有没有办法确保所有文本元素确实正确对齐? HTML
我在使用 flexbox 工具 align-items:center; 时遇到了问题,它在中间并不完美,但文本、图标等的像素太高了……有人知道如何解决这个问题? screenshot how it l
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and t
GameKit 是否允许您以编程方式邀请特定的 Game Center friend 参加比赛,即不提供 GC ViewController?以下 handleInviteFromGameCenter
我有一个布局问题。 假设我有一个 RelativeLayout 出现在我的屏幕底部。在此,我想添加 2 个 TextView ,一个在中心,一个在顶部中心,一个在底部中心。 |------------
我有一个启动画面的 Activity layout.xml,它基本上显示了我想要的内容。但是如果我在更大或更小的屏幕设备上打开应用程序,它就不再居中了。 有没有办法在元素周围放置一个包装器并将其在屏幕
尝试了几十种方法后,我自己也想不出解决方案 #banner .slide { position: static; top: 0px; left: 0px; z-inde
您将如何修复以下传递过多参数的错误代码? void helper1(int p1, int p3, int p5, int p7, int p9, int p10) { // ... } void
我是一名优秀的程序员,十分优秀!