ios 如何在 UILabel 中使第一个字母大写?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9022164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 16:31:26  来源:igfitidea点击:

How to make first letter uppercase in a UILabel?

iosobjective-cswiftcocoa-touchnsstring

提问by HardCode

I'm developing an iPhone app. In a label, I want to show an user's first letter of the name uppercase. How do I do that?

我正在开发一个 iPhone 应用程序。在标签中,我想显示用户名的首字母大写。我怎么做?

回答by beryllium

If there is only one word String, then use the method

如果只有一个字String,则使用该方法

-capitalized

-大写

let capitalizedString = myStr.capitalized // capitalizes every word

Otherwise, for multi word strings, you have to extract first character and make only that character upper case.

否则,对于多字串,您必须提取第一个字符并仅将该字符设为大写。

回答by José Manuel Sánchez

(2014-07-24: Currently accepted answer is not correct) The question is very specific: Make the first letter uppercase, leave the rest lowercase. Using capitalizedString produces a different result: “Capitalized String” instead of “Capitalized string”. There is another variant depending on the locale, which is capitalizedStringWithLocale, but it's not correct for spanish, right now it's using the same rules as in english, so this is how I'm doing it for spanish:

(2014-07-24: 目前接受的答案不正确) 问题很具体:第一个字母大写,其余小写。使用大写字符串会产生不同的结果:“大写字符串”而不是“大写字符串”。还有另一个取决于语言环境的变体,它是大写的StringWithLocale,但它不适用于西班牙语,现在它使用与英语相同的规则,所以这就是我对西班牙语的处理方式:

NSString *abc = @"this is test";

abc = [NSString stringWithFormat:@"%@%@",[[abc substringToIndex:1] uppercaseString],[abc substringFromIndex:1] ];       
NSLog(@"abc = %@",abc);

回答by winterized

In case someone is still interested in 2016, here is a Swift 3extension:

如果有人仍然对 2016 感兴趣,这里有一个Swift 3扩展:

extension String {
    func capitalizedFirst() -> String {
        let first = self[self.startIndex ..< self.index(startIndex, offsetBy: 1)]
        let rest = self[self.index(startIndex, offsetBy: 1) ..< self.endIndex]
        return first.uppercased() + rest.lowercased()
    }

    func capitalizedFirst(with: Locale?) -> String {
        let first = self[self.startIndex ..< self.index(startIndex, offsetBy: 1)]
        let rest = self[self.index(startIndex, offsetBy: 1) ..< self.endIndex]
        return first.uppercased(with: with) + rest.lowercased(with: with)
    }
}

Then you use it exactly as you would for the usual uppercased() or capitalized():

然后你可以像通常的大写()或大写()一样使用它:

myString.capitalizedFirst()or myString.capitalizedFirst(with: Locale.current)

myString.capitalizedFirst()或者 myString.capitalizedFirst(with: Locale.current)

回答by Amr Lotfy

Simply

简单地

- (NSString *)capitalizeFirstLetterOnlyOfString:(NSString *)string{
     NSMutableString *result = [string lowercaseString].mutableCopy;
     [result replaceCharactersInRange:NSMakeRange(0, 1) withString:[[result substringToIndex:1] capitalizedString]];

     return result;
}

回答by Dan Rosenstark

This is for your NSString+Utilcategory...

这是为您的NSString+Util类别...

- (NSString *) capitalizedFirstLetter {
    NSString *retVal;
    if (self.length < 2) {
        retVal = self.capitalizedString;
    } else {
        retVal = string(@"%@%@",[[self substringToIndex:1] uppercaseString],[self substringFromIndex:1]);
    }
    return retVal;
}

You can do that with NSString stringWithFormat, of course. I use this weirdness:

NSString stringWithFormat当然,你可以用 来做到这一点。我用这个奇怪:

#define string(...) \
[NSString stringWithFormat:__VA_ARGS__]

回答by calql8edkos

here's a swift extension for it

这是它的快速扩展

extension NSString {
    func capitalizeFirstLetter() -> NSString {
        return self.length > 1 ?
          self.substringToIndex(1).capitalizedString + self.substringFromIndex(1) :
          self.capitalizedString
    }
}

回答by ppalancica

This is how it worked for me:

这对我来说是这样的:

NSString *serverString = jsonObject[@"info"];

NSMutableString *textToDisplay = [NSMutableString stringWithFormat:@"%@", serverString];

[textToDisplay replaceCharactersInRange:NSMakeRange(0, 1) withString:[textToDisplay substringToIndex:1].capitalizedString];

cell.infoLabel.text = textToDisplay;

Hope it helps.

希望能帮助到你。

回答by Suraj K Thomas

As an extension to the accepted answer

作为已接受答案的扩展

capitalizedStringis used for making uppercase letters .

capitalizedString用于制作大写字母。

NSString *capitalizedString = [myStr capitalizedString]; // capitalizes every word

But if you have many words in a string and wants to get only first character as upper case use the below solution

但是,如果您在字符串中有很多单词并且只想获取第一个字符作为大写,请使用以下解决方案

NSString *firstCapitalChar = [[string substringToIndex:1] capitalizedString];
NSString *capString = [string stringByReplacingCharactersInRange:NSMakeRange(0,1) withString: capString];


// extract first character and make only that character upper case.

回答by Juan Boero

Swift:

迅速:

let userName = "hard CODE"
yourLabel.text = userName.localizedUppercaseString

I recommend using this localised version of uppercase, since names are locale sensitive.

我建议使用这个大写的本地化版本,因为名称是区域敏感的。