iOS的:算法中心词在句子中一个UILabel内 [英] iOS: Algorithm to center a word in a sentence inside of a UILabel
问题描述
我需要通过截断长句的开头和结尾一个UILabel内居中在一个句子中一个特定的词,例如:
I need to center a particular word in a sentence by truncating the beginning and endings of a long sentence inside of a UILabel, For example
NSString mySentence = @"This is my very long sentence to give you an example of what I am trying to do.. And its still going..";
NSString myWord = @"example";
<Algorithm goes here>
应显示:
......一句话给你什么,我想......一个示例
"...sentence to give you an example of what I am trying to..."
如果这个词是接近一端或其他,只是尽你所能地居中显示文本适量,例如:
If the word is closer to one end or the other, just do your best to center and display an appropriate amount of the text, example:
NSString myWord = @"my";
这是我很长的句子给你一个什么样的一个例子。
"This is my very long sentence to give you an example of what..."
任何想法?
感谢。
推荐答案
我相信你可以做你的搜索放在扫描。比方说,你有指数在名为centerWordIndex变量。然后,您可以将字符串基于whitechars分裂,并添加单词的开头和你的话的结尾,直到你出在每边的话,或者直到字符串的大小标签的尺寸相匹配。
I believe you can do a scan for your search to be placed. Let's say you have the index in a variable named centerWordIndex. You could then split the string based on whitechars, and add words to the beginning and the end of your word until you are out of words at each side, or until the size of you string matches the size of the label.
你怎么看:)?
这篇关于iOS的:算法中心词在句子中一个UILabel内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!