IOS UILabel 文字过长处理
2015-05-11 16:45
113 查看
一直没有注意到UILabel 的各种属性,没有深入研究过,今天做定位功能的时候遇到了问题,所以记下来,以便以后再次遇到,还需要查阅资料
我们看到上图中间navigationItem.titleView 是由一个背景View
和 三个控件(imageView ,Label ,imageView)组成的
我们的需求是,俩imageView 分别在Label左右侧等距离 而当 Label的width>= 160的时候,显示Label 多余文字...
我是这样实现的哦
//中
UIView *navTitleView = [[UIView
alloc] initWithFrame:KCGRect(0,
0, 180,
16)];
locationLabel = [[UILabel
alloc] initWithFrame:KCGRect(16,
0, 148,
16)];
locationLabel.textAlignment =
NSTextAlignmentCenter;
locationLabel.text =
@"点击定位您的位置";
locationLabel.textColor = [UIColor
whiteColor];
locationLabel.font = [UIFont
systemFontOfSize:16.0];
[locationLabel
sizeToFit];
//这里用到一个UIExit第三方类,可以直接获取width
if (locationLabel.width >=
160) {
locationLabel.lineBreakMode =
NSLineBreakByTruncatingMiddle;
locationLabel.width =
160;
}
locationLabel.center = navTitleView.center;
UIImageView *fisrtImageView = [[UIImageView
alloc] initWithFrame:KCGRect(0,
0, 16,
16)];
fisrtImageView.userInteractionEnabled =
YES;
fisrtImageView.image = [UIImage
imageNamed:@"locationTitle@2x.png"];
fisrtImageView.right =
locationLabel.left -
KCGRectWithx(10.0);
UIImageView *secondImageView = [[UIImageView
alloc]
initWithFrame:KCGRect(162,
0, 16,
16)];
secondImageView.left =
locationLabel.right +KCGRectWithx(10.0);
secondImageView.image = [UIImage
imageNamed:@"homeArrowDown@2x.png"];
//定位完成之后,将locationButton重新赋值
[navTitleView
addSubview:locationLabel];
[navTitleView
addSubview:secondImageView];
[navTitleView
addSubview:fisrtImageView];
self.navigationItem.titleView = navTitleView;
是我浅薄了啊,查了一圈才想到
typedef
NS_ENUM(NSInteger, NSLineBreakMode) {
/* What to do with long lines */
NSLineBreakByWordWrapping =
0, /* Wrap at word boundaries, default */
NSLineBreakByCharWrapping,
/* Wrap at character boundaries */
NSLineBreakByClipping,
/* Simply clip */
NSLineBreakByTruncatingHead,
/* Truncate at head of line: "...wxyz" */
NSLineBreakByTruncatingTail,
/* Truncate at tail of line: "abcd..." */
NSLineBreakByTruncatingMiddle
/* Truncate middle of line: "ab...yz" */
} NS_ENUM_AVAILABLE_IOS(6_0);
一篇没什么水平的文章,希望也能帮到大家,让我自己记清楚
我们看到上图中间navigationItem.titleView 是由一个背景View
和 三个控件(imageView ,Label ,imageView)组成的
我们的需求是,俩imageView 分别在Label左右侧等距离 而当 Label的width>= 160的时候,显示Label 多余文字...
我是这样实现的哦
//中
UIView *navTitleView = [[UIView
alloc] initWithFrame:KCGRect(0,
0, 180,
16)];
locationLabel = [[UILabel
alloc] initWithFrame:KCGRect(16,
0, 148,
16)];
locationLabel.textAlignment =
NSTextAlignmentCenter;
locationLabel.text =
@"点击定位您的位置";
locationLabel.textColor = [UIColor
whiteColor];
locationLabel.font = [UIFont
systemFontOfSize:16.0];
[locationLabel
sizeToFit];
//这里用到一个UIExit第三方类,可以直接获取width
if (locationLabel.width >=
160) {
locationLabel.lineBreakMode =
NSLineBreakByTruncatingMiddle;
locationLabel.width =
160;
}
locationLabel.center = navTitleView.center;
UIImageView *fisrtImageView = [[UIImageView
alloc] initWithFrame:KCGRect(0,
0, 16,
16)];
fisrtImageView.userInteractionEnabled =
YES;
fisrtImageView.image = [UIImage
imageNamed:@"locationTitle@2x.png"];
fisrtImageView.right =
locationLabel.left -
KCGRectWithx(10.0);
UIImageView *secondImageView = [[UIImageView
alloc]
initWithFrame:KCGRect(162,
0, 16,
16)];
secondImageView.left =
locationLabel.right +KCGRectWithx(10.0);
secondImageView.image = [UIImage
imageNamed:@"homeArrowDown@2x.png"];
//定位完成之后,将locationButton重新赋值
[navTitleView
addSubview:locationLabel];
[navTitleView
addSubview:secondImageView];
[navTitleView
addSubview:fisrtImageView];
self.navigationItem.titleView = navTitleView;
是我浅薄了啊,查了一圈才想到
typedef
NS_ENUM(NSInteger, NSLineBreakMode) {
/* What to do with long lines */
NSLineBreakByWordWrapping =
0, /* Wrap at word boundaries, default */
NSLineBreakByCharWrapping,
/* Wrap at character boundaries */
NSLineBreakByClipping,
/* Simply clip */
NSLineBreakByTruncatingHead,
/* Truncate at head of line: "...wxyz" */
NSLineBreakByTruncatingTail,
/* Truncate at tail of line: "abcd..." */
NSLineBreakByTruncatingMiddle
/* Truncate middle of line: "ab...yz" */
} NS_ENUM_AVAILABLE_IOS(6_0);
一篇没什么水平的文章,希望也能帮到大家,让我自己记清楚
相关文章推荐
- iOS UILabel多行时文字向下偏移
- UILabel标签文字过长时的显示方式
- iOS UILabel自适应里面的文字,自动调整宽度和高度
- iOS开发技巧 - 使UILabel中的文字吸顶(顶部对齐)
- iOS UILabel 文字自动左上角对齐
- UILabel换行与多行文字显示处理
- echarts2 饼图处理标签文字过长使之达到指定字数换行的目的
- iOS开发之UIlabel多行文字自动换行 (自动折行)
- UILabel文字过长时的显示格式
- UILabel文字过长时的文字省略位置
- iOS -让UILabel的文字顶部对齐
- iOS开发之UIlabel多行文字自动换行 (自动折行)
- NSTableView 文字过长的处理代码
- iOS 用NSAttributedString处理文字大小不一致情况
- iOS开发小技巧--即时通讯项目:使用富文本在UILabel中显示图片和文字;使用富文本占位显示图片
- iOS 在一个UILabel里显示不同的颜色,给文字加下划线
- UILabel标签文字过长时的显示方式
- ios tableView那些事 (六) 如何处理tableView上的文字
- iOS-UILabel的宽度自适应文字
- iOS学习5:UILabel的使用 (文字颜色,高度,行数) 设置