IOS截取指定宽度

时间:2017-08-08 10:10:57

标签: ios

effect要求截取显示屏上宽度为3/2的字符串显示...

希望得到你的帮助,谢谢!

if (meetModel.title.length > 18) {
    title = [NSString stringWithFormat:@"%@...", [meetModel.title substringToIndex:18]];
} else {
    title = meetModel.title;
}

这种方法不符合我的需要。页面显示为中文。

1 个答案:

答案 0 :(得分:1)

我没有清楚地理解你的问题,但我认为你需要添加这样的东西(例子在Swift 3.1中)。我们的想法是你应该设置宽度约束,如果文本大于这个宽度,它应该被自动截断。

yourLabel.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.66).isActive = true

// It's needed for adding three dots in the end, if the string is larger then it should be
yourLabel.adjustsFontSizeToFitWidth = false
yourLabel.lineBreakMode = .byTruncatingTail