UIButtons titleLabel剪辑由CGAffineTransformMakeRotate()旋转后的文本 [英] UIButtons titleLabel clips Text after being rotated by CGAffineTransformMakeRotate()

查看:221
本文介绍了UIButtons titleLabel剪辑由CGAffineTransformMakeRotate()旋转后的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个iOS-6应用程式。我有一个UIViewController的视图需要固定方向(肖像模式)。但是当手机旋转时,该视图上的一个控件需要移动和旋转(以便它将始终位于左上角,并且其文本将可读)。我通过使用我的控件的frame-property(它是一个自定义视图,稍后更多)移动控件(一个UIView),然后使用CGAffineTRansformMakeRotate()后,因为我知道它不建议使用框架后旋转视图。一切都很好,到目前为止,但这里的东西:该自定义视图有三个类型UIButtonTypeCustom UIButtons作为其子视图。因为我旋转视图,但不能旋转视图内的按钮(他们不是正方形),我需要旋转的按钮的标签为文本在新deviceOrientation可读。但它不会工作得很好。文本将被旋转,正如我打算,但它将被剪辑的titleLabel,因为titleLabel有错误的帧。我通过应用边界到标签检查了这一点。所以我需要改变titleLabels框架,对吧?但我该怎么做呢?我尝试使用 [titleLabel setFrame:frameThatFits]; 设置它,但没有效果。 (frameThatFits是我创建的CGRect)。也,调用 [button.titleLabel sizeToFit]; 没有我可以看到的效果。
我使用 [button setTitle:title forControlState:UIControlStateNormal]; 设置标题。

I am developing a iOS-6 app. I have a UIViewController with a view that needs fixed orientation (portrait mode). But when the phone is rotated, one control on that view needs to be moved and rotated (so that it will always be in the upper left corner, and its text will be readable). I am achieving this by shifting the control(a UIView) using the frame-property of my control (it is a custom view, more on that later), and then using CGAffineTRansformMakeRotate() afterwards, since i know that its not advisable to use the frame after rotating a view. everything is fine so far, but here's the thing: That custom view has three UIButtons of type UIButtonTypeCustom as its subviews. Because i rotated the View, but cannot rotate the buttons inside the view (they are not squares), i need to rotate the titleLabels of the Buttons for the text to be readable in the new deviceOrientation. But it wont work very well. The text will be rotated, as i intended, but it will be clipped by the titleLabel, because the titleLabel has the wrong frame. i checked this by applying borders to the label. So i need to change the titleLabels frame, right? But how can i do that? I tried setting it using [titleLabel setFrame: frameThatFits];, but to no avail. (frameThatFits is a CGRect i created). also,, calling [button.titleLabel sizeToFit]; has no effect that i could see. I am using [button setTitle:title forControlState: UIControlStateNormal];to set the title.

TL; DR :在使用仿射变换旋转后,我试图更改UIButtons titleLabel的边框/边界。任何帮助?
感谢。

TL;DR: Im trying to change the frame/bounds of a UIButtons titleLabel after rotating it using an affine transformation. Any help? Thanks.

PS:我可以在需要时提供代码,但我不知道该告诉你什么。

PS: I can supply code when needed, but i wouldnt know what to show you. Tell me what you need, ill post it.

推荐答案

确定,首先,感谢所有试图帮助的人。为我的问题发布一个替代解决方案,虽然它没有真正解决更改titleLabels维度的问题,它将导致我的ViewController的正确显示。
原来使用框架是一个坏主意。我最初使用框架重新定位视图,我想这不会是一个问题,因为我以前只应用转换,但我错了。因为OBVIOUSLY我试图改变titleLabels帧。旋转后。这没有工作。
所以,这里的方法是在整个代码中使用center-property和视图的bouds。这将导致正确旋转的按钮,后不需要任何阻碍。
我在这里的介绍是,我永远不会再次使用NSLog语句外的框架属性。但为什么 [button sizeToFit]; couldnt产生任何结果仍然超出我。如果我想出来,我可能会发布,如果我记得。

OK, first of all, thanks to everyone who tried to help. Im posting an alternative solution for my problem, and although it doesnt really address the problem of changing the titleLabels dimensions, it will result in the proper display of my ViewController. It turns out using the frame is a bad idea. I initially used the frame to reposition the view and i figured that this couldnt be a problem because i only ever applied transformations afterwards, but i was wrong. Because OBVIOUSLY i tried to change the titleLabels frame. AFTER the rotation. And that didnt work. So the way to go here is using the center-property and the bouds of the view consistently throughout the code. It will result in properly rotated Buttons, that do not need any fidgeting afterwards. My takeaway here is that i will never ever again use the frame-property outside of a NSLog-statement. But why [button sizeToFit];wouldnt yield any results is still beyond me. If i ever figure it out, i might post it if i remember.

编辑:
@ZevEisenberg用这个评论:
警告:如果transform属性不是identity变换,那么该属性的值是未定义的,因此应该被忽略。所以你是正确的使用中心和边界在这里,但如果你没有一个变换,框架是完美的安全使用。

@ZevEisenberg nailed it with this comment: "Warning: If the transform property is not the identity transform, the value of this property is undefined and therefore should be ignored." So you are right to use the center and bounds here, but if you do not have a transform, the frame is perfectly safe to use.

下一个编辑:
我如何重新定位按钮:

NEXT Heres how i ended up repositioning the Buttons:

-(CGPoint)centerForView:(UIView *)view{
    //calculate a suitableposition for the view
    //depending on the current orientation and the device type (iphone 4S/5, etc)
    return point;
}

然后,作为对deviceOrientation更改通知的反应,我将CGAffineTransformIdentity应用于所有视图,使用上面显示的centerForView重新定位它们,并对视图应用正确的旋转变换。我对每一次divice旋转时所有的subviews这样做:

Then, as a reaction to the deviceOrientation change notification, i apply CGAffineTransformIdentity to all the views, reposition them using my centerForView shown above, and apply the correct rotation transformation to the View. I do this for all the subviews every time the divice rotates, like so:

-(void)setRightRotationTransformations{
[self resetAllTransformations];
self.someSubview.transform = CGAffineTransformRotate(self.someSubview.transform, -M_PI_2);
}

这篇关于UIButtons titleLabel剪辑由CGAffineTransformMakeRotate()旋转后的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆