根据给定的大小创建一个空字符串(由空格构成)

时间:2013-01-28 15:30:36

标签: ios nsstring

在Iphone应用程序中,是否有办法为标签文本添加空格以创建空白空间(由大小指定)。这背后的想法是我想在标签上附加第二个字符串,但是用固定大小分隔。

这样的事情:

//first we append a string containing only spaces to create a width of 100
label1.text = [label1.text stringByAppendingString:[self getSpaceStringByWidth:100 font:label1.font];
//then we append the second string
label1.text = [label1.text stringByAppendingString:@"something else to append"];


-(NSString*)getSpaceStringByWidth:(CGFloat)width andFont:(UIFont*)font
{
    //This method takes the width needed and the font used and returns a string containing only empty spaces that will create an empty space before appending the second string
}

3 个答案:

答案 0 :(得分:0)

基本

怎么样?
for(int i=0; i<number ;i++){
   string=[string stringByAppendingString:@" "];
}

答案 1 :(得分:0)

也许这会有所帮助:

NSString *concatenateString = [[myString1 stringByAppendingString:@" "] stringByAppendingString:myString2];

编辑(看到@AKV回答后)

for(int i=0; i<number ;i++){
   string=[string stringByAppendingString:@" "];
}

NSString *concatenateString = [[myString1 stringByAppendingString:string] stringByAppendingString:myString2];

答案 2 :(得分:0)

你需要得到像这样的当前大小..

NSDictionary *attr = [NSDictionary dictionaryWithObject:[label1 font]
                                                 forKey:NSFontAttributeName];
NSSize stringSize = [label1.text sizeWithAttributes:attr];
NSSize sizeOfSpace = [@" " sizeWithAttributes:attr];

然后处理你想要的大小,循环并添加像其他人描述的空格,直到你开心。