0

セルのラベルに取り消し線のサブビューがあり、その幅は行のテキストに応じて設定されています。セルのラベルの行数は 2 に設定されており、2 行の場合はラベルに 2 つの取り消し線を設定しています。

これは、テキストが 1 行だけのラベルに対して私が行っていることです。

 UIView *crossout = [[UIView alloc] init];
 crossout.frame = CGRectMake(x, y, w + 5, 2);
 crossout.backgroundColor = [UIColor colorWithRed: 0.0 / 255 green:175.0 / 255 blue: 30.0 / 255 alpha:1.0];
 crossout.tag = 1;
 [self.contentView addSubview:crossout];

次の方法で幅 (w) を取得しています。

 CGFloat w = [self.label.text sizeWithFont:[UIFont systemFontOfSize:15.0f]].width;

2 番目の取り消し線がある場合は、ラベルの長さから w を引くだけで、2 番目の取り消し線の幅が得られます。

ただし、問題は w がラベルのテキストの空白や空白を考慮していないため、改行全体で一貫して見えるとは限らないことです。

空白と空白が含まれるように w を計算するにはどうすればよいですか?

4

2 に答える 2

0

文字列内のインライン空白の数を取得するには、次のコードを試すことができます。

NSCharacterSet* whitespaceSet = [NSCharacterSet whitespaceCharacterSet];
NSArray* substringsArray = [yourStringToCheck componentsSeparatedByCharactersInSet: whitespaceSet]
NSLog(@"Number of whitespaces : %d", [substringsArray count]);

または、さらに良いかもしれません (ただし、これはおそらくタブなどを考慮していません):

NSLog(@"Number of whitespaces : %d", [[yourStringToCheck componentsSeparatedByString:@" "] count]);

別のオプション (NSMutableString):

NSUInteger replacementCount = [yourStringToCheck replaceOccurrencesOfString:@" " withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [receiver yourStringToCheck])];
于 2012-12-23T11:23:55.570 に答える
0

私はあなたがこれを使用できることを知ってUITextFieldいますが、試してみることができます..

NSString *resultingString = self.label.text;

NSCharacterSet *whitespaceSet = [NSCharacterSet whitespaceCharacterSet];

if  ([resultingString rangeOfCharacterFromSet:whitespaceSet].location == NSNotFound)         {
//here you can put your code

}
于 2012-12-23T06:20:16.343 に答える