【问题标题】:Vertical text in a Horizontal UIButton水平 UIButton 中的垂直文本
【发布时间】:2012-10-25 04:45:00
【问题描述】:

我在纵向应用程序中使用垂直 UIButton(只是一个宽度为 60 和高度为 160 的普通按钮)

我想将标签 Vetically 放在按钮下方,而不是放在按钮上方。

当我使用以下代码旋转标签时

    [workPackageButton.titleLabel setTransform:CGAffineTransformMakeRotation(M_PI / 2)];

它会旋转标签,但长度似乎受到原始宽度的限制,所以我在中间得到了 ... 缩写。有没有简单的方法解决这个问题?

【问题讨论】:

  • 使用子类查看下面的答案。

标签: ios objective-c text uibutton vertical-alignment


【解决方案1】:

您可以在按钮上添加标签并旋转标签,如下所示:

UILabel *lbl= [[UILabel alloc] initWithFrame:CGRectMake(button.frame.size.width*.3, button.frame.size.height*.5, button.frame.size.width,button.frame.size.height)];
lbl.transform = CGAffineTransformMakeRotation(M_PI / 2);
lbl.textColor =[UIColor whiteColor];
lbl.backgroundColor =[UIColor clearColor];
[button addSubview:lbl];

【讨论】:

  • 当你这样做时,是否可以与按钮交互?
  • 我试过但帧值不匹配,我试过 CGRectMake(0, 0, button.frame.size.height,button.frame.size.weight) 它比以前更接近..跨度>
  • 其实我是这样用的。 lblC= [[UILabel alloc] initWithFrame:CGRectMake(button.frame.size.width*.39, button.frame.size.height*.50, button.frame.size.width*.7,button.frame.size.高度/5)]; lblC.backgroundColor =[UIColor yellowColor]; lblC.transform = CGAffineTransformMakeRotation(degreesToRadian(38)); [按钮 addSubview:lblC];
  • 谢谢 这很好用。 (虽然我坚持使用 M_PI /2)为帮助而欢呼。 :)
  • 更新:这在我第一次使用时有效。但是我第二次使用它时,我得到了 AutoLayout 的内部不一致错误 ----->> NSInternalInconsistencyException',原因:'Autolayout 不支持带有边缘布局约束的交叉旋转边界变换,例如右、左、上、底部。有问题的视图是: -----
【解决方案2】:

我知道这是一个旧线程,但另一种方法是对按钮进行子类化并为按钮标签指定正方形大小。

@interface RotatingButton : UIButton

@end


@implementation RotatingButton

- (CGRect) titleRectForContentRect:(CGRect)bounds {
    CGRect frame = [super titleRectForContentRect:bounds];

    frame.origin.y -= (frame.size.width - frame.size.height) / 2;
    frame.size.height = frame.size.width;

    return frame;
}

@end

在代码或 Storyboard 上创建的任何 RotatingButton 的标签都可以旋转而不会被截断。

[button.titleLabel setTransform:CGAffineTransformMakeRotation(M_PI / 2)];

【讨论】:

  • 我在按钮 frame.origin.y = 0 frame.size.height = self.bounds.size.height; 中使用了这个作为全尺寸标签
【解决方案3】:

我为堆栈视图中的按钮添加了同样的问题 所以我需要一些动态的东西

对于 Swift 4.2

class VerticalButton: UIButton {

    override func titleRect(forContentRect bounds: CGRect) -> CGRect {
        var frame: CGRect = super.titleRect(forContentRect: bounds)

        frame.origin.y = 0
        frame.size.height = bounds.size.height

        return frame
    }
}
extension UILabel {
    @IBInspectable
    var rotation: Int {
        get {
            return 0
        } set {
            let radians = CGFloat(CGFloat(Double.pi) * CGFloat(newValue) / CGFloat(180.0))
            self.transform = CGAffineTransform(rotationAngle: radians)
        }
    }
}

    let button = VerticalButton.init(type: UIButton.ButtonType.custom)
    button.titleLabel?.textAlignment = .center
    button.titleLabel?.rotation = -90

【讨论】:

  • 欢迎来到stackoverflow。除了您提供的答案之外,请考虑提供一个简短说明,说明为什么以及如何解决此问题。
  • 我在swift中添加了同样的问题,我花了很多时间才找到这个解决方案。在按钮中旋转标签可能很棘手
【解决方案4】:

我从未这样做过,但您是否尝试过将按钮创建为水平按钮 (160w x 60h) 然后旋转整个按钮?

【讨论】:

  • 伊恩。你愿意这样想。但是当你尝试这种方法时,每一个想法都会让你感到害怕(这是我开始的地方)...... :)
【解决方案5】:

为了方便,在属性检查器中对按钮进行以下设置:

  • 类型:自定义
  • 标题:归属

接下来,选择左/中/右对齐或两端对齐。不要使用“自然对齐”。

现在

[workPackageButton.titleLabel setTransform:CGAffineTransformMakeRotation(M_PI / 2)];

在标签未缩写(但始终居中对齐...)的情况下按预期工作。

使用 XCode 6.3 测试。

【讨论】:

    猜你喜欢
    • 2015-09-25
    • 2020-04-06
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 1970-01-01
    • 2012-03-14
    • 2017-06-28
    相关资源
    最近更新 更多