iOS添加UIButton源代码:can';t控件UIButton标题栏文本对齐
我在iOS添加UIButton源代码:can';t控件UIButton标题栏文本对齐,ios,objective-c,uibutton,Ios,Objective C,Uibutton,我在ui视图中添加了ui按钮 UIButton *switchButtonL = [UIButton buttonWithType: UIButtonTypeSystem]; [switchButtonL addTarget:self action:@selector(switchCity:) forControlEvents:UIControlEventTouchUpInside]; switchButtonL.frame = CGRectMake(0, 50, se
ui视图中添加了ui按钮
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];
注释行无法工作,文本始终居中对齐。
您可以从上图所示的xib更改它。如果您想通过代码进行更改,请在您所需的职位上使用:
self.yourbtn.titleEdgeInsets=UIEdgeInsetsMake(topVal, leftVal, downVal, rightVal);
您可以从上图所示的xib更改它。如果您想通过代码进行更改,请在您所需的职位上使用:
self.yourbtn.titleEdgeInsets=UIEdgeInsetsMake(topVal, leftVal, downVal, rightVal);
试试这个
switchButtonL.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
switchButtonL.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
试试这个
switchButtonL.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
switchButtonL.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
试用替代品
switchButtonL.titleLabel.textAlignment = NSTextAlignmentLeft; // this line can't work
与:
试用替代品
switchButtonL.titleLabel.textAlignment = NSTextAlignmentLeft; // this line can't work
与:
如果任何答案与u相关,请打勾如果任何答案与u相关,请打勾