2023年7月13日发(作者:)
UIButton设置textAlignment属性的⽅法tHorizontalAlignment =UIControlContentHorizontalAlignmentLeft;tHorizontalAlignment =UIControlContentHorizontalAlignmentRight;tHorizontalAlignment =UIControlContentVerticalAlignmentLeft;前⾔⼤家都知道UIButton按钮是IOS开发中最常⽤的控件,作为IOS基础学习教程知识 ,初学者需要了解其基本定义和常⽤设置,以便在开发在熟练运⽤。⼀、iOS设置UIButton的字体⼤⼩ = CGRectMake(x, y, width, height);
[btn setTitle: @"search" forState: UIControlStateNormal];
//设置按钮上的⾃体的⼤⼩
// [btn setFont: [UIFont systemFontSize: 14.0]]; //这种可以⽤来设置字体的⼤⼩,但是可能会在将来的SDK版本中去除改⽅法
//应该使⽤ = [UIFont systemFontOfSize: 14.0]; [btn seBackgroundColor: [UIColor blueColor]];
//最后将按钮加⼊到指定视图superView [superView addSubview: btn];⼆、iOS设置UIButton的⽂字显⽰位置 tvnamelabel=[[UIButton alloc]initWithFrame:CGRectMake(5,5,200,40)];这样初始化的button,⽂字默认颜⾊是⽩⾊的,所有如果背景也是⽩⾊的话,是看不到⽂字的,tHorizontalAlignment=UIControlContentHorizontalAlignmentLeft ;设置⽂字位置,现设为居左,默认的是居中[btn setTitle:@“title”forState:UIControlStateNormal];// 添加⽂字有些时候我们想让UIButton的title居左对齐,我们设置 ignment = UITextAlignmentLeft是没有作⽤的,我们需要设置: tHorizontalAlignment = UIControlContentHorizonAlignmentLeft;但是问题⼜出来,此时⽂字会紧贴到做边框,我们可以设置: tEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);使⽂字距离做边框保持10个像素的距离。三、iOS设置UIButton的字体颜⾊设置UIButton上字体的颜⾊设置UIButton上字体的颜⾊,不是⽤: [abel setTextColor:[UIColorblackColor]]; lor=[UIColor redColor];⽽是⽤: [btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];总结以上就是这篇⽂章的全部内容了,希望能对各位iOS开发者们有所帮助,如果有疑问⼤家可以留⾔交流。以上就是iOS设置UIButton⽂字显⽰位置和字体⼤⼩、颜⾊的⽅法的详细内容。四、图⽚和⽂字的位置:⼀、直接设置左⽂字右图,利⽤原有属性
semanticContentAttribute在需要图⽚和⽂字,⽽且还能点击的控件,button 是当之⽆愧的选择,button 默认的模式是图⽚在左,⽂字在右,以往需要调整⽂字和图⽚的顺序的时候,需要重写
button,今天发现了⼀个新的办法,就是⼀个叫
semanticContentAttribute 的属性,有四个选择,默认就是
Unspecified,然后选择Force Right-to-Left 就变成图⽚在右边,⽂字在左边的效果了icContentAttribute = UISemanticContentAttributeForceRightToLeft;五、例如: _teamBut = [[UIButton alloc] initWithFrame:CGRectMake([UIScreen mainScreen]./375*171, [UIScreen mainScreen]./375*2, [UI [_teamBut setTitle:@"队内赛" forState:UIControlStateNormal]; [_teamBut setTitleColor:RGBA(102, 102, 102, 1) forState:UIControlStateNormal]; _ = [UIFont systemFontOfSize:[UIScreen mainScreen]./375*16 weight:UIFontWeightMedium]; _oBounds = YES; _Radius = [UIScreen mainScreen]./375*4;
// 内容:位置left,外置⼤⼩,0位默认 _tHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; _tEdgeInsets = UIEdgeInsetsMake(0, [UIScreen mainScreen]./375*33, 0, [UIScreen mainScreen]./375*55);
_teamButTipsL = [[UILabel alloc] init]; _ = @"发起"; _lor = RGBA(223, 109, 53, 1); _ignment = NSTextAlignmentLeft; _ = [UIFont systemFontOfSize:[UIScreen mainScreen]./375*8 weight:UIFontWeightRegular]; [t addSubview:tTipsL]; [tTipsL makeConstraints:^(MASConstraintMaker *make) { _equalTo(_right).offset([UIScreen mainScreen]./375*5); _equalTo(abel); _equalTo(_right).offset(-[UIScreen mainScreen]./375*5); }];
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689262432a226319.html
评论列表(0条)