简体   繁体   English

如何在uilabel中显示连字符?

[英]How to display hyphen in uilabel?

How to display hyphen with UILabel like this, - AI origine - , Here I use the string appending method. 如何使用UILabel这样显示连字符UILabel origine- ,在这里我使用字符串附加方法。 I get this type of output - À l'origine de la guerre - . 我得到这种输出-Àl'originede la guerre- But I want display hyphen before the starting point of text and display hyphen after 10 charatcers. 但是我想在文本的起点之前显示连字符,并在10个字符之后显示连字符。

I was searched but i can't get valied source. 我被搜查了,但是我无法得到任何消息来源。 kindly give any suggestion if you know. 如果您知道的话,请提出任何建议。

 NSString *tempStr = @" - ";
    tempStr = [tempStr stringByAppendingString:NSLocalizedString(@"OriginallyWar", @"")];
    tempStr = [tempStr stringByAppendingString:@" -"];
    [headingLabel setText:tempStr];
    [headingLabel setFont:MRSEAVES_BOLD(17)];

使用NSMutableString并插入字符,

[yourString insertString:@"-" atIndex:10];

if you are using StoryBoard directly set it to the text property on Attribute inspector. 如果您使用的是StoryBoard,则将其直接设置为Attribute inspector上的text属性。 Put 10 empty spaces after the end of character and the -. 在字符末尾和-后面放置10个空格。

You may try this code 您可以尝试此代码

  NSString *inputString = @"OriginallyWarDFdfsdfdDFSDfdsfdsfDFdsfadsfawerdsaf";
  NSMutableString *localizedInputString = [NSMutableString stringWithString:NSLocalizedString(inputString, @"")];

  int numberOfCharacters = localizedInputString.length;
  int numberOf10s = (numberOfCharacters/10 + 1);
  int numberOfCharactersToBeInserted = 0;

  for (int i = 1; i < numberOf10s; i++) {
    int characterIndex = (i * 10) + numberOfCharactersToBeInserted;
    if (i == (numberOf10s - 1) && numberOfCharacters % 10 == 0) {
      [localizedInputString insertString:@" -" atIndex:characterIndex];
      numberOfCharactersToBeInserted = 2 * i;
    } else {
      [localizedInputString insertString:@" - " atIndex:characterIndex];
      numberOfCharactersToBeInserted = 3 * i;
    }
  }
  if (numberOfCharacters == 0) {
    [localizedInputString insertString:@"-" atIndex:0];
  } else {
    [localizedInputString insertString:@"- " atIndex:0];
  }

  NSLog(@"localizedInputString : %@", localizedInputString);

try using NSMutableString 尝试使用NSMutableString

NSString *tempStr = @" - ";
tempStr = [tempStr stringByAppendingString:NSLocalizedString(@"OriginallyWar", @"")];
NSMutableString *tempStrMutable=[[NSMutableString alloc]initWithString:tempStr];
[tempStrMutable insertString:@"-" atIndex:10];
[headingLabel setText:tempStrMutable];

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM