UIButton文字居左显示
2015-08-25 17:00
316 查看
UILabel文字居左显示
实现文字居左显示代码如下:UILabel *label = [[UILabel alloc] init]; label.frame = CGRectMake(50, 100, 200, 50); label.text = @"Label is me"; label.textAlignment = UITextAlignmentLeft; label.backgroundColor = [UIColor orangeColor]; [self.view addSubview:label];
运行的效果如下:
label.backgroundColor = [UIColor orangeColor]; 设置label的背景颜色方便我们参考。
UIButton文字居左显示
创建UIButton// 创建按钮对象 UIButton *button = [[UIButton alloc] init]; // 设置坐标 button.frame = CGRectMake(100, 100, 150, 100); // 设置标题 [button setTitle:@"button for JH" forState:UIControlStateNormal]; // 设置标题颜色 [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; // 设置背景颜色 [button setBackgroundColor:[UIColor orangeColor]]; [self.view addSubview:button];
让文字居左
按照UILabel文字居左的写法,UIButton应该这么写:button.titleLabel.textAlignment = NSTextAlignmentLeft;运行效果如下:
typedef NS_ENUM(NSInteger, UIControlContentVerticalAlignment) { UIControlContentVerticalAlignmentCenter = 0, UIControlContentVerticalAlignmentTop = 1, UIControlContentVerticalAlignmentBottom = 2, UIControlContentVerticalAlignmentFill = 3, }; typedef NS_ENUM(NSInteger, UIControlContentHorizontalAlignment) { UIControlContentHorizontalAlignmentCenter = 0, UIControlContentHorizontalAlignmentLeft = 1, UIControlContentHorizontalAlignmentRight = 2, UIControlContentHorizontalAlignmentFill = 3, };
设置contentHorizontalAlignment
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
果然可以,是不是看着居左显示很难看,太靠边了。很简单,设置UIButton的titleEdgeInsets属性:
button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
这样button的title就距左边10个像素的距离。
居右显示就简单了:
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
enen.UIButton文字居左显示完成了。 谢谢。
相关文章推荐
- Solr: index product and price for sellers and perfoming query and sorting
- zoj 1292 Integer Inquiry【大数】
- IOS瀑布流通过UICollectionView控件实现
- easyui formatter 操作列传多个参数的写法
- easyUI的列名实现简单的树形结构
- UI中的响应时间:3个重要的界限
- iOS开发系列--transform和UIScrollView学习
- IOS:UILocalNotification使用
- poj2749 Building roads 2-sat
- 手动Handler与Thread处理UI数据同步问题
- POJ-1503 Integer Inquiry-多个大数相加
- POJ-2299 Ultra-QuickSort-分治法排序求交换速度
- UIScrollView使用
- 无需编码实现UI动效:XCode+Canvas
- easyui-combobox如何使用选择器
- 代码里手动编写UI
- building live555 library for iphoneos
- POJ 2524 Ubiquitous Religions 并查集
- [leetcode] 95.Unique Binary Search Trees II
- UILabel设置圆角和背景