您的位置:首页 > 产品设计 > UI/UE

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);

一篇没什么水平的文章,希望也能帮到大家,让我自己记清楚
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: