gpt4 book ai didi

iOS 从代码 : can't control UIButton titlebar text alignment 添加 UIButton

转载 作者:行者123 更新时间:2023-11-28 19:01:37 24 4
gpt4 key购买 nike

我在 UIView 中添加了一个 UIButton

    UIButton *switchButtonL = [UIButton buttonWithType: UIButtonTypeSystem];
[switchButtonL addTarget:self action:@selector(switchCity:) forControlEvents:UIControlEventTouchUpInside];
switchButtonL.frame = CGRectMake(0, 50, self.screenWidth/2, 30);
[switchButtonL setTitle: NSLocalizedString(@"switchcity", nil) forState:UIControlStateNormal];
[switchButtonL setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[switchButtonL setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[switchButtonL setBackgroundColor:[UIColor clearColor] ];
switchButtonL.titleLabel.textAlignment = NSTextAlignmentLeft; // this line can't work
switchButtonL.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:18];
[self addSubview:switchButtonL];

并且注释行不起作用,文本总是居中对齐。

最佳答案

尝试替代

switchButtonL.titleLabel.textAlignment = NSTextAlignmentLeft; // this line can't work

与:

switchButtonL.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
switchButtonL.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);

关于iOS 从代码 : can't control UIButton titlebar text alignment 添加 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24507868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com