简体   繁体   English

设置NSLineBreakByTruncatingHead无效

[英]Setting NSLineBreakByTruncatingHead has no effect

I have the following lines of code: 我有以下几行代码:

textView.textContainer.maximumNumberOfLines = 10;
textView.textContainer.lineBreakMode = NSLineBreakByTruncatingHead;
[textView setText:@"1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11"];

No matter what I set the lineBreakMode to, the output always excludes the 11, but includes the 1. I need to truncate the start of the string, not the end. 无论我将lineBreakMode设置为什么,输出始终排除11,但包括1。我需要截断字符串的开头,而不是结尾。 Any ideas on why this isn't working? 有什么想法为什么不起作用?

That's not what the line break mode does. 这不是换行模式的功能。 If an individual line is wider than will fit in the text view, that mode should cause it to get truncated at the head rather than wrapping to an additional line, but it won't cause the text view to change which lines it displays. 如果单个行的宽度超出了文本视图的宽度,则该模式应使它在头部被截断而不是换行到另一行,但这不会导致文本视图更改其显示的行。

To get the effect you're after, you'll have to modify the string yourself. 为了获得想要的效果,您必须自己修改字符串。 If it's formatted the way your test string is (with explicit newlines) that's relatively easy— -rangeOfString:options:range: will let you search backwards along the string until you find the start of the Nth line you want to keep—but if you're relying on word wrapping, then you'll have to use -boundingRectWithSize:options:attributes: and keep chopping words off the beginning until it fits within the size you need. 如果将其格式化为测试字符串的方式(使用显式换行符),则相对比较容易-rangeOfString:options:range:将使您沿字符串向后搜索,直到找到要保留的第N行的开头,但如果依靠自动换行,那么您必须使用-boundingRectWithSize:options:attributes:-boundingRectWithSize:options:attributes:开始切掉单词,直到适合您所需的大小为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在MPNowPlayingInfoCenter中设置值无效 - Setting values in MPNowPlayingInfoCenter has no effect 在UITableView子类中设置UITableViewDataSource无效 - Setting UITableViewDataSource within UITableView subclass has no effect 设置自定义UIButton class的backgroundColor没有效果? - Setting backgroundColor of custom UIButton class has no effect? 设置UINavigationBar的背景颜色没有效果? - Setting the background color of UINavigationBar has no effect? 将 videoGravity 设置为 AVLayerVideoGravityResizeAspectFill 对 iOS 5.0 没有影响 - Setting videoGravity to AVLayerVideoGravityResizeAspectFill has no effect on iOS 5.0 在Cordova 3.0.0 for iOS中设置AutoHideSplashScreen无效 - Setting AutoHideSplashScreen has no effect in Cordova 3.0.0 for iOS 在loadView中设置UIView的backgroundColor无效 - Setting UIView's backgroundColor in loadView has no effect Swift:设置按钮的高度和宽度没有效果 - Swift: Setting the height and width of buttons has no effect 为customView设置背景颜色没有效果 - Setting background color for customView has no effect 在viewDidLoad / viewWillAppear中设置我的UIScrollView的内容大小没有效果 - 为什么? - Setting the content size of my UIScrollView in viewDidLoad/viewWillAppear has no effect - why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM