1

在 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
}
4

3 回答 3

0

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

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

然后处理你想要的大小,像其他人描述的那样循环并添加空间,直到你满意为止。

于 2013-01-28T15:44:54.877 回答
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];
于 2013-01-28T15:35:18.077 回答
0

基本的呢

for(int i=0; i<number ;i++){
   string=[string stringByAppendingString:@" "];
}
于 2013-01-28T15:34:16.907 回答